Refactors Context initialization

This commit is contained in:
Manu Mtz-Almeida
2015-03-31 17:39:30 +02:00
parent df3ed787e1
commit 4a37b0808b
3 changed files with 34 additions and 38 deletions
+10 -21
View File
@@ -22,36 +22,25 @@ const AbortIndex = math.MaxInt8 / 2
// Context is the most important part of gin. It allows us to pass variables between middleware,
// manage the flow, validate the JSON of a request and render a JSON response for example.
type Context struct {
Engine *Engine
writermem responseWriter
Request *http.Request
Writer ResponseWriter
Keys map[string]interface{}
Errors errorMsgs
Params httprouter.Params
Engine *Engine
handlers []HandlerFunc
index int8
accepted []string
Params httprouter.Params
Input inputHolder
handlers []HandlerFunc
index int8
Keys map[string]interface{}
Errors errorMsgs
accepted []string
}
/************************************/
/********** CONTEXT CREATION ********/
/************************************/
func (engine *Engine) createContext(w http.ResponseWriter, req *http.Request, params httprouter.Params, handlers []HandlerFunc) *Context {
c := engine.pool.Get().(*Context)
c.reset()
c.writermem.reset(w)
c.Request = req
c.Params = params
c.handlers = handlers
return c
}
func (engine *Engine) reuseContext(c *Context) {
engine.pool.Put(c)
}
func (c *Context) reset() {
c.Keys = nil
c.index = -1