-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctx_pool.go
44 lines (36 loc) · 828 Bytes
/
ctx_pool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package decoder
import "sync"
// CtxPool represents context pool.
type CtxPool struct {
p sync.Pool
}
var (
// CP is a default instance of context pool.
// You may use it directly as decoder.CP.Get()/Put() or using functions AcquireCtx()/ReleaseCtx().
CP CtxPool
// Suppress go vet warning.
_, _ = AcquireCtx, ReleaseCtx
)
// Get context object from the pool or make new object if pool is empty.
func (p *CtxPool) Get() *Ctx {
v := p.p.Get()
if v != nil {
if c, ok := v.(*Ctx); ok {
return c
}
}
return NewCtx()
}
// Put the object to the pool.
func (p *CtxPool) Put(ctx *Ctx) {
ctx.Reset()
p.p.Put(ctx)
}
// AcquireCtx returns object from the default context pool.
func AcquireCtx() *Ctx {
return CP.Get()
}
// ReleaseCtx puts object back to default pool.
func ReleaseCtx(ctx *Ctx) {
CP.Put(ctx)
}