forked from mennanov/limiters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcurrent_buffer.go
154 lines (137 loc) · 4.78 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
package limiters
import (
"context"
"fmt"
"sync"
"time"
"github.com/go-redis/redis"
"github.com/pkg/errors"
)
// 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(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(); 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(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(key string) error {
return c.backend.Remove(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(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.Client
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.Client, 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(func(pipeliner redis.Pipeliner) error {
// Remove expired items.
now := c.clock.Now()
pipeliner.ZRemRangeByScore(c.key, "-inf", fmt.Sprintf("%d", now.Add(-c.ttl).UnixNano()))
pipeliner.ZAdd(c.key, redis.Z{
Score: float64(now.UnixNano()),
Member: key,
})
countCmd = pipeliner.ZCount(c.key, "-inf", "+inf")
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(key string) error {
return errors.Wrap(c.cli.ZRem(c.key, key).Err(), "failed to remove an item from redis set")
}