-
Notifications
You must be signed in to change notification settings - Fork 1
/
field_array_pool.gen.go
207 lines (167 loc) · 4.79 KB
/
field_array_pool.gen.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// This file was automatically generated by genny.
// Any changes will be lost if this file is regenerated.
// see https://github.com/mauricelam/genny
package field
import (
"errors"
"math"
"sync/atomic"
"github.com/m3db/m3/src/x/instrument"
"github.com/uber-go/tally"
)
// FieldArrayPoolOptions provide a set of options for the value pool.
type FieldArrayPoolOptions struct {
instrumentOpts instrument.Options
size int
refillLowWatermark float64
refillHighWatermark float64
}
// NewFieldArrayPoolOptions create a new set of value pool options.
func NewFieldArrayPoolOptions() *FieldArrayPoolOptions {
return &FieldArrayPoolOptions{
instrumentOpts: instrument.NewOptions(),
size: 4096,
}
}
// SetInstrumentOptions sets the instrument options.
func (o *FieldArrayPoolOptions) SetInstrumentOptions(v instrument.Options) *FieldArrayPoolOptions {
opts := *o
opts.instrumentOpts = v
return &opts
}
// InstrumentOptions returns the instrument options.
func (o *FieldArrayPoolOptions) InstrumentOptions() instrument.Options {
return o.instrumentOpts
}
// SetSize sets the pool size.
func (o *FieldArrayPoolOptions) SetSize(v int) *FieldArrayPoolOptions {
opts := *o
opts.size = v
return &opts
}
// Size returns pool size.
func (o *FieldArrayPoolOptions) Size() int { return o.size }
// SetRefillLowWatermark sets the low watermark for refilling the pool.
func (o *FieldArrayPoolOptions) SetRefillLowWatermark(v float64) *FieldArrayPoolOptions {
opts := *o
opts.refillLowWatermark = v
return &opts
}
// RefillLowWatermark returns the low watermark for refilling the pool.
func (o *FieldArrayPoolOptions) RefillLowWatermark() float64 { return o.refillLowWatermark }
// SetRefillHighWatermark sets the high watermark for refilling the pool.
func (o *FieldArrayPoolOptions) SetRefillHighWatermark(v float64) *FieldArrayPoolOptions {
opts := *o
opts.refillHighWatermark = v
return &opts
}
// RefillHighWatermark returns the high watermark for stop refilling the pool.
func (o *FieldArrayPoolOptions) RefillHighWatermark() float64 { return o.refillHighWatermark }
type fieldArrayPoolMetrics struct {
free tally.Gauge
total tally.Gauge
getOnEmpty tally.Counter
putOnFull tally.Counter
}
func newfieldArrayPoolMetrics(m tally.Scope) fieldArrayPoolMetrics {
return fieldArrayPoolMetrics{
free: m.Gauge("free"),
total: m.Gauge("total"),
getOnEmpty: m.Counter("get-on-empty"),
putOnFull: m.Counter("put-on-full"),
}
}
// FieldArrayPool is a value pool.
type FieldArrayPool struct {
values chan []Field
alloc func() []Field
size int
refillLowWatermark int
refillHighWatermark int
filling int32
initialized int32
dice int32
metrics fieldArrayPoolMetrics
}
// NewFieldArrayPool creates a new pool.
func NewFieldArrayPool(opts *FieldArrayPoolOptions) *FieldArrayPool {
if opts == nil {
opts = NewFieldArrayPoolOptions()
}
p := &FieldArrayPool{
values: make(chan []Field, opts.Size()),
size: opts.Size(),
refillLowWatermark: int(math.Ceil(
opts.RefillLowWatermark() * float64(opts.Size()))),
refillHighWatermark: int(math.Ceil(
opts.RefillHighWatermark() * float64(opts.Size()))),
metrics: newfieldArrayPoolMetrics(opts.InstrumentOptions().MetricsScope()),
}
p.setGauges()
return p
}
// Init initializes the pool.
func (p *FieldArrayPool) Init(alloc func() []Field) {
if !atomic.CompareAndSwapInt32(&p.initialized, 0, 1) {
panic(errors.New("pool is already initialized"))
}
p.alloc = alloc
for i := 0; i < cap(p.values); i++ {
p.values <- p.alloc()
}
p.setGauges()
}
// Get gets a value from the pool.
func (p *FieldArrayPool) Get() []Field {
if atomic.LoadInt32(&p.initialized) != 1 {
panic(errors.New("get before pool is initialized"))
}
var v []Field
select {
case v = <-p.values:
default:
v = p.alloc()
p.metrics.getOnEmpty.Inc(1)
}
p.trySetGauges()
if p.refillLowWatermark > 0 && len(p.values) <= p.refillLowWatermark {
p.tryFill()
}
return v
}
// Put returns a value to pool.
func (p *FieldArrayPool) Put(v []Field) {
if atomic.LoadInt32(&p.initialized) != 1 {
panic(errors.New("put before pool is initialized"))
}
select {
case p.values <- v:
default:
p.metrics.putOnFull.Inc(1)
}
p.trySetGauges()
}
func (p *FieldArrayPool) trySetGauges() {
if atomic.AddInt32(&p.dice, 1)%100 == 0 {
p.setGauges()
}
}
func (p *FieldArrayPool) setGauges() {
p.metrics.free.Update(float64(len(p.values)))
p.metrics.total.Update(float64(p.size))
}
func (p *FieldArrayPool) tryFill() {
if !atomic.CompareAndSwapInt32(&p.filling, 0, 1) {
return
}
go func() {
defer atomic.StoreInt32(&p.filling, 0)
for len(p.values) < p.refillHighWatermark {
select {
case p.values <- p.alloc():
default:
return
}
}
}()
}