Adds Keep() and Release() to gin.Context

This commit is contained in:
Manu Mtz-Almeida 2014-07-03 15:59:39 +02:00
parent 30ea9c06fc
commit d9573b45c7

27
gin.go
View File

@ -45,6 +45,7 @@ type (
Errors ErrorMsgs Errors ErrorMsgs
Params httprouter.Params Params httprouter.Params
Engine *Engine Engine *Engine
keep bool
handlers []HandlerFunc handlers []HandlerFunc
index int8 index int8
} }
@ -180,6 +181,7 @@ func (engine *Engine) createContext(w http.ResponseWriter, req *http.Request, pa
Writer: w, Writer: w,
Req: req, Req: req,
Params: params, Params: params,
keep: false,
handlers: handlers, handlers: handlers,
index: -1, index: -1,
Engine: engine, Engine: engine,
@ -188,9 +190,11 @@ func (engine *Engine) createContext(w http.ResponseWriter, req *http.Request, pa
} }
func (engine *Engine) reuseContext(c *Context) { func (engine *Engine) reuseContext(c *Context) {
select { if c.keep == false {
case engine.cache <- c: select {
default: case engine.cache <- c:
default:
}
} }
} }
@ -268,6 +272,23 @@ func (group *RouterGroup) combineHandlers(handlers []HandlerFunc) []HandlerFunc
/****** FLOW AND ERROR MANAGEMENT****/ /****** FLOW AND ERROR MANAGEMENT****/
/************************************/ /************************************/
func (c *Context) Keep() {
if c.keep == false {
c.keep = true
} else {
log.Println("gin: trying to Keep same context several times")
}
}
func (c *Context) Release() {
if c.keep == true {
c.keep = false
c.Engine.reuseContext(c)
} else {
log.Println("gin: bug: trying to Release same context several times")
}
}
// Next should be used only in the middlewares. // Next should be used only in the middlewares.
// It executes the pending handlers in the chain inside the calling handler. // It executes the pending handlers in the chain inside the calling handler.
// See example in github. // See example in github.