generated from denpeshkov/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rate.go
155 lines (136 loc) · 4.39 KB
/
rate.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
package throttle
import (
"context"
_ "embed"
"errors"
"fmt"
"math"
"strings"
"time"
)
// Inf is the infinite duration.
const Inf = time.Duration(math.MaxInt64)
var (
errInvalidInterval = errors.New("limit interval below 1 ms")
errInvalidEvents = errors.New("limit events is negative")
)
// Rediser defines an interface to abstract a Redis client.
type Rediser interface {
// ScriptLoad preloads a Lua script into Redis and returns its SHA-1 hash.
ScriptLoad(ctx context.Context, script string) (string, error)
// EvalSHA executes a preloaded Lua script using its SHA-1 hash.
EvalSHA(ctx context.Context, sha1 string, keys []string, args ...any) (any, error)
// Del removes the specified keys. A key is ignored if it does not exist.
Del(ctx context.Context, keys ...string) (int64, error)
}
// Limit defines the maximum number of events allowed within a specified time interval.
// Setting Events to zero disallows all events. Interval must be at least 1 millisecond.
type Limit struct {
Events int
Interval time.Duration
}
func (l Limit) String() string {
return fmt.Sprintf("%d req in %s", l.Events, l.Interval.String())
}
// Status represents the result of evaluating the rate limit.
type Status struct {
// Limited indicates whether the current event was limited.
Limited bool
// Remaining specifies the number of events left in the current limit window.
Remaining int
// Delay is the duration until the next event is permitted.
// A zero duration means the event can occur immediately.
// An [Inf] duration indicates that no events are allowed.
Delay time.Duration
}
func (s Status) String() string {
d := s.Delay.String()
if s.Delay == Inf {
d = "Inf"
}
l := "unlimited"
if s.Limited {
l = "limited"
}
return fmt.Sprintf("(%s, %d req, %s)", l, s.Remaining, d)
}
//go:embed allow.lua
var luaScript string
// A Limiter controls how frequently events are allowed to happen.
// Limiter works with 1ms resolution.
type Limiter struct {
rds Rediser
scriptSHA1 string
key string
lim Limit
}
// NewLimiter returns a new [Limiter] for the given key that allows events up to the specified limit.
// Creating multiple [Limiter] instances for the same key with different limits may violate limits.
// It implements a "sliding window log" algorithm backed by [Redis].
//
// [Redis]: https://redis.io
func NewLimiter(rds Rediser, key string, limit Limit) (*Limiter, error) {
if limit.Interval.Milliseconds() <= 0 {
return nil, errInvalidInterval
}
if limit.Events < 0 {
return nil, errInvalidEvents
}
return &Limiter{rds: rds, scriptSHA1: "", key: key, lim: limit}, nil
}
// Allow returns the result of evaluating whether the event can occur now.
func (l *Limiter) Allow(ctx context.Context) (*Status, error) {
return l.allowAt(ctx, time.Now(), 2*l.lim.Interval)
}
func (l *Limiter) allowAt(ctx context.Context, now time.Time, keyTTL time.Duration) (*Status, error) {
if l.lim.Events == 0 {
return &Status{Limited: true, Remaining: 0, Delay: Inf}, nil
}
keys := []string{l.key}
args := []any{l.lim.Events, l.lim.Interval.Milliseconds(), now.UnixMilli(), keyTTL.Milliseconds()}
v, err := l.execScript(ctx, keys, args...)
if err != nil {
return nil, err
}
values := v.([]interface{})
return &Status{
Limited: values[0].(int64) != 0,
Remaining: int(values[1].(int64)),
Delay: time.Duration(values[2].(int64)) * time.Millisecond,
}, nil
}
func (l *Limiter) execScript(ctx context.Context, keys []string, args ...any) (any, error) {
v, err := l.rds.EvalSHA(ctx, l.scriptSHA1, keys, args...)
if err != nil && strings.HasPrefix(err.Error(), "NOSCRIPT") {
var sha1 string
if sha1, err = l.rds.ScriptLoad(ctx, luaScript); err == nil {
l.scriptSHA1 = sha1
v, err = l.rds.EvalSHA(ctx, l.scriptSHA1, keys, args...)
}
}
if err != nil {
return nil, err
}
return v, nil
}
// Limit returns the current limit.
func (l *Limiter) Limit() Limit {
return l.lim
}
// SetLimit sets a new [Limit] for the limiter.
// Events from the previous limit are still applied to the new limit.
func (l *Limiter) SetLimit(_ context.Context, newLimit Limit) error {
if newLimit.Interval.Milliseconds() <= 0 {
return errInvalidInterval
}
if newLimit.Events < 0 {
return errInvalidEvents
}
l.lim = newLimit
return nil
}
// Reset clears all limitations and previous usage of the limiter.
func (l *Limiter) Reset(ctx context.Context) error {
_, err := l.rds.Del(ctx, l.key)
return err
}