/
token_bucket.go
374 lines (299 loc) · 10.1 KB
/
token_bucket.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package core
import (
"errors"
"fmt"
"sync"
"sync/atomic"
"time"
"github.com/inoxlang/inox/internal/utils"
)
const (
TOKEN_BUCKET_MANAGEMENT_TICK_INTERVAL = time.Millisecond
TOKEN_BUCKET_CAPACITY_SCALE = int64(time.Second / TOKEN_BUCKET_MANAGEMENT_TICK_INTERVAL)
MAX_WAIT_CHAN_COUNT = 1000
)
var (
ErrDestroyedTokenBucket = errors.New("token bucket is destroyed")
//token bucket management
tokenBucketManagerStarted atomic.Bool
tokenBuckets = map[*tokenBucket]struct{}{}
tokenBucketsLock sync.Mutex
waitChanPool = make(chan (chan (struct{})), MAX_WAIT_CHAN_COUNT)
)
func init() {
startTokenBucketManagerGoroutine()
}
// A tokenBucket represents a thread-safe token bucket, a single goroutine manages the buckets (see startTokenBucketManagerGoroutine).
// The most important methods are Take(count) and GiveBack(count). Take may wait for the token to refill if there are not enough tokens.
type tokenBucket struct {
lastDecrementTime time.Time
lock *sync.Mutex
capacity ScaledTokenCount
available ScaledTokenCount
increment ScaledTokenCount
depletingStateCount atomic.Int32
decrementFn TokenDepletionFn
context *Context
chanListLock sync.Mutex
waitChans []chan (struct{})
neededTokenCountList []ScaledTokenCount
useTokenCountList []ScaledTokenCount
cancelContextOnNegativeCount bool
shouldBeDestroyed bool
destroyed bool
}
// Token count scaled by TOKEN_BUCKET_CAPACITY_SCALE.
type ScaledTokenCount int64
func (c ScaledTokenCount) RealCount() int64 {
return int64(c) / TOKEN_BUCKET_CAPACITY_SCALE
}
type TokenDepletionFn func(lastDecrementTime time.Time, beingDepletedStateCount int32) int64
type tokenBucketConfig struct {
cap int64
initialAvail int64
fillRate int64 //tokens per second
depleteFn TokenDepletionFn
cancelContextOnNegativeCount bool
}
// newBucket returns a new token bucket with the specified fillrate & capacity, the bucket is created full.
func newBucket(config tokenBucketConfig) *tokenBucket {
if config.cap < 0 {
panic(fmt.Sprintf("token bucket: capacity %v should be > 0", config.cap))
}
avail := config.initialAvail
if avail < 0 {
avail = config.cap
}
tb := &tokenBucket{
lock: &sync.Mutex{},
capacity: ScaledTokenCount(config.cap * TOKEN_BUCKET_CAPACITY_SCALE),
available: ScaledTokenCount(avail * TOKEN_BUCKET_CAPACITY_SCALE),
increment: ScaledTokenCount(config.fillRate),
decrementFn: config.depleteFn,
cancelContextOnNegativeCount: config.cancelContextOnNegativeCount,
lastDecrementTime: time.Now(),
}
tokenBucketsLock.Lock()
tokenBuckets[tb] = struct{}{}
tokenBucketsLock.Unlock()
return tb
}
func (tb *tokenBucket) SetContext(ctx *Context) {
tb.lock.Lock()
defer tb.lock.Unlock()
tb.context = ctx
}
func (tb *tokenBucket) Capacity() int64 {
return tb.capacity.RealCount()
}
func (tb *tokenBucket) Available() int64 {
tb.lock.Lock()
defer tb.lock.Unlock()
return tb.available.RealCount()
}
func (tb *tokenBucket) assertNotDestroyedNoLock() {
if tb.destroyed || tb.shouldBeDestroyed {
panic(ErrDestroyedTokenBucket)
}
}
// TryTake trys to task specified count tokens from the bucket. if there are
// not enough tokens in the bucket, it will return false.
func (tb *tokenBucket) TryTake(count int64) bool {
scaledCount := ScaledTokenCount(count * TOKEN_BUCKET_CAPACITY_SCALE)
return tb.tryTake(scaledCount, scaledCount)
}
// Take tasks specified count tokens from the bucket, if there are
// not enough tokens in the bucket, it will keep waiting until count tokens are
// available and then take them.
func (tb *tokenBucket) Take(count int64) {
tb.waitAndTake(count, count)
}
func (tb *tokenBucket) GiveBack(count int64) {
tb.lock.Lock()
defer tb.lock.Unlock()
tb.assertNotDestroyedNoLock()
tb.available += ScaledTokenCount(count * TOKEN_BUCKET_CAPACITY_SCALE)
tb.available = min(tb.capacity, tb.available)
}
func (tb *tokenBucket) PauseOneStateDepletion() {
tb.depletingStateCount.Add(-1)
}
func (tb *tokenBucket) ResumeOneStateDepletion() {
tb.depletingStateCount.Add(1)
}
// TakeMaxDuration tasks specified count tokens from the bucket, if there are
// not enough tokens in the bucket, it will keep waiting until count tokens are
// available and then take them or just return false when max time has been spent waiting.
func (tb *tokenBucket) TakeMaxDuration(count int64, max time.Duration) bool {
return tb.waitAndTakeMaxDuration(count, count, max)
}
// Wait will keep waiting until count tokens are available in the bucket.
func (tb *tokenBucket) Wait(count int64) {
tb.waitAndTake(count, 0)
}
// WaitMaxDuration will keep waiting until count tokens are available in the
// bucket or just return false when max time has been spent waiting.
func (tb *tokenBucket) WaitMaxDuration(count int64, max time.Duration) bool {
return tb.waitAndTakeMaxDuration(count, 0, max)
}
func (tb *tokenBucket) tryTake(need, use ScaledTokenCount) bool {
tb.checkCount(need)
tb.lock.Lock()
defer tb.lock.Unlock()
tb.assertNotDestroyedNoLock()
if need <= tb.available {
tb.available -= use
return true
}
return false
}
func (tb *tokenBucket) addWaitChannel(need, use ScaledTokenCount) chan (struct{}) {
var channel chan (struct{})
select {
case chanFromPool := <-waitChanPool:
channel = chanFromPool
default:
channel = make(chan struct{}, 1)
}
tb.chanListLock.Lock()
tb.waitChans = append(tb.waitChans, channel)
tb.neededTokenCountList = append(tb.neededTokenCountList, need)
tb.useTokenCountList = append(tb.useTokenCountList, use)
tb.chanListLock.Unlock()
return channel
}
func (tb *tokenBucket) waitAndTake(need, use int64) {
needCount := ScaledTokenCount(need * TOKEN_BUCKET_CAPACITY_SCALE)
useCount := ScaledTokenCount(use * TOKEN_BUCKET_CAPACITY_SCALE)
if ok := tb.tryTake(needCount, useCount); ok {
return
}
waitChan := tb.addWaitChannel(needCount, useCount)
<-waitChan
}
func (tb *tokenBucket) waitAndTakeMaxDuration(need, use int64, max time.Duration) bool {
needCount := ScaledTokenCount(need * TOKEN_BUCKET_CAPACITY_SCALE)
useCount := ScaledTokenCount(use * TOKEN_BUCKET_CAPACITY_SCALE)
if ok := tb.tryTake(needCount, useCount); ok {
return true
}
waitChan := tb.addWaitChannel(needCount, useCount)
select {
case <-waitChan:
return true
case <-time.After(max):
return false
}
}
func (tb *tokenBucket) Destroy() {
tb.lock.Lock()
defer tb.lock.Unlock()
tb.shouldBeDestroyed = true
}
func (tb *tokenBucket) checkCount(count ScaledTokenCount) {
if count < 0 || count > tb.capacity {
panic(fmt.Sprintf("token-bucket: count %v should be less than bucket's"+
" capacity %v", count, tb.capacity))
}
}
// startTokenBucketManagerGoroutine starts a goroutine that manages all token buckets.
// The goroutine periodically iterates over tokenBuckets and performs several operations for each bucket:
// - add tokens in the bucket if .decrementFn field is nil.
// - cancel the attached context if there are not tokens left and cancelContextOnNegativeCount field is set to true.
// - for each goroutine waiting for the bucket to refill: if the needed token count > available then remove the tokens and resume the goroutine.
// - remove destroyed buckets from tokenBuckets.
func startTokenBucketManagerGoroutine() {
if !tokenBucketManagerStarted.CompareAndSwap(false, true) {
return
}
updateBucketState := func(tb *tokenBucket) {
tb.lock.Lock()
defer tb.lock.Unlock()
if tb.context != nil && tb.context.IsDone() {
tb.shouldBeDestroyed = true
}
if tb.decrementFn == nil {
if tb.available < tb.capacity {
increment := tb.increment
tb.available = tb.available + increment
}
} else if count := tb.depletingStateCount.Load(); count > 0 {
tb.available -= ScaledTokenCount(tb.decrementFn(tb.lastDecrementTime, count) * TOKEN_BUCKET_CAPACITY_SCALE)
}
if tb.available < 0 && tb.cancelContextOnNegativeCount && tb.context != nil {
tb.shouldBeDestroyed = true
if !tb.context.IsDoneSlowCheck() {
go func() {
defer utils.Recover()
tb.context.CancelGracefully() // TODO: add reason
}()
}
}
tb.available = max(0, tb.available)
tb.lastDecrementTime = time.Now() //updated even if tb.decrementFn is not called
func() {
tb.chanListLock.Lock()
defer tb.chanListLock.Unlock()
if tb.shouldBeDestroyed {
tb.shouldBeDestroyed = false
tb.destroyed = true
delete(tokenBuckets, tb)
// resume all waiting goroutines
// TODO: make sure this could not be used to momentarily bypass the limits.
for _, waitChan := range tb.waitChans {
select {
case waitChan <- struct{}{}:
default:
}
// put back the wait channel in the pool if possible
select {
case waitChanPool <- waitChan:
default:
close(waitChan)
}
}
return
}
for len(tb.waitChans) >= 1 { // if at least one goroutine is waiting for the bucket to refill.
waitChan := tb.waitChans[len(tb.waitChans)-1]
neededCount := tb.neededTokenCountList[len(tb.waitChans)-1]
useCount := tb.useTokenCountList[len(tb.waitChans)-1]
//if there are enough tokens we remove the needed count and resume the goroutine.
if tb.available >= neededCount {
newLength := len(tb.waitChans) - 1
tb.waitChans = tb.waitChans[:newLength]
tb.neededTokenCountList = tb.neededTokenCountList[:newLength]
tb.useTokenCountList = tb.useTokenCountList[:newLength]
tb.available -= useCount
//resume the waiting goroutine
select {
case waitChan <- struct{}{}:
default:
}
// put back the wait channel in the pool if possible
select {
case waitChanPool <- waitChan:
default:
close(waitChan)
}
} else {
//not enough tokens.
break
}
}
}()
}
go func() {
ticks := time.Tick(TOKEN_BUCKET_MANAGEMENT_TICK_INTERVAL)
for range ticks {
func() {
tokenBucketsLock.Lock()
defer utils.Recover()
defer tokenBucketsLock.Unlock()
for bucket := range tokenBuckets {
updateBucketState(bucket)
}
}()
}
}()
}