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
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
17 changes: 14 additions & 3 deletions gin.go
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,20 @@ func (engine *Engine) addRoute(method, path string, handlers HandlersChain) {
assert1(len(handlers) > 0, "there must be at least one handler")

debugPrintRoute(method, path, handlers)
varsCount := uint16(0)
Copy link
Member

@thinkerou thinkerou May 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this zero value local var is't reassign value.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thinkerou done.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thinkerou done.


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+varsCount > engine.maxParams {
engine.maxParams = paramsCount + varsCount
}
}

// Routes returns a slice of registered routes, including some useful information, such as:
Expand Down Expand Up @@ -402,10 +411,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