/
tokenbucket.go
61 lines (47 loc) · 1.07 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
package proxy
import (
"sync"
"time"
)
/*
* A variation of token bucket for traffic throttling
* Note goflyway is intended for single user mainly, so throttling is really not that necessary for now
* TODO: bugs all around
*/
type TokenBucket struct {
Speed int64 // bytes per second
capacity int64 // bytes
maxCapacity int64
lastConsume int64
mu sync.Mutex
}
func NewTokenBucket(speed, max int64) *TokenBucket {
return &TokenBucket{
Speed: speed,
lastConsume: time.Now().UnixNano(),
maxCapacity: max,
}
}
func (tb *TokenBucket) Consume(n int64) {
tb.mu.Lock()
defer tb.mu.Unlock()
now := time.Now().UnixNano()
if tb.Speed == 0 {
tb.lastConsume = now
return
}
ms := (now - tb.lastConsume) / 1e6
tb.capacity += ms * tb.Speed / 1000
if tb.capacity > tb.maxCapacity {
tb.capacity = tb.maxCapacity
}
if n <= tb.capacity {
tb.lastConsume = now
tb.capacity -= n
return
}
sec := float64(n-tb.capacity) / float64(tb.Speed)
time.Sleep(time.Duration(sec*1000) * time.Millisecond)
tb.capacity = 0
tb.lastConsume = time.Now().UnixNano()
}