-
Notifications
You must be signed in to change notification settings - Fork 8
/
keyratelimit.go
143 lines (129 loc) · 3.31 KB
/
keyratelimit.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package ratelimit
import (
"context"
"sync"
"time"
errorutil "github.com/projectdiscovery/utils/errors"
)
var (
ErrKeyAlreadyExists = errorutil.NewWithTag("MultiLimiter", "key already exists")
ErrKeyMissing = errorutil.NewWithTag("MultiLimiter", "key does not exist")
)
// Options of MultiLimiter
type Options struct {
Key string // Unique Identifier
IsUnlimited bool
MaxCount uint
Duration time.Duration
}
// Validate given MultiLimiter Options
func (o *Options) Validate() error {
if !o.IsUnlimited {
if o.Key == "" {
return errorutil.NewWithTag("MultiLimiter", "empty keys not allowed")
}
if o.MaxCount == 0 {
return errorutil.NewWithTag("MultiLimiter", "maxcount cannot be zero")
}
if o.Duration == 0 {
return errorutil.NewWithTag("MultiLimiter", "time duration not set")
}
}
return nil
}
// MultiLimiter is wrapper around Limiter than can limit based on a key
type MultiLimiter struct {
limiters sync.Map // map of limiters
ctx context.Context
}
// Add new bucket with key
func (m *MultiLimiter) Add(opts *Options) error {
if err := opts.Validate(); err != nil {
return err
}
var rlimiter *Limiter
if opts.IsUnlimited {
rlimiter = NewUnlimited(m.ctx)
} else {
rlimiter = New(m.ctx, opts.MaxCount, opts.Duration)
}
// ok is true if key already exists
_, ok := m.limiters.LoadOrStore(opts.Key, rlimiter)
if ok {
return ErrKeyAlreadyExists.Msgf("key: %v", opts.Key)
}
return nil
}
// GetLimit returns current ratelimit of given key
func (m *MultiLimiter) GetLimit(key string) (uint, error) {
limiter, err := m.get(key)
if err != nil {
return 0, err
}
return limiter.GetLimit(), nil
}
// Take one token from bucket returns error if key not present
func (m *MultiLimiter) Take(key string) error {
limiter, err := m.get(key)
if err != nil {
return err
}
limiter.Take()
return nil
}
// CanTake checks if the rate limiter with the given key has any token
func (m *MultiLimiter) CanTake(key string) bool {
limiter, err := m.get(key)
if err != nil {
return false
}
return limiter.CanTake()
}
// AddAndTake adds key if not present and then takes token from bucket
func (m *MultiLimiter) AddAndTake(opts *Options) {
if limiter, err := m.get(opts.Key); err == nil {
limiter.Take()
return
}
_ = m.Add(opts)
_ = m.Take(opts.Key)
}
// Stop internal limiters with defined keys or all if no key is provided
func (m *MultiLimiter) Stop(keys ...string) {
if len(keys) == 0 {
m.limiters.Range(func(key, value any) bool {
if limiter, ok := value.(*Limiter); ok {
limiter.Stop()
}
return true
})
return
}
for _, v := range keys {
if limiter, err := m.get(v); err == nil {
limiter.Stop()
}
}
}
// get returns *Limiter instance
func (m *MultiLimiter) get(key string) (*Limiter, error) {
val, _ := m.limiters.Load(key)
if val == nil {
return nil, ErrKeyMissing.Msgf("key: %v", key)
}
if limiter, ok := val.(*Limiter); ok {
return limiter, nil
}
return nil, errorutil.NewWithTag("MultiLimiter", "type assertion of rateLimiter failed in multiLimiter")
}
// NewMultiLimiter : Limits
func NewMultiLimiter(ctx context.Context, opts *Options) (*MultiLimiter, error) {
if err := opts.Validate(); err != nil {
return nil, err
}
multilimiter := &MultiLimiter{
ctx: ctx,
limiters: sync.Map{},
}
return multilimiter, multilimiter.Add(opts)
}