Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync route tree to httprouter latest code #2368

Merged
merged 15 commits into from
May 10, 2020
2 changes: 2 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type Context struct {
fullPath string

engine *Engine
params *Params

// This mutex protect Keys map
mu sync.RWMutex
Expand Down Expand Up @@ -95,6 +96,7 @@ func (c *Context) reset() {
c.Accepted = nil
c.queryCache = nil
c.formCache = nil
*c.params = (*c.params)[0:0]
}

// Copy returns a copy of the current context that can be safely used outside the request's scope.
Expand Down
16 changes: 13 additions & 3 deletions gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ type Engine struct {
noMethod HandlersChain
pool sync.Pool
trees methodTrees
maxParams uint16
}

var _ IRouter = &Engine{}
Expand Down Expand Up @@ -163,7 +164,8 @@ func Default() *Engine {
}

func (engine *Engine) allocateContext() *Context {
return &Context{engine: engine}
v := make(Params, 0, engine.maxParams)
return &Context{engine: engine, params: &v}
}

// Delims sets template left and right delims and returns a Engine instance.
Expand Down Expand Up @@ -256,13 +258,19 @@ func (engine *Engine) addRoute(method, path string, handlers HandlersChain) {
assert1(len(handlers) > 0, "there must be at least one handler")

debugPrintRoute(method, path, handlers)

root := engine.trees.get(method)
if root == nil {
root = new(node)
root.fullPath = "/"
engine.trees = append(engine.trees, methodTree{method: method, root: root})
}
root.addRoute(path, handlers)

// Update maxParams
if paramsCount := countParams(path); paramsCount > engine.maxParams {
engine.maxParams = paramsCount
}
}

// Routes returns a slice of registered routes, including some useful information, such as:
Expand Down Expand Up @@ -402,10 +410,12 @@ func (engine *Engine) handleHTTPRequest(c *Context) {
}
root := t[i].root
// Find route in tree
value := root.getValue(rPath, c.Params, unescape)
value := root.getValue(rPath, c.params, unescape)
if value.params != nil {
c.Params = *value.params
}
if value.handlers != nil {
c.handlers = value.handlers
c.Params = value.params
c.fullPath = value.fullPath
c.Next()
c.writermem.WriteHeaderNow()
Expand Down
Loading