forked from letsencrypt/boulder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
source.go
120 lines (104 loc) · 3.71 KB
/
source.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
package ratelimits
import (
"context"
"fmt"
"sync"
"time"
)
// ErrBucketNotFound indicates that the bucket was not found.
var ErrBucketNotFound = fmt.Errorf("bucket not found")
// source is an interface for creating and modifying TATs.
type source interface {
// BatchSet stores the TATs at the specified bucketKeys (formatted as
// 'name:id'). Implementations MUST ensure non-blocking operations by
// either:
// a) applying a deadline or timeout to the context WITHIN the method, or
// b) guaranteeing the operation will not block indefinitely (e.g. via
// the underlying storage client implementation).
BatchSet(ctx context.Context, bucketKeys map[string]time.Time) error
// BatchIncrement updates the TATs for the specified bucketKeys, similar to
// BatchSet. Implementations MUST ensure non-blocking operations by either:
// a) applying a deadline or timeout to the context WITHIN the method, or
// b) guaranteeing the operation will not block indefinitely (e.g. via
// the underlying storage client implementation).
BatchIncrement(ctx context.Context, buckets map[string]increment) error
// Get retrieves the TAT associated with the specified bucketKey (formatted
// as 'name:id'). Implementations MUST ensure non-blocking operations by
// either:
// a) applying a deadline or timeout to the context WITHIN the method, or
// b) guaranteeing the operation will not block indefinitely (e.g. via
// the underlying storage client implementation).
Get(ctx context.Context, bucketKey string) (time.Time, error)
// BatchGet retrieves the TATs associated with the specified bucketKeys
// (formatted as 'name:id'). Implementations MUST ensure non-blocking
// operations by either:
// a) applying a deadline or timeout to the context WITHIN the method, or
// b) guaranteeing the operation will not block indefinitely (e.g. via
// the underlying storage client implementation).
BatchGet(ctx context.Context, bucketKeys []string) (map[string]time.Time, error)
// Delete removes the TAT associated with the specified bucketKey (formatted
// as 'name:id'). Implementations MUST ensure non-blocking operations by
// either:
// a) applying a deadline or timeout to the context WITHIN the method, or
// b) guaranteeing the operation will not block indefinitely (e.g. via
// the underlying storage client implementation).
Delete(ctx context.Context, bucketKey string) error
}
type increment struct {
cost time.Duration
ttl time.Duration
}
// inmem is an in-memory implementation of the source interface used for
// testing.
type inmem struct {
sync.RWMutex
m map[string]time.Time
}
var _ source = (*inmem)(nil)
func newInmem() *inmem {
return &inmem{m: make(map[string]time.Time)}
}
func (in *inmem) BatchSet(_ context.Context, bucketKeys map[string]time.Time) error {
in.Lock()
defer in.Unlock()
for k, v := range bucketKeys {
in.m[k] = v
}
return nil
}
func (in *inmem) BatchIncrement(_ context.Context, bucketKeys map[string]increment) error {
in.Lock()
defer in.Unlock()
for k, v := range bucketKeys {
in.m[k] = in.m[k].Add(v.cost)
}
return nil
}
func (in *inmem) Get(_ context.Context, bucketKey string) (time.Time, error) {
in.RLock()
defer in.RUnlock()
tat, ok := in.m[bucketKey]
if !ok {
return time.Time{}, ErrBucketNotFound
}
return tat, nil
}
func (in *inmem) BatchGet(_ context.Context, bucketKeys []string) (map[string]time.Time, error) {
in.RLock()
defer in.RUnlock()
tats := make(map[string]time.Time, len(bucketKeys))
for _, k := range bucketKeys {
tat, ok := in.m[k]
if !ok {
continue
}
tats[k] = tat
}
return tats, nil
}
func (in *inmem) Delete(_ context.Context, bucketKey string) error {
in.Lock()
defer in.Unlock()
delete(in.m, bucketKey)
return nil
}