1 Star 2 Fork 0

api-go / iris

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
pool.go 1.47 KB
一键复制 编辑 原始数据 按行查看 历史
kataras 提交于 2017-11-09 12:03 . Update to version 8.5.8 | Read HISTORY.md
package context
import (
"net/http"
"sync"
)
// Pool is the context pool, it's used inside router and the framework by itself.
//
// It's the only one real implementation inside this package because it used widely.
type Pool struct {
pool *sync.Pool
newFunc func() Context // we need a field otherwise is not working if we change the return value
}
// New creates and returns a new context pool.
func New(newFunc func() Context) *Pool {
c := &Pool{pool: &sync.Pool{}, newFunc: newFunc}
c.pool.New = func() interface{} { return c.newFunc() }
return c
}
// Attach changes the pool's return value Context.
//
// The new Context should explicitly define the `Next()`
// and `Do(context.Handlers)` functions.
//
// Example: https://github.com/kataras/iris/blob/master/_examples/routing/custom-context/method-overriding/main.go
func (c *Pool) Attach(newFunc func() Context) {
c.newFunc = newFunc
}
// Acquire returns a Context from pool.
// See Release.
func (c *Pool) Acquire(w http.ResponseWriter, r *http.Request) Context {
ctx := c.pool.Get().(Context)
ctx.BeginRequest(w, r)
return ctx
}
// Release puts a Context back to its pull, this function releases its resources.
// See Acquire.
func (c *Pool) Release(ctx Context) {
ctx.EndRequest()
c.pool.Put(ctx)
}
// ReleaseLight will just release the object back to the pool, but the
// clean method is caller's responsibility now, currently this is only used
// on `SPABuilder`.
func (c *Pool) ReleaseLight(ctx Context) {
c.pool.Put(ctx)
}
1
https://gitee.com/netscript/iris.git
git@gitee.com:netscript/iris.git
netscript
iris
iris
v11.1.1

搜索帮助