-
Notifications
You must be signed in to change notification settings - Fork 49
/
concurrent_buffer.go
271 lines (245 loc) · 8.23 KB
/
concurrent_buffer.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package limiters
import (
"bytes"
"context"
"encoding/gob"
"fmt"
"sync"
"time"
"github.com/bradfitz/gomemcache/memcache"
"github.com/pkg/errors"
"github.com/redis/go-redis/v9"
)
// ConcurrentBufferBackend wraps the Add and Remove methods.
type ConcurrentBufferBackend interface {
// Add adds the request with the given key to the buffer and returns the total number of requests in it.
Add(ctx context.Context, key string) (int64, error)
// Remove removes the request from the buffer.
Remove(ctx context.Context, key string) error
}
// ConcurrentBuffer implements a limiter that allows concurrent requests up to the given capacity.
type ConcurrentBuffer struct {
locker DistLocker
backend ConcurrentBufferBackend
logger Logger
capacity int64
mu sync.Mutex
}
// NewConcurrentBuffer creates a new ConcurrentBuffer instance.
func NewConcurrentBuffer(locker DistLocker, concurrentStateBackend ConcurrentBufferBackend, capacity int64, logger Logger) *ConcurrentBuffer {
return &ConcurrentBuffer{locker: locker, backend: concurrentStateBackend, capacity: capacity, logger: logger}
}
// Limit puts the request identified by the key in a buffer.
func (c *ConcurrentBuffer) Limit(ctx context.Context, key string) error {
c.mu.Lock()
defer c.mu.Unlock()
if err := c.locker.Lock(ctx); err != nil {
return err
}
defer func() {
if err := c.locker.Unlock(ctx); err != nil {
c.logger.Log(err)
}
}()
// Optimistically add the new request.
counter, err := c.backend.Add(ctx, key)
if err != nil {
return err
}
if counter > c.capacity {
// Rollback the Add() operation.
if err = c.backend.Remove(ctx, key); err != nil {
c.logger.Log(err)
}
return ErrLimitExhausted
}
return nil
}
// Done removes the request identified by the key from the buffer.
func (c *ConcurrentBuffer) Done(ctx context.Context, key string) error {
return c.backend.Remove(ctx, key)
}
// ConcurrentBufferInMemory is an in-memory implementation of ConcurrentBufferBackend.
type ConcurrentBufferInMemory struct {
clock Clock
ttl time.Duration
mu sync.Mutex
registry *Registry
}
// NewConcurrentBufferInMemory creates a new instance of ConcurrentBufferInMemory.
// When the TTL of a key exceeds the key is removed from the buffer. This is needed in case if the process that added
// that key to the buffer did not call Done() for some reason.
func NewConcurrentBufferInMemory(registry *Registry, ttl time.Duration, clock Clock) *ConcurrentBufferInMemory {
return &ConcurrentBufferInMemory{clock: clock, ttl: ttl, registry: registry}
}
// Add adds the request with the given key to the buffer and returns the total number of requests in it.
// It also removes the keys with expired TTL.
func (c *ConcurrentBufferInMemory) Add(ctx context.Context, key string) (int64, error) {
c.mu.Lock()
defer c.mu.Unlock()
now := c.clock.Now()
c.registry.DeleteExpired(now)
c.registry.GetOrCreate(key, func() interface{} {
return struct{}{}
}, c.ttl, now)
return int64(c.registry.Len()), ctx.Err()
}
// Remove removes the request from the buffer.
func (c *ConcurrentBufferInMemory) Remove(_ context.Context, key string) error {
c.mu.Lock()
defer c.mu.Unlock()
c.registry.Delete(key)
return nil
}
// ConcurrentBufferRedis implements ConcurrentBufferBackend in Redis.
type ConcurrentBufferRedis struct {
clock Clock
cli redis.UniversalClient
key string
ttl time.Duration
}
// NewConcurrentBufferRedis creates a new instance of ConcurrentBufferRedis.
// When the TTL of a key exceeds the key is removed from the buffer. This is needed in case if the process that added
// that key to the buffer did not call Done() for some reason.
func NewConcurrentBufferRedis(cli redis.UniversalClient, key string, ttl time.Duration, clock Clock) *ConcurrentBufferRedis {
return &ConcurrentBufferRedis{clock: clock, cli: cli, key: key, ttl: ttl}
}
// Add adds the request with the given key to the sorted set in Redis and returns the total number of requests in it.
// It also removes the keys with expired TTL.
func (c *ConcurrentBufferRedis) Add(ctx context.Context, key string) (int64, error) {
var countCmd *redis.IntCmd
var err error
done := make(chan struct{})
go func() {
defer close(done)
_, err = c.cli.Pipelined(ctx, func(pipeliner redis.Pipeliner) error {
// Remove expired items.
now := c.clock.Now()
pipeliner.ZRemRangeByScore(ctx, c.key, "-inf", fmt.Sprintf("%d", now.Add(-c.ttl).UnixNano()))
pipeliner.ZAdd(ctx, c.key, redis.Z{
Score: float64(now.UnixNano()),
Member: key,
})
countCmd = pipeliner.ZCard(ctx, c.key)
return nil
})
}()
select {
case <-ctx.Done():
return 0, ctx.Err()
case <-done:
if err != nil {
return 0, errors.Wrap(err, "failed to add an item to redis set")
}
return countCmd.Val(), nil
}
}
// Remove removes the request identified by the key from the sorted set in Redis.
func (c *ConcurrentBufferRedis) Remove(ctx context.Context, key string) error {
return errors.Wrap(c.cli.ZRem(ctx, c.key, key).Err(), "failed to remove an item from redis set")
}
// ConcurrentBufferMemcached implements ConcurrentBufferBackend in Memcached.
type ConcurrentBufferMemcached struct {
clock Clock
cli *memcache.Client
key string
ttl time.Duration
}
// NewConcurrentBufferMemcached creates a new instance of ConcurrentBufferMemcached.
// When the TTL of a key exceeds the key is removed from the buffer. This is needed in case if the process that added
// that key to the buffer did not call Done() for some reason.
func NewConcurrentBufferMemcached(cli *memcache.Client, key string, ttl time.Duration, clock Clock) *ConcurrentBufferMemcached {
return &ConcurrentBufferMemcached{clock: clock, cli: cli, key: key, ttl: ttl}
}
type SortedSetNode struct {
CreatedAt int64
Value string
}
// Add adds the request with the given key to the slice in Memcached and returns the total number of requests in it.
// It also removes the keys with expired TTL.
func (c *ConcurrentBufferMemcached) Add(ctx context.Context, element string) (int64, error) {
var err error
done := make(chan struct{})
now := c.clock.Now()
var newNodes []SortedSetNode
var casId uint64 = 0
go func() {
defer close(done)
var item *memcache.Item
item, err = c.cli.Get(c.key)
if err != nil {
if !errors.Is(err, memcache.ErrCacheMiss) {
return
}
} else {
casId = item.CasID
b := bytes.NewBuffer(item.Value)
var oldNodes []SortedSetNode
_ = gob.NewDecoder(b).Decode(&oldNodes)
for _, node := range oldNodes {
if node.CreatedAt > now.UnixNano() && node.Value != element {
newNodes = append(newNodes, node)
}
}
}
newNodes = append(newNodes, SortedSetNode{CreatedAt: now.Add(c.ttl).UnixNano(), Value: element})
var b bytes.Buffer
_ = gob.NewEncoder(&b).Encode(newNodes)
item = &memcache.Item{
Key: c.key,
Value: b.Bytes(),
CasID: casId,
}
if casId > 0 {
err = c.cli.CompareAndSwap(item)
} else {
err = c.cli.Add(item)
}
}()
select {
case <-ctx.Done():
return 0, ctx.Err()
case <-done:
if err != nil {
if errors.Is(err, memcache.ErrCASConflict) || errors.Is(err, memcache.ErrNotStored) || errors.Is(err, memcache.ErrCacheMiss) {
return c.Add(ctx, element)
}
return 0, errors.Wrap(err, "failed to add in memcached")
}
return int64(len(newNodes)), nil
}
}
// Remove removes the request identified by the key from the slice in Memcached.
func (c *ConcurrentBufferMemcached) Remove(ctx context.Context, key string) error {
var err error
now := c.clock.Now()
var newNodes []SortedSetNode
var casID uint64
item, err := c.cli.Get(c.key)
if err != nil {
if errors.Is(err, memcache.ErrCacheMiss) {
return nil
}
return errors.Wrap(err, "failed to Get")
}
casID = item.CasID
var oldNodes []SortedSetNode
_ = gob.NewDecoder(bytes.NewBuffer(item.Value)).Decode(&oldNodes)
for _, node := range oldNodes {
if node.CreatedAt > now.UnixNano() && node.Value != key {
newNodes = append(newNodes, node)
}
}
var b bytes.Buffer
_ = gob.NewEncoder(&b).Encode(newNodes)
item = &memcache.Item{
Key: c.key,
Value: b.Bytes(),
CasID: casID,
}
err = c.cli.CompareAndSwap(item)
if err != nil && (errors.Is(err, memcache.ErrCASConflict) || errors.Is(err, memcache.ErrNotStored) || errors.Is(err, memcache.ErrCacheMiss)) {
return c.Remove(ctx, key)
}
return errors.Wrap(err, "failed to CompareAndSwap")
}