-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken_bucket.go
52 lines (44 loc) · 1.29 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
package ratelimiter
import (
"sync"
"time"
)
type TokenBucket struct {
rate float64
capacity float64
tokens float64
lastRefill time.Time
mu sync.Mutex
timeProvider TimeProvider
}
// NewTokenBucket create a new Rate Limiter using Token Bucket algorithm.
// rate represents the number of tokens added per second.
// capacity is the total number of tokens allowed anytime.
// tp represents the time provider for getting the current UTC time. helpful for mocking time when testing.
func NewTokenBucket(rate float64, capacity float64, tp TimeProvider) *TokenBucket {
return &TokenBucket{
rate: rate,
capacity: capacity,
tokens: capacity,
lastRefill: tp.Now(),
timeProvider: tp,
}
}
func (tb *TokenBucket) Allow() bool {
tb.mu.Lock()
defer tb.mu.Unlock()
// 1. Calculate the number of tokens to add based on the time elapsed since last refill
now := tb.timeProvider.Now()
elapsed := now.Sub(tb.lastRefill)
// 2. Add tokens to the bucket (up to capacity)
tokensToAdd := elapsed.Seconds() * tb.rate
tb.tokens = min(tb.tokens+tokensToAdd, tb.capacity)
tb.lastRefill = now
// 3. If there's at least one token, consume it and return true
if tb.tokens >= 1 {
tb.tokens -= 1
return true
}
// 4. Otherwise, return false
return false
}