-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlimiter.go
More file actions
283 lines (238 loc) · 5.94 KB
/
Copy pathlimiter.go
File metadata and controls
283 lines (238 loc) · 5.94 KB
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
package ratelimiter
import (
"errors"
"math"
"sync"
"time"
)
var (
ErrInvalidRate = errors.New("rate must be greater than zero")
ErrInvalidBurst = errors.New("burst must be greater than zero")
ErrInvalidHalfLife = errors.New("half-life must be greater than zero")
ErrInvalidHeatCost = errors.New("heat cost must be zero or greater")
ErrNilLimiter = errors.New("limiter must not be nil")
)
// Decision describes the outcome of a reservation attempt.
type Decision struct {
Allowed bool
ReadyAt time.Time
WaitFor time.Duration
Reason string
}
// Config tunes the limiter.
//
// Rate and Burst behave like a token bucket. HeatHalfLife and HeatCost add a
// caller-specific penalty that decays over time, making repeated bursts from
// the same key back off sooner than evenly distributed traffic.
type Config struct {
Rate float64
Burst int
HeatHalfLife time.Duration
HeatCost float64
MaxKeys int
}
// Limiter is a token bucket with caller heat decay.
type Limiter struct {
mu sync.Mutex
rate float64
burst float64
tokens float64
lastRefill time.Time
heatHalfLife time.Duration
heatCost float64
maxKeys int
callers map[string]callerState
now func() time.Time
}
type callerState struct {
heat float64
last time.Time
}
// New creates a limiter with a caller-fairness penalty.
func New(cfg Config) (*Limiter, error) {
if cfg.Rate <= 0 {
return nil, ErrInvalidRate
}
if cfg.Burst <= 0 {
return nil, ErrInvalidBurst
}
if cfg.HeatHalfLife <= 0 {
return nil, ErrInvalidHalfLife
}
if cfg.HeatCost < 0 {
return nil, ErrInvalidHeatCost
}
if cfg.MaxKeys <= 0 {
cfg.MaxKeys = 1024
}
now := time.Now()
return &Limiter{
rate: cfg.Rate,
burst: float64(cfg.Burst),
tokens: float64(cfg.Burst),
lastRefill: now,
heatHalfLife: cfg.HeatHalfLife,
heatCost: cfg.HeatCost,
maxKeys: cfg.MaxKeys,
callers: make(map[string]callerState, min(cfg.MaxKeys, 64)),
now: time.Now,
}, nil
}
// Allow reports whether a single action can proceed immediately.
func (l *Limiter) Allow(key string) bool {
return l.AllowN(key, 1)
}
// AllowN reports whether n actions can proceed immediately.
func (l *Limiter) AllowN(key string, n int) bool {
now := l.now()
if n <= 0 {
return true
}
l.mu.Lock()
defer l.mu.Unlock()
l.refill(now)
globalWait := l.previewGlobal(float64(n))
fairnessWait := l.previewCallerHeat(key, now)
if maxDuration(globalWait, fairnessWait) > 0 {
return false
}
l.commitGlobal(float64(n))
l.commitCallerHeat(key, now, float64(n))
return true
}
// Reserve returns when the request is allowed to proceed. Requests that would
// need to wait return Allowed=false; callers can inspect WaitFor or ReadyAt.
func (l *Limiter) Reserve(key string) Decision {
return l.ReserveN(key, 1)
}
// ReserveN attempts to acquire n actions for the caller key.
func (l *Limiter) ReserveN(key string, n int) Decision {
now := l.now()
if n <= 0 {
return Decision{
Allowed: true,
ReadyAt: now,
WaitFor: 0,
}
}
l.mu.Lock()
defer l.mu.Unlock()
l.refill(now)
globalWait := l.previewGlobal(float64(n))
fairnessWait := l.previewCallerHeat(key, now)
waitFor := maxDuration(globalWait, fairnessWait)
allowed := waitFor == 0
if allowed {
l.commitGlobal(float64(n))
l.commitCallerHeat(key, now, float64(n))
}
reason := ""
if !allowed {
switch {
case globalWait > fairnessWait:
reason = "global capacity exhausted"
case fairnessWait > globalWait:
reason = "caller heat throttled"
default:
reason = "global and caller limits engaged"
}
}
return Decision{
Allowed: allowed,
ReadyAt: now.Add(waitFor),
WaitFor: waitFor,
Reason: reason,
}
}
// Snapshot exposes the current token estimate and active caller count.
func (l *Limiter) Snapshot() (tokens float64, activeKeys int) {
now := l.now()
l.mu.Lock()
defer l.mu.Unlock()
l.refill(now)
return l.tokens, len(l.callers)
}
func (l *Limiter) refill(now time.Time) {
if !now.After(l.lastRefill) {
return
}
elapsed := now.Sub(l.lastRefill).Seconds()
l.tokens = math.Min(l.burst, l.tokens+elapsed*l.rate)
l.lastRefill = now
}
func (l *Limiter) previewGlobal(amount float64) time.Duration {
if l.tokens >= amount {
return 0
}
shortage := amount - l.tokens
seconds := shortage / l.rate
return durationFromSeconds(seconds)
}
func (l *Limiter) commitGlobal(amount float64) {
l.tokens -= amount
}
func (l *Limiter) previewCallerHeat(key string, now time.Time) time.Duration {
state, ok := l.callers[key]
if ok {
state.heat = decayHeat(state.heat, now.Sub(state.last), l.heatHalfLife)
}
penaltySeconds := 0.0
if l.heatCost > 0 && state.heat > 0 {
penaltySeconds = (state.heat * l.heatCost) / l.rate
}
return durationFromSeconds(penaltySeconds)
}
func (l *Limiter) commitCallerHeat(key string, now time.Time, amount float64) {
state, ok := l.callers[key]
if ok {
state.heat = decayHeat(state.heat, now.Sub(state.last), l.heatHalfLife)
}
state.heat += amount
state.last = now
l.callers[key] = state
if len(l.callers) > l.maxKeys {
l.evictColdest(now)
}
}
func (l *Limiter) evictColdest(now time.Time) {
var (
victimKey string
victimHeat = math.MaxFloat64
found bool
)
for key, state := range l.callers {
heat := decayHeat(state.heat, now.Sub(state.last), l.heatHalfLife)
if heat < victimHeat {
victimHeat = heat
victimKey = key
found = true
}
}
if found {
delete(l.callers, victimKey)
}
}
func decayHeat(heat float64, elapsed time.Duration, halfLife time.Duration) float64 {
if heat <= 0 || elapsed <= 0 {
return heat
}
return heat * math.Pow(0.5, float64(elapsed)/float64(halfLife))
}
func durationFromSeconds(seconds float64) time.Duration {
if seconds <= 0 {
return 0
}
return time.Duration(seconds * float64(time.Second))
}
func maxDuration(a, b time.Duration) time.Duration {
if a > b {
return a
}
return b
}
func min(a, b int) int {
if a < b {
return a
}
return b
}