-
Notifications
You must be signed in to change notification settings - Fork 0
/
round_robin_resource_pool.go
241 lines (192 loc) · 5.07 KB
/
round_robin_resource_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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package resource_pool
import (
"sync"
"sync/atomic"
"github.com/dropbox/godropbox/errors"
"github.com/dropbox/godropbox/math2/rand2"
)
type ResourceLocationPool struct {
ResourceLocation string
Pool ResourcePool
}
func shuffle(pools []*ResourceLocationPool) {
for i := len(pools) - 1; i > 0; i-- {
idx := rand2.Intn(i + 1)
pools[i], pools[idx] = pools[idx], pools[i]
}
}
// A resource pool implementation which returns handles from the registered
// resource locations in a round robin fashion.
type roundRobinResourcePool struct {
options Options
createPool func(Options) ResourcePool
rwMutex sync.RWMutex
isLameDuck bool
pools []*ResourceLocationPool
counter *int64 // atomic counter
}
// This returns a RoundRobinResourcePool.
func NewRoundRobinResourcePool(
options Options,
createPool func(Options) ResourcePool,
pools ...*ResourceLocationPool) (ResourcePool, error) {
locations := make(map[string]bool)
for _, pool := range pools {
if pool.ResourceLocation == "" {
return nil, errors.New("Invalid resource location")
}
if locations[pool.ResourceLocation] {
return nil, errors.Newf(
"Duplication resource location %s",
pool.ResourceLocation)
}
locations[pool.ResourceLocation] = true
if pool.Pool == nil {
return nil, errors.New("Invalid pool")
}
}
if createPool == nil {
createPool = NewSimpleResourcePool
}
counter := new(int64)
atomic.StoreInt64(counter, 0)
shuffle(pools)
return &roundRobinResourcePool{
options: options,
createPool: createPool,
rwMutex: sync.RWMutex{},
isLameDuck: false,
pools: pools,
counter: counter,
}, nil
}
// See ResourcePool for documentation.
func (p *roundRobinResourcePool) NumActive() int32 {
total := int32(0)
p.rwMutex.RLock()
defer p.rwMutex.RUnlock()
for _, locPool := range p.pools {
total += locPool.Pool.NumActive()
}
return total
}
// See ResourcePool for documentation.
func (p *roundRobinResourcePool) ActiveHighWaterMark() int32 {
high := int32(0)
p.rwMutex.RLock()
defer p.rwMutex.RUnlock()
for _, locPool := range p.pools {
val := locPool.Pool.ActiveHighWaterMark()
if val > high {
high = val
}
}
return high
}
// See ResourcePool for documentation.
func (p *roundRobinResourcePool) NumIdle() int {
total := 0
p.rwMutex.RLock()
defer p.rwMutex.RUnlock()
for _, locPool := range p.pools {
total += locPool.Pool.NumIdle()
}
return total
}
// See ResourcePool for documentation.
func (p *roundRobinResourcePool) Register(resourceLocation string) error {
if resourceLocation == "" {
return errors.New("Registering invalid resource location")
}
p.rwMutex.Lock()
defer p.rwMutex.Unlock()
if p.isLameDuck {
return errors.Newf(
"Cannot register %s to lame duck resource pool",
resourceLocation)
}
for _, locPool := range p.pools {
if locPool.ResourceLocation == resourceLocation {
return nil
}
}
pool := p.createPool(p.options)
if err := pool.Register(resourceLocation); err != nil {
return err
}
p.pools = append(
p.pools,
&ResourceLocationPool{
ResourceLocation: resourceLocation,
Pool: pool,
})
shuffle(p.pools)
return nil
}
// See ResourcePool for documentation.
func (p *roundRobinResourcePool) Unregister(resourceLocation string) error {
p.rwMutex.Lock()
defer p.rwMutex.Unlock()
idx := -1
for i, locPool := range p.pools {
if locPool.ResourceLocation == resourceLocation {
idx = i
break
}
}
if idx >= 0 {
_ = p.pools[idx].Pool.Unregister(p.pools[idx].ResourceLocation)
p.pools[idx].Pool.EnterLameDuckMode()
tail := p.pools[idx+1:]
p.pools = p.pools[0:idx]
p.pools = append(p.pools, tail...)
shuffle(p.pools)
}
return nil
}
func (p *roundRobinResourcePool) ListRegistered() []string {
p.rwMutex.RLock()
defer p.rwMutex.RUnlock()
result := make([]string, 0, len(p.pools))
for _, locPool := range p.pools {
result = append(result, locPool.ResourceLocation)
}
return result
}
// See ResourcePool for documentation.
func (p *roundRobinResourcePool) Get(key string) (ManagedHandle, error) {
p.rwMutex.RLock()
defer p.rwMutex.RUnlock()
var err error
var handle ManagedHandle
for i := 0; i < len(p.pools); i++ {
next := int(atomic.AddInt64(p.counter, 1) % int64(len(p.pools)))
pool := p.pools[next].Pool
handle, err = pool.Get(key)
if err == nil {
return handle, nil
}
}
return nil, errors.Wrap(err, "No resource handle available")
}
// See ResourcePool for documentation.
func (p *roundRobinResourcePool) Release(handle ManagedHandle) error {
// NOTE: check if the handle belongs to this pool is expensive, so we'll
// just skip the check.
return handle.Release()
}
// See ResourcePool for documentation.
func (p *roundRobinResourcePool) Discard(handle ManagedHandle) error {
// NOTE: check if the handle belongs to this pool is expensive, so we'll
// just skip the check.
return handle.Discard()
}
// See ResourcePool for documentation.
func (p *roundRobinResourcePool) EnterLameDuckMode() {
p.rwMutex.RLock()
defer p.rwMutex.RUnlock()
p.isLameDuck = true
for _, locPool := range p.pools {
locPool.Pool.EnterLameDuckMode()
}
}