瀏覽代碼

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

Manu Mtz-Almeida 11 年之前
父節點
當前提交
d9573b45c7
共有 1 個文件被更改,包括 24 次插入3 次删除
  1. 24 3
      gin.go

+ 24 - 3
gin.go

@@ -45,6 +45,7 @@ type (
 		Errors   ErrorMsgs
 		Params   httprouter.Params
 		Engine   *Engine
+		keep     bool
 		handlers []HandlerFunc
 		index    int8
 	}
@@ -180,6 +181,7 @@ func (engine *Engine) createContext(w http.ResponseWriter, req *http.Request, pa
 			Writer:   w,
 			Req:      req,
 			Params:   params,
+			keep:     false,
 			handlers: handlers,
 			index:    -1,
 			Engine:   engine,
@@ -188,9 +190,11 @@ func (engine *Engine) createContext(w http.ResponseWriter, req *http.Request, pa
 }
 
 func (engine *Engine) reuseContext(c *Context) {
-	select {
-	case engine.cache <- c:
-	default:
+	if c.keep == false {
+		select {
+		case engine.cache <- c:
+		default:
+		}
 	}
 }
 
@@ -268,6 +272,23 @@ func (group *RouterGroup) combineHandlers(handlers []HandlerFunc) []HandlerFunc
 /****** 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.
 // It executes the pending handlers in the chain inside the calling handler.
 // See example in github.