Skip to content

Commit

Permalink
Move the check & realloc logic closer to calling HandlerFunc
Browse files Browse the repository at this point in the history
  • Loading branch information
3DRX committed Jul 10, 2024
1 parent dbea427 commit 182aa48
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
8 changes: 4 additions & 4 deletions pkg/app/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -846,11 +846,11 @@ func (ctx *RequestContext) HandlerName() string {
return utils.NameOfFunction(ctx.handlers.Last())
}

func (ctx *RequestContext) GetParamsCount() int {
return ctx.paramsCount
}

func (ctx *RequestContext) ResetWithoutConn() {
// if ctx.Params is re-assigned by user in HandlerFunc and the capacity changed we need to realloc
if cap(ctx.Params) < ctx.paramsCount {
ctx.Params = make(param.Params, ctx.paramsCount)
}
ctx.Params = ctx.Params[0:0]
ctx.Errors = ctx.Errors[0:0]
ctx.handlers = nil
Expand Down
6 changes: 6 additions & 0 deletions pkg/route/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import (
"github.com/cloudwego/hertz/pkg/protocol/http1"
"github.com/cloudwego/hertz/pkg/protocol/http1/factory"
"github.com/cloudwego/hertz/pkg/protocol/suite"
"github.com/cloudwego/hertz/pkg/route/param"
)

const unknownTransporterName = "unknown"
Expand Down Expand Up @@ -749,6 +750,11 @@ func (engine *Engine) ServeHTTP(c context.Context, ctx *app.RequestContext) {
return
}

// if ctx.Params is re-assigned by user in HandlerFunc and the capacity changed we need to realloc
if cap(ctx.Params) < ctx.GetParamsCount() {
ctx.Params = make(param.Params, ctx.GetParamsCount())
}

// Find root of the tree for the given HTTP method
t := engine.trees
paramsPointer := &ctx.Params
Expand Down

0 comments on commit 182aa48

Please sign in to comment.