-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswlog.go
More file actions
116 lines (100 loc) · 2.5 KB
/
Copy pathswlog.go
File metadata and controls
116 lines (100 loc) · 2.5 KB
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
package throttle
import (
"context"
"crypto/sha1" //nolint:gosec
_ "embed" // embed lua script
"encoding/hex"
"io"
"sync"
"time"
)
//go:embed swlog.lua
var swLogScript string
// SWLogLimiter implements a rate limiter using the Sliding-Window Log algorithm.
// It works with a 1 ms resolution.
type SWLogLimiter struct {
rds Rediser
script script
keyTTL time.Duration
clock func() time.Time
mu sync.Mutex
lim Limit
}
// NewSWLogLimiter returns a new configured [SWLogLimiter].
func NewSWLogLimiter(rds Rediser, limit Limit, opts ...Option) (*SWLogLimiter, error) {
if err := limit.Valid(); err != nil {
return nil, err
}
options := options{
keyTTL: 1 * time.Second,
clock: time.Now,
}
for _, o := range opts {
o.apply(&options)
}
h := sha1.New() //nolint:gosec
_, _ = io.WriteString(h, swLogScript)
script := script{
rds: rds,
script: swLogScript,
sha: hex.EncodeToString(h.Sum(nil)),
}
return &SWLogLimiter{
rds: rds,
script: script,
keyTTL: options.keyTTL,
clock: options.clock,
lim: limit,
mu: sync.Mutex{},
}, nil
}
// Allow determines whether the event for the specified key is permitted at the current time.
//
//nolint:forcetypeassert
func (l *SWLogLimiter) Allow(ctx context.Context, key string) (Status, error) {
l.mu.Lock()
lim := l.lim
now := l.clock()
ttl := max(lim.Interval+l.keyTTL, lim.Interval)
l.mu.Unlock()
if lim.Events == 0 {
return Status{Limited: true, Remaining: 0, Delay: Inf}, nil
}
keys := []string{key}
args := []any{lim.Events, lim.Interval.Milliseconds(), now.UTC().UnixMilli(), ttl.Milliseconds()}
v, err := l.script.exec(ctx, keys, args...)
if err != nil {
return Status{}, err
}
values := v.([]any)
return Status{
Limited: values[0].(int64) != 0,
Remaining: int(values[1].(int64)),
Delay: time.Duration(values[2].(int64)) * time.Millisecond, //nolint:durationcheck
}, nil
}
// Limit returns the current limit.
func (l *SWLogLimiter) Limit() Limit {
l.mu.Lock()
defer l.mu.Unlock()
return l.lim
}
// SetLimit sets a new limit.
func (l *SWLogLimiter) SetLimit(_ context.Context, newLimit Limit) error {
if err := newLimit.Valid(); err != nil {
return err
}
l.mu.Lock()
defer l.mu.Unlock()
l.lim = newLimit
return nil
}
// Reset clears all limitations and previous usage for the specified keys.
// If no keys are provided, it's a no-op.
func (l *SWLogLimiter) Reset(ctx context.Context, keys ...string) error {
if len(keys) == 0 {
return nil
}
_, err := l.rds.Del(ctx, keys...)
return err
}