-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenbucket.go
86 lines (71 loc) · 1.71 KB
/
tokenbucket.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
package tokenbucket
import (
"sync"
"time"
)
type Bucket struct {
capacity int64
tokens chan struct{}
rate time.Duration // Add a token to the bucket every 1/r units of time
rateMutex sync.Mutex
}
func NewBucket(rate time.Duration, capacity int64) *Bucket {
//A bucket is simply a channel with a buffer representing the maximum size
tokens := make(chan struct{}, capacity)
b := &Bucket{capacity, tokens, rate, sync.Mutex{}}
//Set off a function that will continuously add tokens to the bucket
go func(b *Bucket) {
ticker := time.NewTicker(rate)
for _ = range ticker.C {
b.tokens <- struct{}{}
}
}(b)
return b
}
func (b *Bucket) GetRate() time.Duration {
b.rateMutex.Lock()
tmp := b.rate
b.rateMutex.Unlock()
return tmp
}
func (b *Bucket) SetRate(rate time.Duration) {
b.rateMutex.Lock()
b.rate = rate
b.rateMutex.Unlock()
}
//AddTokens manually adds n tokens to the bucket
func (b *Bucket) AddToken(n int64) {
}
func (b *Bucket) withdrawTokens(n int64) error {
for i := int64(0); i < n; i++ {
<-b.tokens
}
return nil
}
func (b *Bucket) SpendToken(n int64) <-chan error {
// Default to spending a single token
if n < 0 {
n = 1
}
c := make(chan error)
go func(b *Bucket, n int64, c chan error) {
c <- b.withdrawTokens(n)
close(c)
return
}(b, n, c)
return c
}
// Drain will empty all tokens in the bucket
// If the tokens are being added too quickly (if the rate is too fast)
// this will never drain
func (b *Bucket) Drain() error{
// TODO replace this with a more solid approach (such as replacing the channel altogether)
for {
select {
case _ = <-b.tokens:
continue
default:
return nil
}
}
}