forked from mirror/gin
Refactors Context allocation
This commit is contained in:
parent
aa9fad5ad8
commit
1e417c7a50
12
context.go
12
context.go
|
@ -79,14 +79,11 @@ type Context struct {
|
||||||
|
|
||||||
func (engine *Engine) createContext(w http.ResponseWriter, req *http.Request, params httprouter.Params, handlers []HandlerFunc) *Context {
|
func (engine *Engine) createContext(w http.ResponseWriter, req *http.Request, params httprouter.Params, handlers []HandlerFunc) *Context {
|
||||||
c := engine.pool.Get().(*Context)
|
c := engine.pool.Get().(*Context)
|
||||||
|
c.reset()
|
||||||
c.writermem.reset(w)
|
c.writermem.reset(w)
|
||||||
c.Request = req
|
c.Request = req
|
||||||
c.Params = params
|
c.Params = params
|
||||||
c.handlers = handlers
|
c.handlers = handlers
|
||||||
c.Keys = nil
|
|
||||||
c.index = -1
|
|
||||||
c.accepted = nil
|
|
||||||
c.Errors = c.Errors[0:0]
|
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,6 +91,13 @@ func (engine *Engine) reuseContext(c *Context) {
|
||||||
engine.pool.Put(c)
|
engine.pool.Put(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Context) reset() {
|
||||||
|
c.Keys = nil
|
||||||
|
c.index = -1
|
||||||
|
c.accepted = nil
|
||||||
|
c.Errors = c.Errors[0:0]
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Context) Copy() *Context {
|
func (c *Context) Copy() *Context {
|
||||||
var cp Context = *c
|
var cp Context = *c
|
||||||
cp.index = AbortIndex
|
cp.index = AbortIndex
|
||||||
|
|
10
gin.go
10
gin.go
|
@ -56,9 +56,7 @@ func New() *Engine {
|
||||||
engine.router.NotFound = engine.handle404
|
engine.router.NotFound = engine.handle404
|
||||||
engine.router.MethodNotAllowed = engine.handle405
|
engine.router.MethodNotAllowed = engine.handle405
|
||||||
engine.pool.New = func() interface{} {
|
engine.pool.New = func() interface{} {
|
||||||
c := &Context{Engine: engine}
|
return engine.allocateContext()
|
||||||
c.Writer = &c.writermem
|
|
||||||
return c
|
|
||||||
}
|
}
|
||||||
return engine
|
return engine
|
||||||
}
|
}
|
||||||
|
@ -70,6 +68,12 @@ func Default() *Engine {
|
||||||
return engine
|
return engine
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (engine *Engine) allocateContext() (c *Context) {
|
||||||
|
c = &Context{Engine: engine}
|
||||||
|
c.Writer = &c.writermem
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func (engine *Engine) LoadHTMLGlob(pattern string) {
|
func (engine *Engine) LoadHTMLGlob(pattern string) {
|
||||||
if IsDebugging() {
|
if IsDebugging() {
|
||||||
render.HTMLDebug.AddGlob(pattern)
|
render.HTMLDebug.AddGlob(pattern)
|
||||||
|
|
Loading…
Reference in New Issue