@@ -10,6 +10,7 @@ import (
1010 "reflect"
1111 "regexp"
1212 "strings"
13+ "sync"
1314 "time"
1415)
1516
@@ -156,6 +157,8 @@ type Router interface {
156157 CurrentRequest () * Request
157158 // MatchRequest resolves a request to a route and binds its parameters.
158159 MatchRequest (request * Request ) (* Route , error )
160+ // FlushPending materializes deferred resource registrations.
161+ FlushPending ()
159162 // GetRoutes returns every registered route.
160163 GetRoutes () []* Route
161164
@@ -261,6 +264,7 @@ type router struct {
261264 fallback interface {}
262265 currentRoute * Route
263266 currentRequest * Request
267+ currentMu sync.RWMutex
264268 disableMiddleware bool
265269 controllers map [string ]any
266270 controllerDispatcher ControllerDispatcher
@@ -303,7 +307,9 @@ func (r *router) Dispatch(request any) *Response {
303307 default :
304308 return NotFoundResponse ()
305309 }
310+ r .currentMu .Lock ()
306311 r .currentRequest = req
312+ r .currentMu .Unlock ()
307313 for _ , fn := range r .onRouting {
308314 fn (req )
309315 }
@@ -355,7 +361,9 @@ func (r *router) findRoute(request *Request) (*Route, []*parameter, error) {
355361 if route .name != "" {
356362 request .Set ("_route_name" , route .name )
357363 }
364+ r .currentMu .Lock ()
358365 r .currentRoute = route
366+ r .currentMu .Unlock ()
359367 return route , params , nil
360368}
361369
@@ -1237,11 +1245,15 @@ func (r *router) ActionRoutePattern(action string) (string, bool) {
12371245
12381246// CurrentRoute returns the route matched by the most recent dispatch.
12391247func (r * router ) CurrentRoute () * Route {
1248+ r .currentMu .RLock ()
1249+ defer r .currentMu .RUnlock ()
12401250 return r .currentRoute
12411251}
12421252
12431253// CurrentRequest returns the request of the most recent dispatch.
12441254func (r * router ) CurrentRequest () * Request {
1255+ r .currentMu .RLock ()
1256+ defer r .currentMu .RUnlock ()
12451257 return r .currentRequest
12461258}
12471259
@@ -1252,6 +1264,9 @@ func (r *router) MatchRequest(request *Request) (*Route, error) {
12521264 return route , err
12531265}
12541266
1267+ // FlushPending materializes deferred resource registrations.
1268+ func (r * router ) FlushPending () { r .flushPending () }
1269+
12551270// GetRoutes returns every registered route.
12561271func (r * router ) GetRoutes () []* Route {
12571272 r .flushPending ()
@@ -1453,7 +1468,9 @@ func (r *router) RestoreCompiled(data []byte) error {
14531468 }
14541469
14551470 r .collection = NewRouteCollection ()
1471+ r .currentMu .Lock ()
14561472 r .currentRoute = nil
1473+ r .currentMu .Unlock ()
14571474
14581475 for _ , cr := range out {
14591476 if _ , ok := r .controllers [cr .Action [:strings .Index (cr .Action , "@" )]]; ! ok {
0 commit comments