forked from uber/cadence
/
tb.go
189 lines (167 loc) · 5.73 KB
/
tb.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
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package common
import (
"sync"
"time"
)
type (
// TokenBucketFactory is an interface mainly used for injecting mock implementation of TokenBucket for unit testing
TokenBucketFactory interface {
CreateTokenBucket(rps int, timeSource TimeSource) TokenBucket
}
// TokenBucket is the interface for
// any implememtation of a token bucket
// rate limiter
TokenBucket interface {
// TryConsume attempts to take count tokens from the
// bucket. Returns true on success, false
// otherwise along with the duration for the next refill
TryConsume(count int) (bool, time.Duration)
// Consume waits up to timeout duration to take count
// tokens from the bucket. Returns true if count
// tokens were acquired before timeout, false
// otherwise
Consume(count int, timeout time.Duration) bool
}
tokenBucketFactoryImpl struct{}
tokenBucketImpl struct {
sync.Mutex
tokens int
fillRate int // amount of tokens to add every interval
fillInterval int64 // time between refills
// Because we divide the per-second quota equally
// every 100 millis, there could be a remainder when
// the desired rate is not a multiple 10 (1second/100Millis)
// To overcome this, we keep track of left over remainder
// and distribute this evenly during every fillInterval
overflowRps int
overflowTokens int
nextRefillTime int64
nextOverflowRefillTime int64
timeSource TimeSource
}
)
const (
millisPerSecond = 1000
backoffInterval = int64(10 * time.Millisecond)
)
// NewTokenBucket creates and returns a
// new token bucket rate limiter that
// repelenishes the bucket every 100
// milliseconds. Thread safe.
//
// @param rps
// Desired rate per second
//
// Golang.org has an alternative implementation
// of the rate limiter. On benchmarking, golang's
// implementation was order of magnitude slower.
// In addition, it does a lot more than what we
// need. These are the benchmarks under different
// scenarios
//
// BenchmarkTokenBucketParallel 50000000 40.7 ns/op
// BenchmarkGolangRateParallel 10000000 150 ns/op
// BenchmarkTokenBucketParallel-8 20000000 124 ns/op
// BenchmarkGolangRateParallel-8 10000000 208 ns/op
// BenchmarkTokenBucketParallel 50000000 37.8 ns/op
// BenchmarkGolangRateParallel 10000000 153 ns/op
// BenchmarkTokenBucketParallel-8 10000000 129 ns/op
// BenchmarkGolangRateParallel-8 10000000 208 ns/op
//
func NewTokenBucket(rps int, timeSource TimeSource) TokenBucket {
tb := new(tokenBucketImpl)
tb.timeSource = timeSource
tb.fillInterval = int64(time.Millisecond * 100)
tb.fillRate = (rps * 100) / millisPerSecond
tb.overflowRps = rps - (10 * tb.fillRate)
tb.refill(time.Now().UnixNano())
return tb
}
// NewTokenBucketFactory creates an instance of factory used for creating TokenBucket instances
func NewTokenBucketFactory() TokenBucketFactory {
return &tokenBucketFactoryImpl{}
}
// CreateTokenBucket creates and returns a
// new token bucket rate limiter that
// repelenishes the bucket every 100
// milliseconds. Thread safe.
func (f *tokenBucketFactoryImpl) CreateTokenBucket(rps int, timeSource TimeSource) TokenBucket {
return NewTokenBucket(rps, timeSource)
}
func (tb *tokenBucketImpl) TryConsume(count int) (bool, time.Duration) {
now := tb.timeSource.Now().UnixNano()
tb.Lock()
tb.refill(now)
nextRefillTime := time.Duration(tb.nextRefillTime - now)
if tb.tokens < count {
tb.Unlock()
return false, nextRefillTime
}
tb.tokens -= count
tb.Unlock()
return true, nextRefillTime
}
func (tb *tokenBucketImpl) Consume(count int, timeout time.Duration) bool {
var remTime = int64(timeout)
var expiryTime = time.Now().UnixNano() + int64(timeout)
for {
if ok, _ := tb.TryConsume(count); ok {
return true
}
if remTime < backoffInterval {
time.Sleep(time.Duration(remTime))
} else {
time.Sleep(time.Duration(backoffInterval))
}
now := time.Now().UnixNano()
if now >= expiryTime {
return false
}
remTime = expiryTime - now
}
}
func (tb *tokenBucketImpl) refill(now int64) {
tb.refillOverFlow(now)
if tb.isRefillDue(now) {
tb.tokens = tb.fillRate
if tb.overflowTokens > 0 {
tb.tokens++
tb.overflowTokens--
}
tb.nextRefillTime = now + tb.fillInterval
}
}
func (tb *tokenBucketImpl) refillOverFlow(now int64) {
if tb.overflowRps < 1 {
return
}
if tb.isOverflowRefillDue(now) {
tb.overflowTokens = tb.overflowRps
tb.nextOverflowRefillTime = now + int64(time.Second)
}
}
func (tb *tokenBucketImpl) isRefillDue(now int64) bool {
return now >= tb.nextRefillTime
}
func (tb *tokenBucketImpl) isOverflowRefillDue(now int64) bool {
return now >= tb.nextOverflowRefillTime
}