/
throttle.go
95 lines (79 loc) · 1.7 KB
/
throttle.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
package utils
import (
"context"
"time"
"github.com/Laisky/errors"
"github.com/Laisky/go-utils/v2/log"
)
// ThrottleCfg Throttle's configuration
type ThrottleCfg struct {
Max, NPerSec int
}
// Throttle current limitor
type Throttle struct {
*ThrottleCfg
token struct{}
tokensChan chan struct{}
stopChan chan struct{}
}
// NewThrottleWithCtx create new Throttle
//
// 90x faster than `rate.NewLimiter`
func NewThrottleWithCtx(ctx context.Context, cfg *ThrottleCfg) (t *Throttle, err error) {
if cfg.NPerSec <= 0 {
return nil, errors.Errorf("npersec should greater than 0")
}
if cfg.Max < cfg.NPerSec {
return nil, errors.Errorf("max should greater than npersec")
}
t = &Throttle{
ThrottleCfg: cfg,
token: struct{}{},
stopChan: make(chan struct{}),
}
t.tokensChan = make(chan struct{}, t.Max)
for i := 0; i < t.NPerSec; i++ {
t.tokensChan <- t.token
}
go t.runWithCtx(ctx)
return t, nil
}
// Allow check whether is allowed
func (t *Throttle) Allow() bool {
select {
case <-t.tokensChan:
return true
default:
return false
}
}
// runWithCtx start throttle with context
func (t *Throttle) runWithCtx(ctx context.Context) {
defer log.Shared.Debug("throttle exit")
var nBatch float64 = 10
nPerBatch := float64(t.NPerSec) / nBatch
interval := time.Duration(1/nBatch*1000) * time.Millisecond
ticker := time.NewTicker(interval)
defer ticker.Stop()
TOKEN_LOOP:
for {
select {
case <-ticker.C:
case <-ctx.Done():
return
case <-t.stopChan:
return
}
for i := float64(0); i < nPerBatch; i++ {
select {
case t.tokensChan <- t.token:
default:
continue TOKEN_LOOP
}
}
}
}
// Close stop throttle
func (t *Throttle) Close() {
close(t.stopChan)
}