-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilter.go
69 lines (61 loc) · 1.28 KB
/
filter.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
package miser
import (
"sync"
)
//filter分装了对GCRARateLimiter使用,用户可以实时控制流量
type Filter struct {
st GCRAStore
// map[{限流key}]*GCRARateLimiter
keyM sync.Map
// 限流回调函数
f func(key string)
}
func NewFilter(store GCRAStore) *Filter {
return &Filter{
st: store,
}
}
// 对外暴露的限流方法,false为通过
func (f *Filter) RateLimit(key string) (bool, error) {
if l, ok := f.GetRateLimiter(key); ok {
b, _, e := l.RateLimit(key, 1)
if e == nil {
if b && f.f != nil {
go f.f(key)
}
return b, nil
}
return false, e
}
//默认通过
return false, nil
}
func (f *Filter) AddKey(key string, quota RateQuota) error {
rateLimiter, err := NewGCRARateLimiter(f.st, quota)
if err != nil {
return err
}
f.keyM.Store(key, rateLimiter)
return nil
}
func (f *Filter) UpdateKey(key string, quota RateQuota) error {
rateLimiter, err := NewGCRARateLimiter(f.st, quota)
if err != nil {
return err
}
f.keyM.Store(key, rateLimiter)
return nil
}
func (f *Filter) DeleteKey(key string) {
f.keyM.Delete(key)
}
func (f *Filter) GetRateLimiter(key string) (*GCRARateLimiter, bool) {
l, ok := f.keyM.Load(key)
if !ok {
return nil, false
}
return l.(*GCRARateLimiter), true
}
func (f *Filter) Clean() {
f.keyM = sync.Map{}
}