Skip to content

Commit

Permalink
Comments
Browse files Browse the repository at this point in the history
  • Loading branch information
pkieltyka committed Feb 20, 2020
1 parent baf4ef5 commit b612eb4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
11 changes: 7 additions & 4 deletions mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ func (mx *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
rctx = mx.pool.Get().(*Context)
rctx.Reset()
rctx.Routes = mx

// NOTE: r.WithContext() causes 2 allocations and context.WithValue() causes 1 allocation
r = r.WithContext(context.WithValue(r.Context(), RouteCtxKey, rctx))

// Serve the request and once its done, put the request context back in the sync pool
mx.handler.ServeHTTP(w, r)
mx.pool.Put(rctx)
}
Expand Down Expand Up @@ -220,7 +224,7 @@ func (mx *Mux) MethodNotAllowed(handlerFn http.HandlerFunc) {

// With adds inline middlewares for an endpoint handler.
func (mx *Mux) With(middlewares ...func(http.Handler) http.Handler) Router {
// Similarly as in handle(), we must build the mux handler once further
// Similarly as in handle(), we must build the mux handler once additional
// middleware registration isn't allowed for this stack, like now.
if !mx.inline && mx.handler == nil {
mx.buildRouteHandler()
Expand Down Expand Up @@ -288,7 +292,6 @@ func (mx *Mux) Mount(pattern string, handler http.Handler) {
subr.MethodNotAllowed(mx.methodNotAllowedHandler)
}

// Wrap the sub-router in a handlerFunc to scope the request path for routing.
mountHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rctx := RouteContext(r.Context())
rctx.RoutePath = mx.nextRoutePath(rctx)
Expand Down Expand Up @@ -379,7 +382,7 @@ func (mx *Mux) handle(method methodTyp, pattern string, handler http.Handler) *n
panic(fmt.Sprintf("chi: routing pattern must begin with '/' in '%s'", pattern))
}

// Build the final routing handler for this Mux.
// Build the computed routing handler for this routing pattern.
if !mx.inline && mx.handler == nil {
mx.buildRouteHandler()
}
Expand Down Expand Up @@ -439,7 +442,7 @@ func (mx *Mux) nextRoutePath(rctx *Context) string {
routePath := "/"
nx := len(rctx.routeParams.Keys) - 1 // index of last param in list
if nx >= 0 && rctx.routeParams.Keys[nx] == "*" && len(rctx.routeParams.Values) > nx {
routePath += rctx.routeParams.Values[nx]
routePath = "/" + rctx.routeParams.Values[nx]
}
return routePath
}
Expand Down
11 changes: 7 additions & 4 deletions mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1700,21 +1700,24 @@ func BenchmarkMux(b *testing.B) {
mx.Get("/hi", h2)
mx.Get("/sup/{id}/and/{this}", h3)

mx.Route("/sharing/{hash}", func(mx Router) {
mx.Route("/sharing/{x}/{hash}", func(mx Router) {
mx.Get("/", h4) // subrouter-1
mx.Get("/{network}", h5) // subrouter-1
mx.Get("/twitter", h5)
mx.Route("/direct", func(mx Router) {
mx.Get("/", h6) // subrouter-2
mx.Get("/download", h6)
})
})

routes := []string{
"/",
"/hi",
"/sup/123/and/this",
"/sharing/aBc", // subrouter-1
"/sharing/aBc/twitter", // subrouter-1
"/sharing/aBc/direct", // subrouter-2
"/sharing/z/aBc", // subrouter-1
"/sharing/z/aBc/twitter", // subrouter-1
"/sharing/z/aBc/direct", // subrouter-2
"/sharing/z/aBc/direct/download", // subrouter-2
}

for _, path := range routes {
Expand Down
9 changes: 0 additions & 9 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,15 +525,6 @@ func (n *node) findEdge(ntyp nodeTyp, label byte) *node {
}
}

func (n *node) isEmpty() bool {
for _, nds := range n.children {
if len(nds) > 0 {
return false
}
}
return true
}

func (n *node) isLeaf() bool {
return n.endpoints != nil
}
Expand Down

0 comments on commit b612eb4

Please sign in to comment.