-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool.go
54 lines (47 loc) · 1.09 KB
/
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
45
46
47
48
49
50
51
52
53
54
package logger
import (
"sync"
"go.uber.org/zap"
)
var fieldPool = &sync.Pool{
New: func() any {
return &fieldContainer{
make([]zap.Field, 0, 32),
}
},
}
type fieldContainer struct {
Fields []Field
}
func (c *fieldContainer) reset() *fieldContainer {
c.Fields = c.Fields[:0]
return c
}
// PoolGet selects an arbitrary item from the field Pool, removes it from the
// field Pool, and returns it to the caller.
// PoolGet may choose to ignore the field pool and treat it as empty.
// Callers should not assume any relation between values passed to PoolPut and
// the values returned by PoolGet.
//
// NOTE: This function should be call PoolPut to give back.
// NOTE: You should know `sync.Pool` work principle
// ```go
//
// fc := logger.PoolGet()
// defer logger.PoolPut(fc)
// fc.Fields = append(fc.Fields, logger.String("k1", "v1"))
// ... use fc.Fields
//
// ```
func PoolGet() *fieldContainer {
c := fieldPool.Get().(*fieldContainer)
return c.reset()
}
// PoolPut adds x to the pool.
// NOTE: See PoolGet.
func PoolPut(c *fieldContainer) {
if c == nil {
return
}
fieldPool.Put(c.reset())
}