-
Notifications
You must be signed in to change notification settings - Fork 62
/
new.go
234 lines (194 loc) · 5.59 KB
/
new.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
// SPDX-License-Identifier: AGPL-3.0-only
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>
package rate
import (
"context"
_ "embed"
"fmt"
"strconv"
"time"
"github.com/go-redis/redis/v8"
"github.com/bangumi/server/config"
"github.com/bangumi/server/internal/model"
"github.com/bangumi/server/internal/pkg/errgo"
"github.com/bangumi/server/internal/pkg/gtime"
"github.com/bangumi/server/web/rate/action"
)
const keyPrefix = "chii:rate:" + config.Version
const defaultAllowPerHour = 5
//go:embed allow.lua
var allowLua string
var allowScript = redis.NewScript(allowLua) //nolint:gochecknoglobals
type Manager interface {
// Login 检查是登录限流。
Login(ctx context.Context, ip string) (allowed bool, remain int, err error)
AllowAction(
ctx context.Context,
u model.UserID,
action action.Action,
limit Limit,
) (allowed bool, remain int, err error)
Reset(ctx context.Context, ip string) error
}
const RedisRateKeyPrefix = "chii:login:rate:"
const RedisBanKeyPrefix = "chii:ban:"
func New(rdb *redis.Client) Manager {
return manager{
r: rdb,
}
}
type manager struct {
r *redis.Client
}
func (m manager) AllowAction(
ctx context.Context,
u model.UserID,
action action.Action,
limit Limit,
) (bool, int, error) {
rateKey := fmt.Sprintf("%s:%d:%d", keyPrefix, action, u)
banKey := fmt.Sprintf("%s:ban:%d:%d", keyPrefix, action, u)
res, err := m.allow(ctx, rateKey, banKey, limit)
if err != nil {
return false, 0, errgo.Wrap(err, "Limiter.Allow")
}
return res.Allowed > 0, res.Remaining, nil
}
func (m manager) Login(ctx context.Context, ip string) (bool, int, error) {
var rateKey = RedisRateKeyPrefix + ip
var banKey = RedisBanKeyPrefix + ip
res, err := m.allow(ctx, rateKey, banKey, PerHour(defaultAllowPerHour))
if err != nil {
return false, 0, errgo.Wrap(err, "Limiter.Allow")
}
return res.Allowed > 0, res.Remaining, nil
}
func (m manager) Reset(ctx context.Context, ip string) error {
err := m.r.Del(ctx, RedisRateKeyPrefix+ip, RedisBanKeyPrefix+ip).Err()
return errgo.Wrap(err, "Limiter.Allow")
}
// AllowN reports whether n events may happen at time now.
func (m manager) allow(
ctx context.Context,
rateKey string,
banKey string,
limit Limit,
) (Result, error) {
now := time.Now()
var keys = []string{rateKey, banKey}
scriptValues := []any{
limit.Burst, limit.Rate, limit.Period.Seconds(), now.Unix(), now.Nanosecond() / 1000, gtime.OneWeekSec,
}
v, err := allowScript.Run(ctx, m.r, keys, scriptValues...).Result()
if err != nil {
return Result{}, errgo.Wrap(err, "luaScript.Run")
}
values, ok := v.([]any)
if !ok {
panic("failed to cast redis lua result type")
}
retryAfter, err := strconv.ParseFloat(values[2].(string), 64)
if err != nil {
return Result{}, errgo.Wrap(err, "strconv.ParseFloat")
}
resetAfter, err := strconv.ParseFloat(values[3].(string), 64)
if err != nil {
return Result{}, errgo.Wrap(err, "strconv.ParseFloat")
}
allowed, ok := values[0].(int64)
if !ok {
panic("can't convert redis result 'allowed' to int64")
}
remaining, ok := values[1].(int64)
if !ok {
panic("can't convert redis result 'remaining' to int64")
}
return Result{
Limit: limit,
Allowed: int(allowed),
Remaining: int(remaining),
RetryAfter: dur(retryAfter),
ResetAfter: dur(resetAfter),
}, nil
}
type Limit struct {
Rate int
Burst int
Period time.Duration
}
func (l Limit) String() string {
return fmt.Sprintf("%d req/%s (burst %d)", l.Rate, fmtDur(l.Period), l.Burst)
}
func (l Limit) IsZero() bool {
return l == Limit{}
}
func fmtDur(d time.Duration) string {
switch d { //nolint:exhaustive
case time.Second:
return "s"
case time.Minute:
return "m"
case time.Hour:
return "h"
}
return d.String()
}
func PerSecond(rate int) Limit {
return Limit{
Rate: rate,
Period: time.Second,
Burst: rate,
}
}
func PerMinute(rate int) Limit {
return Limit{
Rate: rate,
Period: time.Minute,
Burst: rate,
}
}
func PerHour(rate int) Limit {
return Limit{
Rate: rate,
Period: time.Hour,
Burst: rate,
}
}
func dur(f float64) time.Duration {
if f == -1 {
return -1
}
return time.Duration(f * float64(time.Second))
}
type Result struct {
// Limit is the limit that was used to obtain this result.
Limit Limit
// Allowed is the number of events that may happen at time now.
Allowed int
// Remaining is the maximum number of requests that could be
// permitted instantaneously for this key given the current
// state. For example, if a rate limiter allows 10 requests per
// second and has already received 6 requests for this key this
// second, Remaining would be 4.
Remaining int
// RetryAfter is the time until the next request will be permitted.
// It should be -1 unless the rate limit has been exceeded.
RetryAfter time.Duration
// ResetAfter is the time until the RateLimiter returns to its
// initial state for a given key. For example, if a rate limiter
// manages requests per second and received one request 200ms ago,
// Reset would return 800ms. You can also think of this as the time
// until Limit and Remaining will be equal.
ResetAfter time.Duration
}