-
Notifications
You must be signed in to change notification settings - Fork 49
/
tokenbucket.go
763 lines (679 loc) · 23.2 KB
/
tokenbucket.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
package limiters
import (
"bytes"
"context"
"encoding/gob"
"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"
"go.etcd.io/etcd/api/v3/mvccpb"
"go.etcd.io/etcd/api/v3/v3rpc/rpctypes"
clientv3 "go.etcd.io/etcd/client/v3"
)
// TokenBucketState represents a state of a token bucket.
type TokenBucketState struct {
// Last is the last time the state was updated (Unix timestamp in nanoseconds).
Last int64
// Available is the number of available tokens in the bucket.
Available int64
}
// isZero returns true if the bucket state is zero valued.
func (s TokenBucketState) isZero() bool {
return s.Last == 0 && s.Available == 0
}
// TokenBucketStateBackend interface encapsulates the logic of retrieving and persisting the state of a TokenBucket.
type TokenBucketStateBackend interface {
// State gets the current state of the TokenBucket.
State(ctx context.Context) (TokenBucketState, error)
// SetState sets (persists) the current state of the TokenBucket.
SetState(ctx context.Context, state TokenBucketState) error
// Reset resets (persists) the current state of the TokenBucket.
Reset(ctx context.Context) error
}
// TokenBucket implements the https://en.wikipedia.org/wiki/Token_bucket algorithm.
type TokenBucket struct {
locker DistLocker
backend TokenBucketStateBackend
clock Clock
logger Logger
// refillRate is the tokens refill rate (1 token per duration).
refillRate time.Duration
// capacity is the bucket's capacity.
capacity int64
mu sync.Mutex
}
// NewTokenBucket creates a new instance of TokenBucket.
func NewTokenBucket(capacity int64, refillRate time.Duration, locker DistLocker, tokenBucketStateBackend TokenBucketStateBackend, clock Clock, logger Logger) *TokenBucket {
return &TokenBucket{
locker: locker,
backend: tokenBucketStateBackend,
clock: clock,
logger: logger,
refillRate: refillRate,
capacity: capacity,
}
}
// Take takes tokens from the bucket.
//
// It returns a zero duration and a nil error if the bucket has sufficient amount of tokens.
//
// It returns ErrLimitExhausted if the amount of available tokens is less than requested. In this case the returned
// duration is the amount of time to wait to retry the request.
func (t *TokenBucket) Take(ctx context.Context, tokens int64) (time.Duration, error) {
t.mu.Lock()
defer t.mu.Unlock()
if err := t.locker.Lock(ctx); err != nil {
return 0, err
}
defer func() {
if err := t.locker.Unlock(ctx); err != nil {
t.logger.Log(err)
}
}()
state, err := t.backend.State(ctx)
if err != nil {
return 0, err
}
if state.isZero() {
// Initially the bucket is full.
state.Available = t.capacity
}
now := t.clock.Now().UnixNano()
// Refill the bucket.
tokensToAdd := (now - state.Last) / int64(t.refillRate)
partialTime := (now - state.Last) % int64(t.refillRate)
if tokensToAdd > 0 {
if tokensToAdd+state.Available < t.capacity {
state.Available += tokensToAdd
state.Last = now - partialTime
} else {
state.Available = t.capacity
state.Last = now
}
}
if tokens > state.Available {
return t.refillRate * time.Duration(tokens-state.Available), ErrLimitExhausted
}
// Take the tokens from the bucket.
state.Available -= tokens
if err = t.backend.SetState(ctx, state); err != nil {
return 0, err
}
return 0, nil
}
// Limit takes 1 token from the bucket.
func (t *TokenBucket) Limit(ctx context.Context) (time.Duration, error) {
return t.Take(ctx, 1)
}
// Reset resets the bucket.
func (t *TokenBucket) Reset(ctx context.Context) error {
return t.backend.Reset(ctx)
}
// TokenBucketInMemory is an in-memory implementation of TokenBucketStateBackend.
//
// The state is not shared nor persisted so it won't survive restarts or failures.
// Due to the local nature of the state the rate at which some endpoints are accessed can't be reliably predicted or
// limited.
//
// Although it can be used as a global rate limiter with a round-robin load-balancer.
type TokenBucketInMemory struct {
state TokenBucketState
}
// NewTokenBucketInMemory creates a new instance of TokenBucketInMemory.
func NewTokenBucketInMemory() *TokenBucketInMemory {
return &TokenBucketInMemory{}
}
// State returns the current bucket's state.
func (t *TokenBucketInMemory) State(ctx context.Context) (TokenBucketState, error) {
return t.state, ctx.Err()
}
// SetState sets the current bucket's state.
func (t *TokenBucketInMemory) SetState(ctx context.Context, state TokenBucketState) error {
t.state = state
return ctx.Err()
}
// Reset resets the current bucket's state.
func (t *TokenBucketInMemory) Reset(ctx context.Context) error {
state := TokenBucketState{
Last: 0,
Available: 0,
}
return t.SetState(ctx, state)
}
const (
etcdKeyTBLease = "lease"
etcdKeyTBAvailable = "available"
etcdKeyTBLast = "last"
)
// TokenBucketEtcd is an etcd implementation of a TokenBucketStateBackend.
//
// See https://github.com/etcd-io/etcd/blob/master/Documentation/learning/data_model.md
//
// etcd is designed to reliably store infrequently updated data, thus it should only be used for the API endpoints which
// are accessed less frequently than it can be processed by the rate limiter.
//
// Aggressive compaction and defragmentation has to be enabled in etcd to prevent the size of the storage
// to grow indefinitely: every change of the state of the bucket (every access) will create a new revision in etcd.
//
// It probably makes it impractical for the high load cases, but can be used to reliably and precisely rate limit an
// access to the business critical endpoints where each access must be reliably logged.
type TokenBucketEtcd struct {
// prefix is the etcd key prefix.
prefix string
cli *clientv3.Client
leaseID clientv3.LeaseID
ttl time.Duration
raceCheck bool
lastVersion int64
}
// NewTokenBucketEtcd creates a new TokenBucketEtcd instance.
// Prefix is used as an etcd key prefix for all keys stored in etcd by this algorithm.
// TTL is a TTL of the etcd lease in seconds used to store all the keys: all the keys are automatically deleted after
// the TTL expires.
//
// If raceCheck is true and the keys in etcd are modified in between State() and SetState() calls then
// ErrRaceCondition is returned.
// It does not add any significant overhead as it can be trivially checked on etcd side before updating the keys.
func NewTokenBucketEtcd(cli *clientv3.Client, prefix string, ttl time.Duration, raceCheck bool) *TokenBucketEtcd {
return &TokenBucketEtcd{
prefix: prefix,
cli: cli,
ttl: ttl,
raceCheck: raceCheck,
}
}
// etcdKey returns a full etcd key from the provided key and prefix.
func etcdKey(prefix, key string) string {
return fmt.Sprintf("%s/%s", prefix, key)
}
// parseEtcdInt64 parses the etcd value into int64.
func parseEtcdInt64(kv *mvccpb.KeyValue) (int64, error) {
v, err := strconv.ParseInt(string(kv.Value), 10, 64)
if err != nil {
return 0, errors.Wrapf(err, "failed to parse key '%s' as int64", string(kv.Key))
}
return v, nil
}
func incPrefix(p string) string {
b := []byte(p)
b[len(b)-1]++
return string(b)
}
// State gets the bucket's current state from etcd.
// If there is no state available in etcd then the initial bucket's state is returned.
func (t *TokenBucketEtcd) State(ctx context.Context) (TokenBucketState, error) {
// Get all the keys under the prefix in a single request.
r, err := t.cli.Get(ctx, t.prefix, clientv3.WithRange(incPrefix(t.prefix)))
if err != nil {
return TokenBucketState{}, errors.Wrapf(err, "failed to get keys in range ['%s', '%s') from etcd", t.prefix, incPrefix(t.prefix))
}
if len(r.Kvs) == 0 {
// State not found, return zero valued state.
return TokenBucketState{}, nil
}
state := TokenBucketState{}
parsed := 0
var v int64
for _, kv := range r.Kvs {
switch string(kv.Key) {
case etcdKey(t.prefix, etcdKeyTBAvailable):
v, err = parseEtcdInt64(kv)
if err != nil {
return TokenBucketState{}, err
}
state.Available = v
parsed |= 1
case etcdKey(t.prefix, etcdKeyTBLast):
v, err = parseEtcdInt64(kv)
if err != nil {
return TokenBucketState{}, err
}
state.Last = v
parsed |= 2
t.lastVersion = kv.Version
case etcdKey(t.prefix, etcdKeyTBLease):
v, err = parseEtcdInt64(kv)
if err != nil {
return TokenBucketState{}, err
}
t.leaseID = clientv3.LeaseID(v)
parsed |= 4
}
}
if parsed != 7 {
return TokenBucketState{}, errors.New("failed to get state from etcd: some keys are missing")
}
return state, nil
}
// createLease creates a new lease in etcd and updates the t.leaseID value.
func (t *TokenBucketEtcd) createLease(ctx context.Context) error {
lease, err := t.cli.Grant(ctx, int64(t.ttl/time.Nanosecond))
if err != nil {
return errors.Wrap(err, "failed to create a new lease in etcd")
}
t.leaseID = lease.ID
return nil
}
// save saves the state to etcd using the existing lease and the fencing token.
func (t *TokenBucketEtcd) save(ctx context.Context, state TokenBucketState) error {
if !t.raceCheck {
if _, err := t.cli.Txn(ctx).Then(
clientv3.OpPut(etcdKey(t.prefix, etcdKeyTBAvailable), fmt.Sprintf("%d", state.Available), clientv3.WithLease(t.leaseID)),
clientv3.OpPut(etcdKey(t.prefix, etcdKeyTBLast), fmt.Sprintf("%d", state.Last), clientv3.WithLease(t.leaseID)),
clientv3.OpPut(etcdKey(t.prefix, etcdKeyTBLease), fmt.Sprintf("%d", t.leaseID), clientv3.WithLease(t.leaseID)),
).Commit(); err != nil {
return errors.Wrap(err, "failed to commit a transaction to etcd")
}
return nil
}
// Put the keys only if they have not been modified since the most recent read.
r, err := t.cli.Txn(ctx).If(
clientv3.Compare(clientv3.Version(etcdKey(t.prefix, etcdKeyTBLast)), ">", t.lastVersion),
).Else(
clientv3.OpPut(etcdKey(t.prefix, etcdKeyTBAvailable), fmt.Sprintf("%d", state.Available), clientv3.WithLease(t.leaseID)),
clientv3.OpPut(etcdKey(t.prefix, etcdKeyTBLast), fmt.Sprintf("%d", state.Last), clientv3.WithLease(t.leaseID)),
clientv3.OpPut(etcdKey(t.prefix, etcdKeyTBLease), fmt.Sprintf("%d", t.leaseID), clientv3.WithLease(t.leaseID)),
).Commit()
if err != nil {
return errors.Wrap(err, "failed to commit a transaction to etcd")
}
if !r.Succeeded {
return nil
}
return ErrRaceCondition
}
// SetState updates the state of the bucket.
func (t *TokenBucketEtcd) SetState(ctx context.Context, state TokenBucketState) error {
if t.leaseID == 0 {
// Lease does not exist, create one.
if err := t.createLease(ctx); err != nil {
return err
}
// No need to send KeepAlive for the newly created lease: save the state immediately.
return t.save(ctx, state)
}
// Send the KeepAlive request to extend the existing lease.
if _, err := t.cli.KeepAliveOnce(ctx, t.leaseID); err == rpctypes.ErrLeaseNotFound {
// Create a new lease since the current one has expired.
if err = t.createLease(ctx); err != nil {
return err
}
} else if err != nil {
return errors.Wrapf(err, "failed to extend the lease '%d'", t.leaseID)
}
return t.save(ctx, state)
}
// Reset resets the state of the bucket.
func (t *TokenBucketEtcd) Reset(ctx context.Context) error {
state := TokenBucketState{
Last: 0,
Available: 0,
}
return t.SetState(ctx, state)
}
const (
redisKeyTBAvailable = "available"
redisKeyTBLast = "last"
redisKeyTBVersion = "version"
)
// If we do use cluster client and if the cluster is large enough, it is possible that when accessing multiple keys
// in leaky bucket or token bucket, these keys might go different slots and it will fail with error message
// `CROSSSLOT Keys in request don't hash to the same slot`. Adding hash tags in redisKey will force them into the
// same slot for keys with the same prefix.
//
// https://redis.io/docs/latest/operate/oss_and_stack/reference/cluster-spec/#hash-tags
func redisKey(prefix, key string) string {
return fmt.Sprintf("{%s}%s", prefix, key)
}
// TokenBucketRedis is a Redis implementation of a TokenBucketStateBackend.
//
// Redis is an in-memory key-value data storage which also supports persistence.
// It is a better choice for high load cases than etcd as it does not keep old values of the keys thus does not need
// the compaction/defragmentation.
//
// Although depending on a persistence and a cluster configuration some data might be lost in case of a failure
// resulting in an under-limiting the accesses to the service.
type TokenBucketRedis struct {
cli redis.UniversalClient
prefix string
ttl time.Duration
raceCheck bool
lastVersion int64
}
// NewTokenBucketRedis creates a new TokenBucketRedis instance.
// Prefix is the key prefix used to store all the keys used in this implementation in Redis.
// TTL is the TTL of the stored keys.
//
// If raceCheck is true and the keys in Redis are modified in between State() and SetState() calls then
// ErrRaceCondition is returned.
// This adds an extra overhead since a Lua script has to be executed on the Redis side which locks the entire database.
func NewTokenBucketRedis(cli redis.UniversalClient, prefix string, ttl time.Duration, raceCheck bool) *TokenBucketRedis {
return &TokenBucketRedis{cli: cli, prefix: prefix, ttl: ttl, raceCheck: raceCheck}
}
// State gets the bucket's state from Redis.
func (t *TokenBucketRedis) State(ctx context.Context) (TokenBucketState, error) {
var values []interface{}
var err error
done := make(chan struct{}, 1)
if t.raceCheck {
// reset in a case of returning an empty TokenBucketState
t.lastVersion = 0
}
go func() {
defer close(done)
keys := []string{
redisKey(t.prefix, redisKeyTBLast),
redisKey(t.prefix, redisKeyTBAvailable),
}
if t.raceCheck {
keys = append(keys, redisKey(t.prefix, redisKeyTBVersion))
}
values, err = t.cli.MGet(ctx, keys...).Result()
}()
select {
case <-done:
case <-ctx.Done():
return TokenBucketState{}, ctx.Err()
}
if err != nil {
return TokenBucketState{}, errors.Wrap(err, "failed to get keys from redis")
}
nilAny := false
for _, v := range values {
if v == nil {
nilAny = true
break
}
}
if nilAny || err == redis.Nil {
// Keys don't exist, return the initial state.
return TokenBucketState{}, nil
}
last, err := strconv.ParseInt(values[0].(string), 10, 64)
if err != nil {
return TokenBucketState{}, err
}
available, err := strconv.ParseInt(values[1].(string), 10, 64)
if err != nil {
return TokenBucketState{}, err
}
if t.raceCheck {
t.lastVersion, err = strconv.ParseInt(values[2].(string), 10, 64)
if err != nil {
return TokenBucketState{}, err
}
}
return TokenBucketState{
Last: last,
Available: available,
}, nil
}
// SetState updates the state in Redis.
func (t *TokenBucketRedis) SetState(ctx context.Context, state TokenBucketState) error {
var err error
done := make(chan struct{}, 1)
go func() {
defer close(done)
if !t.raceCheck {
_, err = t.cli.TxPipelined(ctx, func(pipeliner redis.Pipeliner) error {
if err = pipeliner.Set(ctx, redisKey(t.prefix, redisKeyTBLast), state.Last, t.ttl).Err(); err != nil {
return err
}
return pipeliner.Set(ctx, redisKey(t.prefix, redisKeyTBAvailable), state.Available, t.ttl).Err()
})
return
}
var result interface{}
// TODO: use EVALSHA.
result, err = t.cli.Eval(ctx, `
local version = tonumber(redis.call('get', KEYS[1])) or 0
if version > tonumber(ARGV[1]) then
return 'RACE_CONDITION'
end
return {
redis.call('incr', KEYS[1]),
redis.call('pexpire', KEYS[1], ARGV[4]),
redis.call('set', KEYS[2], ARGV[2], 'PX', ARGV[4]),
redis.call('set', KEYS[3], ARGV[3], 'PX', ARGV[4]),
}
`, []string{
redisKey(t.prefix, redisKeyTBVersion),
redisKey(t.prefix, redisKeyTBLast),
redisKey(t.prefix, redisKeyTBAvailable),
},
t.lastVersion,
state.Last,
state.Available,
// TTL in milliseconds.
int64(t.ttl/time.Millisecond)).Result()
if err == nil {
err = checkResponseFromRedis(result, []interface{}{t.lastVersion + 1, int64(1), "OK", "OK"}, []interface{}{int64(1), int64(1), "OK", "OK"})
}
}()
select {
case <-done:
case <-ctx.Done():
return ctx.Err()
}
return errors.Wrap(err, "failed to save keys to redis")
}
// Reset resets the state in Redis.
func (t *TokenBucketRedis) Reset(ctx context.Context) error {
state := TokenBucketState{
Last: 0,
Available: 0,
}
return t.SetState(ctx, state)
}
// TokenBucketMemcached is a Memcached implementation of a TokenBucketStateBackend.
//
// Memcached is a distributed memory object caching system.
type TokenBucketMemcached struct {
cli *memcache.Client
key string
ttl time.Duration
raceCheck bool
casId uint64
}
// NewTokenBucketMemcached creates a new TokenBucketMemcached instance.
// Key is the key used to store all the keys used in this implementation in Memcached.
// TTL is the TTL of the stored keys.
//
// If raceCheck is true and the keys in Memcached are modified in between State() and SetState() calls then
// ErrRaceCondition is returned.
// This adds an extra overhead since a Lua script has to be executed on the Memcached side which locks the entire database.
func NewTokenBucketMemcached(cli *memcache.Client, key string, ttl time.Duration, raceCheck bool) *TokenBucketMemcached {
return &TokenBucketMemcached{cli: cli, key: key, ttl: ttl, raceCheck: raceCheck}
}
// State gets the bucket's state from Memcached.
func (t *TokenBucketMemcached) State(ctx context.Context) (TokenBucketState, error) {
var item *memcache.Item
var state TokenBucketState
var err error
done := make(chan struct{}, 1)
t.casId = 0
go func() {
defer close(done)
item, err = t.cli.Get(t.key)
}()
select {
case <-done:
case <-ctx.Done():
return state, ctx.Err()
}
if err != nil {
if errors.Is(err, memcache.ErrCacheMiss) {
// Keys don't exist, return the initial state.
return state, nil
}
return state, errors.Wrap(err, "failed to get key from memcached")
}
b := bytes.NewBuffer(item.Value)
err = gob.NewDecoder(b).Decode(&state)
if err != nil {
return state, errors.Wrap(err, "failed to Decode")
}
t.casId = item.CasID
return state, nil
}
// SetState updates the state in Memcached.
func (t *TokenBucketMemcached) SetState(ctx context.Context, state TokenBucketState) error {
var err error
done := make(chan struct{}, 1)
var b bytes.Buffer
err = gob.NewEncoder(&b).Encode(state)
if err != nil {
return errors.Wrap(err, "failed to Encode")
}
go func() {
defer close(done)
item := &memcache.Item{
Key: t.key,
Value: b.Bytes(),
CasID: t.casId,
}
if t.raceCheck && t.casId > 0 {
err = t.cli.CompareAndSwap(item)
} else {
err = t.cli.Set(item)
}
}()
select {
case <-done:
case <-ctx.Done():
return ctx.Err()
}
return errors.Wrap(err, "failed to save keys to memcached")
}
// Reset resets the state in Memcached.
func (t *TokenBucketMemcached) Reset(ctx context.Context) error {
state := TokenBucketState{
Last: 0,
Available: 0,
}
// Override casId to 0 to Set instead of CompareAndSwap in SetState
t.casId = 0
return t.SetState(ctx, state)
}
// TokenBucketDynamoDB is a DynamoDB implementation of a TokenBucketStateBackend.
type TokenBucketDynamoDB struct {
client *dynamodb.Client
tableProps DynamoDBTableProperties
partitionKey string
ttl time.Duration
raceCheck bool
latestVersion int64
keys map[string]types.AttributeValue
}
// NewTokenBucketDynamoDB creates a new TokenBucketDynamoDB instance.
// 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:
// * TTL
//
// TTL is the TTL of the stored item.
//
// If raceCheck is true and the item in DynamoDB are modified in between State() and SetState() calls then
// ErrRaceCondition is returned.
func NewTokenBucketDynamoDB(client *dynamodb.Client, partitionKey string, tableProps DynamoDBTableProperties, ttl time.Duration, raceCheck bool) *TokenBucketDynamoDB {
keys := map[string]types.AttributeValue{
tableProps.PartitionKeyName: &types.AttributeValueMemberS{Value: partitionKey},
}
if tableProps.SortKeyUsed {
keys[tableProps.SortKeyName] = &types.AttributeValueMemberS{Value: partitionKey}
}
return &TokenBucketDynamoDB{
client: client,
partitionKey: partitionKey,
tableProps: tableProps,
ttl: ttl,
raceCheck: raceCheck,
keys: keys,
}
}
// State gets the bucket's state from DynamoDB.
func (t *TokenBucketDynamoDB) State(ctx context.Context) (TokenBucketState, error) {
resp, err := dynamoDBGetItem(ctx, t.client, t.getGetItemInput())
if err != nil {
return TokenBucketState{}, err
}
return t.loadStateFromDynamoDB(resp)
}
// SetState updates the state in DynamoDB.
func (t *TokenBucketDynamoDB) SetState(ctx context.Context, state TokenBucketState) error {
input := t.getPutItemInputFromState(state)
var err error
done := make(chan struct{})
go func() {
defer close(done)
_, err = dynamoDBputItem(ctx, t.client, input)
}()
select {
case <-done:
case <-ctx.Done():
return ctx.Err()
}
return err
}
// Reset resets the state in DynamoDB.
func (t *TokenBucketDynamoDB) Reset(ctx context.Context) error {
state := TokenBucketState{
Last: 0,
Available: 0,
}
return t.SetState(ctx, state)
}
const dynamoDBBucketAvailableKey = "Available"
func (t *TokenBucketDynamoDB) getGetItemInput() *dynamodb.GetItemInput {
return &dynamodb.GetItemInput{
TableName: &t.tableProps.TableName,
Key: t.keys,
}
}
func (t *TokenBucketDynamoDB) getPutItemInputFromState(state TokenBucketState) *dynamodb.PutItemInput {
item := map[string]types.AttributeValue{}
for k, v := range t.keys {
item[k] = v
}
item[dynamoDBBucketLastKey] = &types.AttributeValueMemberN{Value: strconv.FormatInt(state.Last, 10)}
item[dynamoDBBucketVersionKey] = &types.AttributeValueMemberN{Value: strconv.FormatInt(t.latestVersion+1, 10)}
item[t.tableProps.TTLFieldName] = &types.AttributeValueMemberN{Value: strconv.FormatInt(time.Now().Add(t.ttl).Unix(), 10)}
item[dynamoDBBucketAvailableKey] = &types.AttributeValueMemberN{Value: strconv.FormatInt(state.Available, 10)}
input := &dynamodb.PutItemInput{
TableName: &t.tableProps.TableName,
Item: item,
}
if t.raceCheck && t.latestVersion > 0 {
input.ConditionExpression = aws.String(dynamodbBucketRaceConditionExpression)
input.ExpressionAttributeValues = map[string]types.AttributeValue{
":version": &types.AttributeValueMemberN{Value: strconv.FormatInt(t.latestVersion, 10)},
}
}
return input
}
func (t *TokenBucketDynamoDB) loadStateFromDynamoDB(resp *dynamodb.GetItemOutput) (TokenBucketState, error) {
state := TokenBucketState{}
err := attributevalue.Unmarshal(resp.Item[dynamoDBBucketLastKey], &state.Last)
if err != nil {
return state, fmt.Errorf("unmarshal dynamodb Last attribute failed: %w", err)
}
err = attributevalue.Unmarshal(resp.Item[dynamoDBBucketAvailableKey], &state.Available)
if err != nil {
return state, errors.Wrap(err, "unmarshal of dynamodb item attribute failed")
}
if t.raceCheck {
err = attributevalue.Unmarshal(resp.Item[dynamoDBBucketVersionKey], &t.latestVersion)
if err != nil {
return state, fmt.Errorf("unmarshal dynamodb Version attribute failed: %w", err)
}
}
return state, nil
}