-
Notifications
You must be signed in to change notification settings - Fork 49
/
fixedwindow.go
252 lines (226 loc) · 7.57 KB
/
fixedwindow.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package limiters
import (
"context"
"fmt"
"strconv"
"sync"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/bradfitz/gomemcache/memcache"
"github.com/pkg/errors"
"github.com/redis/go-redis/v9"
)
// FixedWindowIncrementer wraps the Increment method.
type FixedWindowIncrementer interface {
// Increment increments the request counter for the window and returns the counter value.
// TTL is the time duration before the next window.
Increment(ctx context.Context, window time.Time, ttl time.Duration) (int64, error)
}
// FixedWindow implements a Fixed Window rate limiting algorithm.
//
// Simple and memory efficient algorithm that does not need a distributed lock.
// However it may be lenient when there are many requests around the boundary between 2 adjacent windows.
type FixedWindow struct {
backend FixedWindowIncrementer
clock Clock
rate time.Duration
capacity int64
mu sync.Mutex
window time.Time
overflow bool
}
// NewFixedWindow creates a new instance of FixedWindow.
// Capacity is the maximum amount of requests allowed per window.
// Rate is the window size.
func NewFixedWindow(capacity int64, rate time.Duration, fixedWindowIncrementer FixedWindowIncrementer, clock Clock) *FixedWindow {
return &FixedWindow{backend: fixedWindowIncrementer, clock: clock, rate: rate, capacity: capacity}
}
// Limit returns the time duration to wait before the request can be processed.
// It returns ErrLimitExhausted if the request overflows the window's capacity.
func (f *FixedWindow) Limit(ctx context.Context) (time.Duration, error) {
f.mu.Lock()
defer f.mu.Unlock()
now := f.clock.Now()
window := now.Truncate(f.rate)
if f.window != window {
f.window = window
f.overflow = false
}
ttl := f.rate - now.Sub(window)
if f.overflow {
// If the window is already overflowed don't increment the counter.
return ttl, ErrLimitExhausted
}
c, err := f.backend.Increment(ctx, window, ttl)
if err != nil {
return 0, err
}
if c > f.capacity {
f.overflow = true
return ttl, ErrLimitExhausted
}
return 0, nil
}
// FixedWindowInMemory is an in-memory implementation of FixedWindowIncrementer.
type FixedWindowInMemory struct {
mu sync.Mutex
c int64
window time.Time
}
// NewFixedWindowInMemory creates a new instance of FixedWindowInMemory.
func NewFixedWindowInMemory() *FixedWindowInMemory {
return &FixedWindowInMemory{}
}
// Increment increments the window's counter.
func (f *FixedWindowInMemory) Increment(ctx context.Context, window time.Time, _ time.Duration) (int64, error) {
f.mu.Lock()
defer f.mu.Unlock()
if window != f.window {
f.c = 0
f.window = window
}
f.c++
return f.c, ctx.Err()
}
// FixedWindowRedis implements FixedWindow in Redis.
type FixedWindowRedis struct {
cli redis.UniversalClient
prefix string
}
// NewFixedWindowRedis returns a new instance of FixedWindowRedis.
// Prefix is the key prefix used to store all the keys used in this implementation in Redis.
func NewFixedWindowRedis(cli redis.UniversalClient, prefix string) *FixedWindowRedis {
return &FixedWindowRedis{cli: cli, prefix: prefix}
}
// Increment increments the window's counter in Redis.
func (f *FixedWindowRedis) Increment(ctx context.Context, window time.Time, ttl time.Duration) (int64, error) {
var incr *redis.IntCmd
var err error
done := make(chan struct{})
go func() {
defer close(done)
_, err = f.cli.Pipelined(ctx, func(pipeliner redis.Pipeliner) error {
key := fmt.Sprintf("%d", window.UnixNano())
incr = pipeliner.Incr(ctx, redisKey(f.prefix, key))
pipeliner.PExpire(ctx, redisKey(f.prefix, key), ttl)
return nil
})
}()
select {
case <-done:
if err != nil {
return 0, errors.Wrap(err, "redis transaction failed")
}
return incr.Val(), incr.Err()
case <-ctx.Done():
return 0, ctx.Err()
}
}
// FixedWindowMemcached implements FixedWindow in Memcached.
type FixedWindowMemcached struct {
cli *memcache.Client
prefix string
}
// NewFixedWindowMemcached returns a new instance of FixedWindowMemcached.
// Prefix is the key prefix used to store all the keys used in this implementation in Memcached.
func NewFixedWindowMemcached(cli *memcache.Client, prefix string) *FixedWindowMemcached {
return &FixedWindowMemcached{cli: cli, prefix: prefix}
}
// Increment increments the window's counter in Memcached.
func (f *FixedWindowMemcached) Increment(ctx context.Context, window time.Time, ttl time.Duration) (int64, error) {
var newValue uint64
var err error
done := make(chan struct{})
go func() {
defer close(done)
key := fmt.Sprintf("%s:%d", f.prefix, window.UnixNano())
newValue, err = f.cli.Increment(key, 1)
if err != nil && errors.Is(err, memcache.ErrCacheMiss) {
newValue = 1
item := &memcache.Item{
Key: key,
Value: []byte(strconv.FormatUint(newValue, 10)),
}
err = f.cli.Add(item)
}
}()
select {
case <-done:
if err != nil {
if errors.Is(err, memcache.ErrNotStored) {
return f.Increment(ctx, window, ttl)
}
return 0, errors.Wrap(err, "failed to Increment or Add")
}
return int64(newValue), err
case <-ctx.Done():
return 0, ctx.Err()
}
}
// FixedWindowDynamoDB implements FixedWindow in DynamoDB.
type FixedWindowDynamoDB struct {
client *dynamodb.Client
partitionKey string
tableProps DynamoDBTableProperties
}
// NewFixedWindowDynamoDB creates a new instance of FixedWindowDynamoDB.
// PartitionKey is the key used to store all the this implementation in DynamoDB.
//
// TableProps describe the table that this backend should work with. This backend requires the following on the table:
// * SortKey
// * TTL
func NewFixedWindowDynamoDB(client *dynamodb.Client, partitionKey string, props DynamoDBTableProperties) *FixedWindowDynamoDB {
return &FixedWindowDynamoDB{
client: client,
partitionKey: partitionKey,
tableProps: props,
}
}
const (
fixedWindowDynamoDBUpdateExpression = "SET #C = if_not_exists(#C, :def) + :inc, #TTL = :ttl"
dynamodbWindowCountKey = "Count"
)
// Increment increments the window's counter in DynamoDB.
func (f *FixedWindowDynamoDB) Increment(ctx context.Context, window time.Time, ttl time.Duration) (int64, error) {
var resp *dynamodb.UpdateItemOutput
var err error
done := make(chan struct{})
go func() {
defer close(done)
resp, err = f.client.UpdateItem(ctx, &dynamodb.UpdateItemInput{
Key: map[string]types.AttributeValue{
f.tableProps.PartitionKeyName: &types.AttributeValueMemberS{Value: f.partitionKey},
f.tableProps.SortKeyName: &types.AttributeValueMemberS{Value: strconv.FormatInt(window.UnixNano(), 10)},
},
UpdateExpression: aws.String(fixedWindowDynamoDBUpdateExpression),
ExpressionAttributeNames: map[string]string{
"#TTL": f.tableProps.TTLFieldName,
"#C": dynamodbWindowCountKey,
},
ExpressionAttributeValues: map[string]types.AttributeValue{
":ttl": &types.AttributeValueMemberN{Value: strconv.FormatInt(time.Now().Add(ttl).Unix(), 10)},
":def": &types.AttributeValueMemberN{Value: "0"},
":inc": &types.AttributeValueMemberN{Value: "1"},
},
TableName: &f.tableProps.TableName,
ReturnValues: types.ReturnValueAllNew,
})
}()
select {
case <-done:
case <-ctx.Done():
return 0, ctx.Err()
}
if err != nil {
return 0, errors.Wrap(err, "dynamodb update item failed")
}
var count float64
err = attributevalue.Unmarshal(resp.Attributes[dynamodbWindowCountKey], &count)
if err != nil {
return 0, errors.Wrap(err, "unmarshal of dynamodb attribute value failed")
}
return int64(count), nil
}