-
Notifications
You must be signed in to change notification settings - Fork 2
/
pool.go
39 lines (33 loc) · 881 Bytes
/
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
package ligno
import (
"bytes"
"sync"
)
// byteBufferPool is pool of byte buffers, used to avoid allocation for each
// formatting of log message in formatter.
type byteBufferPool struct {
sync.Pool
}
// newByteBufferPool creates new instance of byteBufferPool initialized
// to creates new buffer instances or initial size 128 bytes.
func newByteBufferPool() *byteBufferPool {
return &byteBufferPool{
Pool: sync.Pool{
New: func() interface{} {
return bytes.NewBuffer(make([]byte, 0, 128))
},
},
}
}
// Get returns fresh buffer from pool.
func (bp *byteBufferPool) Get() *bytes.Buffer {
buff := bp.Pool.Get().(*bytes.Buffer)
buff.Reset()
return buff
}
// Put returns buffer to pool, for later use.
func (bp *byteBufferPool) Put(buff *bytes.Buffer) {
bp.Pool.Put(buff)
}
// buffPool is single instance of buffer pool.
var buffPool = newByteBufferPool()