forked from thanos-io/thanos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool.go
57 lines (47 loc) · 1.13 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
55
56
57
package downsample
import (
"sync"
"github.com/prometheus/tsdb/chunkenc"
)
// Pool is a memory pool of chunk objects, supporting Thanos aggr chunk encoding.
// It maintains separate pools for xor and aggr chunks.
type pool struct {
wrapped chunkenc.Pool
aggr sync.Pool
}
// TODO(bplotka): Add reasonable limits to our sync pools them to detect OOMs early.
func NewPool() chunkenc.Pool {
return &pool{
wrapped: chunkenc.NewPool(),
aggr: sync.Pool{
New: func() interface{} {
return &AggrChunk{}
},
},
}
}
func (p *pool) Get(e chunkenc.Encoding, b []byte) (chunkenc.Chunk, error) {
switch e {
case ChunkEncAggr:
c := p.aggr.Get().(*AggrChunk)
*c = AggrChunk(b)
return c, nil
}
return p.wrapped.Get(e, b)
}
func (p *pool) Put(c chunkenc.Chunk) error {
switch c.Encoding() {
case ChunkEncAggr:
ac, ok := c.(*AggrChunk)
// This may happen often with wrapped chunks. Nothing we can really do about
// it but returning an error would cause a lot of allocations again. Thus,
// we just skip it.
if !ok {
return nil
}
// Clear []byte.
*ac = AggrChunk(nil)
p.aggr.Put(ac)
}
return p.wrapped.Put(c)
}