5
5
"fmt"
6
6
"net/http"
7
7
"strings"
8
- "sync"
9
8
)
10
9
11
10
var _ Router = & Mux {}
@@ -33,9 +32,6 @@ type Mux struct {
33
32
// to a parent mux
34
33
parent * Mux
35
34
36
- // Routing context pool
37
- pool * sync.Pool
38
-
39
35
// Custom route not found handler
40
36
notFoundHandler http.HandlerFunc
41
37
@@ -50,10 +46,7 @@ type Mux struct {
50
46
// NewMux returns a newly initialized Mux object that implements the Router
51
47
// interface.
52
48
func NewMux () * Mux {
53
- mux := & Mux {tree : & node {}, pool : & sync.Pool {}}
54
- mux .pool .New = func () interface {} {
55
- return NewRouteContext ()
56
- }
49
+ mux := & Mux {tree : & node {}}
57
50
return mux
58
51
}
59
52
@@ -78,17 +71,14 @@ func (mx *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
78
71
// mx.handler that is comprised of mx.middlewares + mx.routeHTTP.
79
72
// Once the request is finished, reset the routing context and put it back
80
73
// into the pool for reuse from another request.
81
- rctx = mx .pool .Get ().(* Context )
82
- rctx .Reset ()
83
- rctx .Routes = mx
84
- rctx .parentCtx = r .Context ()
74
+ rctx = GetRouteContext (mx )
75
+ defer PutRouteContext (rctx )
85
76
86
77
// NOTE: r.WithContext() causes 2 allocations and context.WithValue() causes 1 allocation
87
78
r = r .WithContext (context .WithValue (r .Context (), RouteCtxKey , rctx ))
88
79
89
80
// Serve the request and once its done, put the request context back in the sync pool
90
81
mx .handler .ServeHTTP (w , r )
91
- mx .pool .Put (rctx )
92
82
}
93
83
94
84
// Use appends a middleware handler to the Mux middleware stack.
@@ -255,7 +245,7 @@ func (mx *Mux) With(middlewares ...func(http.Handler) http.Handler) Router {
255
245
mws = append (mws , middlewares ... )
256
246
257
247
im := & Mux {
258
- pool : mx . pool , inline : true , parent : mx , tree : mx .tree , middlewares : mws ,
248
+ inline : true , parent : mx , tree : mx .tree , middlewares : mws ,
259
249
notFoundHandler : mx .notFoundHandler , methodNotAllowedHandler : mx .methodNotAllowedHandler ,
260
250
}
261
251
0 commit comments