-
Notifications
You must be signed in to change notification settings - Fork 7
/
rate.go
148 lines (121 loc) · 3.65 KB
/
rate.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
144
145
146
147
148
package go_limiter
import (
"context"
"errors"
"time"
"github.com/redis/go-redis/v9"
)
const (
DefaultPrefix = "limiter"
GCRAAlgorithm = iota
SlidingWindowAlgorithm
)
var (
algorithmNames = map[uint]string{
GCRAAlgorithm: GCRAAlgorithmName,
SlidingWindowAlgorithm: SlidingWindowAlgorithmName,
}
algorithmKeys = map[string]uint{
GCRAAlgorithmName: GCRAAlgorithm,
SlidingWindowAlgorithmName: SlidingWindowAlgorithm,
}
)
type (
Algorithm interface {
Allow(ctx context.Context) (*Result, error)
Reset(ctx context.Context) error
SetKey(string)
}
rediser interface {
Eval(ctx context.Context, script string, keys []string, args ...interface{}) *redis.Cmd
EvalSha(ctx context.Context, sha1 string, keys []string, args ...interface{}) *redis.Cmd
EvalRO(ctx context.Context, script string, keys []string, args ...interface{}) *redis.Cmd
EvalShaRO(ctx context.Context, sha1 string, keys []string, args ...interface{}) *redis.Cmd
ScriptExists(ctx context.Context, hashes ...string) *redis.BoolSliceCmd
ScriptLoad(ctx context.Context, script string) *redis.StringCmd
Del(ctx context.Context, key ...string) *redis.IntCmd
}
Limit struct {
Algorithm uint
Rate int64
Period time.Duration
Burst int64
}
Result struct {
// Limit is the limit that was used to obtain this result.
Limit *Limit
// Key is the key of limit
Key string
// Allowed reports whether event may happen at time now.
Allowed bool
// Remaining is the maximum number of requests that could be
// permitted instantaneously for this key given the current
// state. For example, if a rate limiter allows 10 requests per
// second and has already received 6 requests for this key this
// second, Remaining would be 4.
Remaining int64
// RetryAfter is the time until the next request will be permitted.
// It should be -1 unless the rate limit has been exceeded.
RetryAfter time.Duration
// ResetAfter is the time until the RateLimiter returns to its
// initial state for a given key. For example, if a rate limiter
// manages requests per second and received one request 200ms ago,
// Reset would return 800ms. You can also think of this as the time
// until Limit and Remaining will be equal.
ResetAfter time.Duration
}
)
// Limiter controls how frequently events are allowed to happen.
type Limiter struct {
rdb rediser
Prefix string
}
// NewLimiter returns a new Limiter.
func NewLimiter(rdb rediser) *Limiter {
return &Limiter{
rdb: rdb,
Prefix: DefaultPrefix,
}
}
func (l *Limiter) Allow(ctx context.Context, key string, limit *Limit) (*Result, error) {
var algo Algorithm
algo, err := l.getAlgorithm(limit)
if err != nil {
return nil, err
}
name, _ := GetAlgorithmName(limit.Algorithm)
algo.SetKey(l.Prefix + ":" + name + ":" + key)
return algo.Allow(ctx)
}
func (l *Limiter) Reset(ctx context.Context, key string, limit *Limit) error {
var algo Algorithm
algo, err := l.getAlgorithm(limit)
if err != nil {
return err
}
name, _ := GetAlgorithmName(limit.Algorithm)
algo.SetKey(l.Prefix + ":" + name + ":" + key)
return algo.Reset(ctx)
}
func (l *Limiter) getAlgorithm(limit *Limit) (Algorithm, error) {
switch limit.Algorithm {
case SlidingWindowAlgorithm:
return &slidingWindow{limit: limit, rdb: l.rdb}, nil
case GCRAAlgorithm:
return &gcra{limit: limit, rdb: l.rdb}, nil
default:
return nil, errors.New("algorithm is not supported")
}
}
func GetAlgorithmName(a uint) (string, bool) {
if name, ok := algorithmNames[a]; ok {
return name, ok
}
return "", false
}
func GetAlgorithmKey(n string) (uint, bool) {
if key, ok := algorithmKeys[n]; ok {
return key, ok
}
return 0, false
}