/
multi.ts
496 lines (443 loc) · 15.6 KB
/
multi.ts
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
import { Cache } from "./cache";
import type { Duration } from "./duration";
import { ms } from "./duration";
import {
fixedWindowLimitScript,
fixedWindowRemainingTokensScript,
slidingWindowLimitScript,
slidingWindowRemainingTokensScript,
} from "./lua-scripts/multi";
import { resetScript } from "./lua-scripts/reset";
import { Ratelimit } from "./ratelimit";
import type { Algorithm, MultiRegionContext } from "./types";
import type { Redis } from "./types";
function randomId(): string {
let result = "";
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
const charactersLength = characters.length;
for (let i = 0; i < 16; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
export type MultiRegionRatelimitConfig = {
/**
* Instances of `@upstash/redis`
* @see https://github.com/upstash/upstash-redis#quick-start
*/
redis: Redis[];
/**
* The ratelimiter function to use.
*
* Choose one of the predefined ones or implement your own.
* Available algorithms are exposed via static methods:
* - MultiRegionRatelimit.fixedWindow
*/
limiter: Algorithm<MultiRegionContext>;
/**
* All keys in redis are prefixed with this.
*
* @default `@upstash/ratelimit`
*/
prefix?: string;
/**
* If enabled, the ratelimiter will keep a global cache of identifiers, that have
* exhausted their ratelimit. In serverless environments this is only possible if
* you create the ratelimiter instance outside of your handler function. While the
* function is still hot, the ratelimiter can block requests without having to
* request data from redis, thus saving time and money.
*
* Whenever an identifier has exceeded its limit, the ratelimiter will add it to an
* internal list together with its reset timestamp. If the same identifier makes a
* new request before it is reset, we can immediately reject it.
*
* Set to `false` to disable.
*
* If left undefined, a map is created automatically, but it can only work
* if the map or the ratelimit instance is created outside your serverless function handler.
*/
ephemeralCache?: Map<string, number> | false;
/**
* If set, the ratelimiter will allow requests to pass after this many milliseconds.
*
* Use this if you want to allow requests in case of network problems
*/
timeout?: number;
/**
* If enabled, the ratelimiter will store analytics data in redis, which you can check out at
* https://console.upstash.com/ratelimit
*
* @default false
*/
analytics?: boolean;
};
/**
* Ratelimiter using serverless redis from https://upstash.com/
*
* @example
* ```ts
* const { limit } = new MultiRegionRatelimit({
* redis: Redis.fromEnv(),
* limiter: MultiRegionRatelimit.fixedWindow(
* 10, // Allow 10 requests per window of 30 minutes
* "30 m", // interval of 30 minutes
* )
* })
*
* ```
*/
export class MultiRegionRatelimit extends Ratelimit<MultiRegionContext> {
/**
* Create a new Ratelimit instance by providing a `@upstash/redis` instance and the algorithn of your choice.
*/
constructor(config: MultiRegionRatelimitConfig) {
super({
prefix: config.prefix,
limiter: config.limiter,
timeout: config.timeout,
analytics: config.analytics,
ctx: {
redis: config.redis,
cache: config.ephemeralCache ? new Cache(config.ephemeralCache) : undefined,
},
});
}
/**
* Each request inside a fixed time increases a counter.
* Once the counter reaches the maximum allowed number, all further requests are
* rejected.
*
* **Pro:**
*
* - Newer requests are not starved by old ones.
* - Low storage cost.
*
* **Con:**
*
* A burst of requests near the boundary of a window can result in a very
* high request rate because two windows will be filled with requests quickly.
*
* @param tokens - How many requests a user can make in each time window.
* @param window - A fixed timeframe
*/
static fixedWindow(
/**
* How many requests are allowed per window.
*/
tokens: number,
/**
* The duration in which `tokens` requests are allowed.
*/
window: Duration,
): Algorithm<MultiRegionContext> {
const windowDuration = ms(window);
return () => ({
async limit(ctx: MultiRegionContext, identifier: string, rate?: number) {
if (ctx.cache) {
const { blocked, reset } = ctx.cache.isBlocked(identifier);
if (blocked) {
return {
success: false,
limit: tokens,
remaining: 0,
reset: reset,
pending: Promise.resolve(),
};
}
}
const requestId = randomId();
const bucket = Math.floor(Date.now() / windowDuration);
const key = [identifier, bucket].join(":");
const incrementBy = rate ? Math.max(1, rate) : 1;
const dbs: { redis: Redis; request: Promise<string[]> }[] = ctx.redis.map((redis) => ({
redis,
request: redis.eval(
fixedWindowLimitScript,
[key],
[requestId, windowDuration, incrementBy],
) as Promise<string[]>,
}));
// The firstResponse is an array of string at every EVEN indexes and rate at which the tokens are used at every ODD indexes
const firstResponse = await Promise.any(dbs.map((s) => s.request));
const usedTokens = firstResponse.reduce((accTokens: number, usedToken, index) => {
let parsedToken = 0;
if (index % 2) {
parsedToken = Number.parseInt(usedToken);
}
return accTokens + parsedToken;
}, 0);
const remaining = tokens - usedTokens;
/**
* If the length between two databases does not match, we sync the two databases
*/
async function sync() {
const individualIDs = await Promise.all(dbs.map((s) => s.request));
const allIDs = Array.from(
new Set(
individualIDs
.flatMap((_) => _)
.reduce((acc: string[], curr, index) => {
if (index % 2 === 0) {
acc.push(curr);
}
return acc;
}, []),
).values(),
);
for (const db of dbs) {
const usedDbTokens = (await db.request).reduce(
(accTokens: number, usedToken, index) => {
let parsedToken = 0;
if (index % 2) {
parsedToken = Number.parseInt(usedToken);
}
return accTokens + parsedToken;
},
0,
);
const dbIds = (await db.request).reduce((ids: string[], currentId, index) => {
if (index % 2 === 0) {
ids.push(currentId);
}
return ids;
}, []);
/**
* If the bucket in this db is already full, it doesn't matter which ids it contains.
* So we do not have to sync.
*/
if (usedDbTokens >= tokens) {
continue;
}
const diff = allIDs.filter((id) => !dbIds.includes(id));
/**
* Don't waste a request if there is nothing to send
*/
if (diff.length === 0) {
continue;
}
for (const requestId of diff) {
await db.redis.hset(key, { [requestId]: incrementBy });
}
}
}
/**
* Do not await sync. This should not run in the critical path.
*/
const success = remaining > 0;
const reset = (bucket + 1) * windowDuration;
if (ctx.cache && !success) {
ctx.cache.blockUntil(identifier, reset);
}
return {
success,
limit: tokens,
remaining,
reset,
pending: sync(),
};
},
async getRemaining(ctx: MultiRegionContext, identifier: string) {
const bucket = Math.floor(Date.now() / windowDuration);
const key = [identifier, bucket].join(":");
const dbs: { redis: Redis; request: Promise<string[]> }[] = ctx.redis.map((redis) => ({
redis,
request: redis.eval(fixedWindowRemainingTokensScript, [key], [null]) as Promise<string[]>,
}));
// The firstResponse is an array of string at every EVEN indexes and rate at which the tokens are used at every ODD indexes
const firstResponse = await Promise.any(dbs.map((s) => s.request));
const usedTokens = firstResponse.reduce((accTokens: number, usedToken, index) => {
let parsedToken = 0;
if (index % 2) {
parsedToken = Number.parseInt(usedToken);
}
return accTokens + parsedToken;
}, 0);
return Math.max(0, tokens - usedTokens);
},
async resetTokens(ctx: MultiRegionContext, identifier: string) {
const pattern = [identifier, "*"].join(":");
if (ctx.cache) {
ctx.cache.pop(identifier)
}
for (const db of ctx.redis) {
await db.eval(resetScript, [pattern], [null]);
}
},
});
}
/**
* Combined approach of `slidingLogs` and `fixedWindow` with lower storage
* costs than `slidingLogs` and improved boundary behavior by calculating a
* weighted score between two windows.
*
* **Pro:**
*
* Good performance allows this to scale to very high loads.
*
* **Con:**
*
* Nothing major.
*
* @param tokens - How many requests a user can make in each time window.
* @param window - The duration in which the user can max X requests.
*/
static slidingWindow(
/**
* How many requests are allowed per window.
*/
tokens: number,
/**
* The duration in which `tokens` requests are allowed.
*/
window: Duration,
): Algorithm<MultiRegionContext> {
const windowSize = ms(window);
const windowDuration = ms(window);
return () => ({
async limit(ctx: MultiRegionContext, identifier: string, rate?: number) {
// if (ctx.cache) {
// const { blocked, reset } = ctx.cache.isBlocked(identifier);
// if (blocked) {
// return {
// success: false,
// limit: tokens,
// remaining: 0,
// reset: reset,
// pending: Promise.resolve(),
// };
// }
// }
const requestId = randomId();
const now = Date.now();
const currentWindow = Math.floor(now / windowSize);
const currentKey = [identifier, currentWindow].join(":");
const previousWindow = currentWindow - 1;
const previousKey = [identifier, previousWindow].join(":");
const incrementBy = rate ? Math.max(1, rate) : 1;
const dbs = ctx.redis.map((redis) => ({
redis,
request: redis.eval(
slidingWindowLimitScript,
[currentKey, previousKey],
[tokens, now, windowDuration, requestId, incrementBy],
// lua seems to return `1` for true and `null` for false
) as Promise<[string[], string[], 1 | null]>,
}));
const percentageInCurrent = (now % windowDuration) / windowDuration;
const [current, previous, success] = await Promise.any(dbs.map((s) => s.request));
// in the case of success, the new request is not included in the current array.
// add it manually
if (success) {
current.push(requestId, incrementBy.toString())
}
const previousUsedTokens = previous.reduce((accTokens: number, usedToken, index) => {
let parsedToken = 0;
if (index % 2) {
parsedToken = Number.parseInt(usedToken);
}
return accTokens + parsedToken;
}, 0);
const currentUsedTokens = current.reduce((accTokens: number, usedToken, index) => {
let parsedToken = 0;
if (index % 2) {
parsedToken = Number.parseInt(usedToken);
}
return accTokens + parsedToken;
}, 0);
const previousPartialUsed = Math.ceil(previousUsedTokens * (1 - percentageInCurrent));
const usedTokens = previousPartialUsed + currentUsedTokens;
const remaining = tokens - usedTokens;
/**
* If a database differs from the consensus, we sync it
*/
async function sync() {
const res = await Promise.all(dbs.map((s) => s.request));
const allCurrentIds = Array.from(
new Set(
res
.flatMap(([current]) => current)
.reduce((acc: string[], curr, index) => {
if (index % 2 === 0) {
acc.push(curr);
}
return acc;
}, []),
).values(),
);
for (const db of dbs) {
const [current, _previous, _success] = await db.request;
const dbIds = current.reduce((ids: string[], currentId, index) => {
if (index % 2 === 0) {
ids.push(currentId);
}
return ids;
}, []);
const usedDbTokens = current.reduce((accTokens: number, usedToken, index) => {
let parsedToken = 0;
if (index % 2) {
parsedToken = Number.parseInt(usedToken);
}
return accTokens + parsedToken;
}, 0);
/**
* If the bucket in this db is already full, it doesn't matter which ids it contains.
* So we do not have to sync.
*/
if (usedDbTokens >= tokens) {
continue;
}
const diff = allCurrentIds.filter((id) => !dbIds.includes(id));
/**
* Don't waste a request if there is nothing to send
*/
if (diff.length === 0) {
continue;
}
for (const requestId of diff) {
await db.redis.hset(currentKey, { [requestId]: incrementBy });
}
}
}
// const success = remaining >= 0;
const reset = (currentWindow + 1) * windowDuration;
if (ctx.cache && !success) {
ctx.cache.blockUntil(identifier, reset);
}
return {
success: Boolean(success),
limit: tokens,
remaining: Math.max(0, remaining),
reset,
pending: sync(),
};
},
async getRemaining(ctx: MultiRegionContext, identifier: string) {
const now = Date.now();
const currentWindow = Math.floor(now / windowSize);
const currentKey = [identifier, currentWindow].join(":");
const previousWindow = currentWindow - 1;
const previousKey = [identifier, previousWindow].join(":");
const dbs = ctx.redis.map((redis) => ({
redis,
request: redis.eval(
slidingWindowRemainingTokensScript,
[currentKey, previousKey],
[now, windowSize],
// lua seems to return `1` for true and `null` for false
) as Promise<number>,
}));
const usedTokens = await Promise.any(dbs.map((s) => s.request));
return Math.max(0, tokens - usedTokens);
},
async resetTokens(ctx: MultiRegionContext, identifier: string) {
const pattern = [identifier, "*"].join(":");
if (ctx.cache) {
ctx.cache.pop(identifier)
}
for (const db of ctx.redis) {
await db.eval(resetScript, [pattern], [null]);
}
},
});
}
}