@upstash/ratelimit 0.4.0 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -3
- package/dist/index.d.ts +137 -137
- package/dist/index.js +248 -248
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +248 -248
- package/dist/index.mjs.map +1 -1
- package/package.json +12 -12
package/dist/index.js
CHANGED
|
@@ -26,30 +26,6 @@ __export(src_exports, {
|
|
|
26
26
|
});
|
|
27
27
|
module.exports = __toCommonJS(src_exports);
|
|
28
28
|
|
|
29
|
-
// src/duration.ts
|
|
30
|
-
function ms(d) {
|
|
31
|
-
const match = d.match(/^(\d+)\s?(ms|s|m|h|d)$/);
|
|
32
|
-
if (!match) {
|
|
33
|
-
throw new Error(`Unable to parse window size: ${d}`);
|
|
34
|
-
}
|
|
35
|
-
const time = parseInt(match[1]);
|
|
36
|
-
const unit = match[2];
|
|
37
|
-
switch (unit) {
|
|
38
|
-
case "ms":
|
|
39
|
-
return time;
|
|
40
|
-
case "s":
|
|
41
|
-
return time * 1e3;
|
|
42
|
-
case "m":
|
|
43
|
-
return time * 1e3 * 60;
|
|
44
|
-
case "h":
|
|
45
|
-
return time * 1e3 * 60 * 60;
|
|
46
|
-
case "d":
|
|
47
|
-
return time * 1e3 * 60 * 60 * 24;
|
|
48
|
-
default:
|
|
49
|
-
throw new Error(`Unable to parse window size: ${d}`);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
29
|
// src/analytics.ts
|
|
54
30
|
var import_core_analytics = require("@upstash/core-analytics");
|
|
55
31
|
var Analytics = class {
|
|
@@ -147,6 +123,30 @@ var Cache = class {
|
|
|
147
123
|
}
|
|
148
124
|
};
|
|
149
125
|
|
|
126
|
+
// src/duration.ts
|
|
127
|
+
function ms(d) {
|
|
128
|
+
const match = d.match(/^(\d+)\s?(ms|s|m|h|d)$/);
|
|
129
|
+
if (!match) {
|
|
130
|
+
throw new Error(`Unable to parse window size: ${d}`);
|
|
131
|
+
}
|
|
132
|
+
const time = parseInt(match[1]);
|
|
133
|
+
const unit = match[2];
|
|
134
|
+
switch (unit) {
|
|
135
|
+
case "ms":
|
|
136
|
+
return time;
|
|
137
|
+
case "s":
|
|
138
|
+
return time * 1e3;
|
|
139
|
+
case "m":
|
|
140
|
+
return time * 1e3 * 60;
|
|
141
|
+
case "h":
|
|
142
|
+
return time * 1e3 * 60 * 60;
|
|
143
|
+
case "d":
|
|
144
|
+
return time * 1e3 * 60 * 60 * 24;
|
|
145
|
+
default:
|
|
146
|
+
throw new Error(`Unable to parse window size: ${d}`);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
150
|
// src/ratelimit.ts
|
|
151
151
|
var Ratelimit = class {
|
|
152
152
|
limiter;
|
|
@@ -159,7 +159,7 @@ var Ratelimit = class {
|
|
|
159
159
|
this.limiter = config.limiter;
|
|
160
160
|
this.timeout = config.timeout ?? 5e3;
|
|
161
161
|
this.prefix = config.prefix ?? "@upstash/ratelimit";
|
|
162
|
-
this.analytics = config.analytics
|
|
162
|
+
this.analytics = config.analytics ? new Analytics({
|
|
163
163
|
redis: Array.isArray(this.ctx.redis) ? this.ctx.redis[0] : this.ctx.redis,
|
|
164
164
|
prefix: this.prefix
|
|
165
165
|
}) : void 0;
|
|
@@ -235,7 +235,7 @@ var Ratelimit = class {
|
|
|
235
235
|
/**
|
|
236
236
|
* Block until the request may pass or timeout is reached.
|
|
237
237
|
*
|
|
238
|
-
* This method returns a
|
|
238
|
+
* This method returns a promise that resolves as soon as the request may be processed
|
|
239
239
|
* or after the timeoue has been reached.
|
|
240
240
|
*
|
|
241
241
|
* Use this if you want to delay the request until it is ready to get processed.
|
|
@@ -278,6 +278,224 @@ var Ratelimit = class {
|
|
|
278
278
|
};
|
|
279
279
|
};
|
|
280
280
|
|
|
281
|
+
// src/multi.ts
|
|
282
|
+
function randomId() {
|
|
283
|
+
let result = "";
|
|
284
|
+
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
285
|
+
const charactersLength = characters.length;
|
|
286
|
+
for (let i = 0; i < 16; i++) {
|
|
287
|
+
result += characters.charAt(Math.floor(Math.random() * charactersLength));
|
|
288
|
+
}
|
|
289
|
+
return result;
|
|
290
|
+
}
|
|
291
|
+
var MultiRegionRatelimit = class extends Ratelimit {
|
|
292
|
+
/**
|
|
293
|
+
* Create a new Ratelimit instance by providing a `@upstash/redis` instance and the algorithn of your choice.
|
|
294
|
+
*/
|
|
295
|
+
constructor(config) {
|
|
296
|
+
super({
|
|
297
|
+
prefix: config.prefix,
|
|
298
|
+
limiter: config.limiter,
|
|
299
|
+
timeout: config.timeout,
|
|
300
|
+
analytics: config.analytics,
|
|
301
|
+
ctx: {
|
|
302
|
+
redis: config.redis,
|
|
303
|
+
cache: config.ephemeralCache ? new Cache(config.ephemeralCache) : void 0
|
|
304
|
+
}
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Each requests inside a fixed time increases a counter.
|
|
309
|
+
* Once the counter reaches a maxmimum allowed number, all further requests are
|
|
310
|
+
* rejected.
|
|
311
|
+
*
|
|
312
|
+
* **Pro:**
|
|
313
|
+
*
|
|
314
|
+
* - Newer requests are not starved by old ones.
|
|
315
|
+
* - Low storage cost.
|
|
316
|
+
*
|
|
317
|
+
* **Con:**
|
|
318
|
+
*
|
|
319
|
+
* A burst of requests near the boundary of a window can result in a very
|
|
320
|
+
* high request rate because two windows will be filled with requests quickly.
|
|
321
|
+
*
|
|
322
|
+
* @param tokens - How many requests a user can make in each time window.
|
|
323
|
+
* @param window - A fixed timeframe
|
|
324
|
+
*/
|
|
325
|
+
static fixedWindow(tokens, window) {
|
|
326
|
+
const windowDuration = ms(window);
|
|
327
|
+
const script = `
|
|
328
|
+
local key = KEYS[1]
|
|
329
|
+
local id = ARGV[1]
|
|
330
|
+
local window = ARGV[2]
|
|
331
|
+
|
|
332
|
+
redis.call("SADD", key, id)
|
|
333
|
+
local members = redis.call("SMEMBERS", key)
|
|
334
|
+
if #members == 1 then
|
|
335
|
+
-- The first time this key is set, the value will be 1.
|
|
336
|
+
-- So we only need the expire command once
|
|
337
|
+
redis.call("PEXPIRE", key, window)
|
|
338
|
+
end
|
|
339
|
+
|
|
340
|
+
return members
|
|
341
|
+
`;
|
|
342
|
+
return async function(ctx, identifier) {
|
|
343
|
+
if (ctx.cache) {
|
|
344
|
+
const { blocked, reset: reset2 } = ctx.cache.isBlocked(identifier);
|
|
345
|
+
if (blocked) {
|
|
346
|
+
return {
|
|
347
|
+
success: false,
|
|
348
|
+
limit: tokens,
|
|
349
|
+
remaining: 0,
|
|
350
|
+
reset: reset2,
|
|
351
|
+
pending: Promise.resolve()
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
const requestID = randomId();
|
|
356
|
+
const bucket = Math.floor(Date.now() / windowDuration);
|
|
357
|
+
const key = [identifier, bucket].join(":");
|
|
358
|
+
const dbs = ctx.redis.map((redis) => ({
|
|
359
|
+
redis,
|
|
360
|
+
request: redis.eval(script, [key], [requestID, windowDuration])
|
|
361
|
+
}));
|
|
362
|
+
const firstResponse = await Promise.any(dbs.map((s) => s.request));
|
|
363
|
+
const usedTokens = firstResponse.length;
|
|
364
|
+
const remaining = tokens - usedTokens - 1;
|
|
365
|
+
async function sync() {
|
|
366
|
+
const individualIDs = await Promise.all(dbs.map((s) => s.request));
|
|
367
|
+
const allIDs = Array.from(new Set(individualIDs.flatMap((_) => _)).values());
|
|
368
|
+
for (const db of dbs) {
|
|
369
|
+
const ids = await db.request;
|
|
370
|
+
if (ids.length >= tokens) {
|
|
371
|
+
continue;
|
|
372
|
+
}
|
|
373
|
+
const diff = allIDs.filter((id) => !ids.includes(id));
|
|
374
|
+
if (diff.length === 0) {
|
|
375
|
+
continue;
|
|
376
|
+
}
|
|
377
|
+
await db.redis.sadd(key, ...allIDs);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
const success = remaining > 0;
|
|
381
|
+
const reset = (bucket + 1) * windowDuration;
|
|
382
|
+
if (ctx.cache && !success) {
|
|
383
|
+
ctx.cache.blockUntil(identifier, reset);
|
|
384
|
+
}
|
|
385
|
+
return {
|
|
386
|
+
success,
|
|
387
|
+
limit: tokens,
|
|
388
|
+
remaining,
|
|
389
|
+
reset,
|
|
390
|
+
pending: sync()
|
|
391
|
+
};
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* Combined approach of `slidingLogs` and `fixedWindow` with lower storage
|
|
396
|
+
* costs than `slidingLogs` and improved boundary behavior by calcualting a
|
|
397
|
+
* weighted score between two windows.
|
|
398
|
+
*
|
|
399
|
+
* **Pro:**
|
|
400
|
+
*
|
|
401
|
+
* Good performance allows this to scale to very high loads.
|
|
402
|
+
*
|
|
403
|
+
* **Con:**
|
|
404
|
+
*
|
|
405
|
+
* Nothing major.
|
|
406
|
+
*
|
|
407
|
+
* @param tokens - How many requests a user can make in each time window.
|
|
408
|
+
* @param window - The duration in which the user can max X requests.
|
|
409
|
+
*/
|
|
410
|
+
static slidingWindow(tokens, window) {
|
|
411
|
+
const windowSize = ms(window);
|
|
412
|
+
const script = `
|
|
413
|
+
local currentKey = KEYS[1] -- identifier including prefixes
|
|
414
|
+
local previousKey = KEYS[2] -- key of the previous bucket
|
|
415
|
+
local tokens = tonumber(ARGV[1]) -- tokens per window
|
|
416
|
+
local now = ARGV[2] -- current timestamp in milliseconds
|
|
417
|
+
local window = ARGV[3] -- interval in milliseconds
|
|
418
|
+
local requestID = ARGV[4] -- uuid for this request
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
local currentMembers = redis.call("SMEMBERS", currentKey)
|
|
422
|
+
local requestsInCurrentWindow = #currentMembers
|
|
423
|
+
local previousMembers = redis.call("SMEMBERS", previousKey)
|
|
424
|
+
local requestsInPreviousWindow = #previousMembers
|
|
425
|
+
|
|
426
|
+
local percentageInCurrent = ( now % window) / window
|
|
427
|
+
if requestsInPreviousWindow * ( 1 - percentageInCurrent ) + requestsInCurrentWindow >= tokens then
|
|
428
|
+
return {currentMembers, previousMembers}
|
|
429
|
+
end
|
|
430
|
+
|
|
431
|
+
redis.call("SADD", currentKey, requestID)
|
|
432
|
+
table.insert(currentMembers, requestID)
|
|
433
|
+
if requestsInCurrentWindow == 0 then
|
|
434
|
+
-- The first time this key is set, the value will be 1.
|
|
435
|
+
-- So we only need the expire command once
|
|
436
|
+
redis.call("PEXPIRE", currentKey, window * 2 + 1000) -- Enough time to overlap with a new window + 1 second
|
|
437
|
+
end
|
|
438
|
+
return {currentMembers, previousMembers}
|
|
439
|
+
`;
|
|
440
|
+
const windowDuration = ms(window);
|
|
441
|
+
return async function(ctx, identifier) {
|
|
442
|
+
if (ctx.cache) {
|
|
443
|
+
const { blocked, reset: reset2 } = ctx.cache.isBlocked(identifier);
|
|
444
|
+
if (blocked) {
|
|
445
|
+
return {
|
|
446
|
+
success: false,
|
|
447
|
+
limit: tokens,
|
|
448
|
+
remaining: 0,
|
|
449
|
+
reset: reset2,
|
|
450
|
+
pending: Promise.resolve()
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
const requestID = randomId();
|
|
455
|
+
const now = Date.now();
|
|
456
|
+
const currentWindow = Math.floor(now / windowSize);
|
|
457
|
+
const currentKey = [identifier, currentWindow].join(":");
|
|
458
|
+
const previousWindow = currentWindow - windowSize;
|
|
459
|
+
const previousKey = [identifier, previousWindow].join(":");
|
|
460
|
+
const dbs = ctx.redis.map((redis) => ({
|
|
461
|
+
redis,
|
|
462
|
+
request: redis.eval(script, [currentKey, previousKey], [tokens, now, windowDuration, requestID])
|
|
463
|
+
}));
|
|
464
|
+
const percentageInCurrent = now % windowDuration / windowDuration;
|
|
465
|
+
const [current, previous] = await Promise.any(dbs.map((s) => s.request));
|
|
466
|
+
const usedTokens = previous.length * (1 - percentageInCurrent) + current.length;
|
|
467
|
+
const remaining = tokens - usedTokens;
|
|
468
|
+
async function sync() {
|
|
469
|
+
const [individualIDs] = await Promise.all(dbs.map((s) => s.request));
|
|
470
|
+
const allIDs = Array.from(new Set(individualIDs.flatMap((_) => _)).values());
|
|
471
|
+
for (const db of dbs) {
|
|
472
|
+
const [ids] = await db.request;
|
|
473
|
+
if (ids.length >= tokens) {
|
|
474
|
+
continue;
|
|
475
|
+
}
|
|
476
|
+
const diff = allIDs.filter((id) => !ids.includes(id));
|
|
477
|
+
if (diff.length === 0) {
|
|
478
|
+
continue;
|
|
479
|
+
}
|
|
480
|
+
await db.redis.sadd(currentKey, ...allIDs);
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
const success = remaining > 0;
|
|
484
|
+
const reset = (currentWindow + 1) * windowDuration;
|
|
485
|
+
if (ctx.cache && !success) {
|
|
486
|
+
ctx.cache.blockUntil(identifier, reset);
|
|
487
|
+
}
|
|
488
|
+
return {
|
|
489
|
+
success,
|
|
490
|
+
limit: tokens,
|
|
491
|
+
remaining,
|
|
492
|
+
reset,
|
|
493
|
+
pending: sync()
|
|
494
|
+
};
|
|
495
|
+
};
|
|
496
|
+
}
|
|
497
|
+
};
|
|
498
|
+
|
|
281
499
|
// src/single.ts
|
|
282
500
|
var RegionRatelimit = class extends Ratelimit {
|
|
283
501
|
/**
|
|
@@ -384,7 +602,7 @@ var RegionRatelimit = class extends Ratelimit {
|
|
|
384
602
|
|
|
385
603
|
local requestsInCurrentWindow = redis.call("GET", currentKey)
|
|
386
604
|
if requestsInCurrentWindow == false then
|
|
387
|
-
requestsInCurrentWindow =
|
|
605
|
+
requestsInCurrentWindow = -1
|
|
388
606
|
end
|
|
389
607
|
|
|
390
608
|
|
|
@@ -394,7 +612,7 @@ var RegionRatelimit = class extends Ratelimit {
|
|
|
394
612
|
end
|
|
395
613
|
local percentageInCurrent = ( now % window) / window
|
|
396
614
|
if requestsInPreviousWindow * ( 1 - percentageInCurrent ) + requestsInCurrentWindow >= tokens then
|
|
397
|
-
return
|
|
615
|
+
return -1
|
|
398
616
|
end
|
|
399
617
|
|
|
400
618
|
local newValue = redis.call("INCR", currentKey)
|
|
@@ -425,7 +643,7 @@ var RegionRatelimit = class extends Ratelimit {
|
|
|
425
643
|
}
|
|
426
644
|
}
|
|
427
645
|
const remaining = await ctx.redis.eval(script, [currentKey, previousKey], [tokens, now, windowSize]);
|
|
428
|
-
const success = remaining
|
|
646
|
+
const success = remaining >= 0;
|
|
429
647
|
const reset = (currentWindow + 1) * windowSize;
|
|
430
648
|
if (ctx.cache && !success) {
|
|
431
649
|
ctx.cache.blockUntil(identifier, reset);
|
|
@@ -433,7 +651,7 @@ var RegionRatelimit = class extends Ratelimit {
|
|
|
433
651
|
return {
|
|
434
652
|
success,
|
|
435
653
|
limit: tokens,
|
|
436
|
-
remaining,
|
|
654
|
+
remaining: Math.max(0, remaining),
|
|
437
655
|
reset,
|
|
438
656
|
pending: Promise.resolve()
|
|
439
657
|
};
|
|
@@ -600,224 +818,6 @@ var RegionRatelimit = class extends Ratelimit {
|
|
|
600
818
|
};
|
|
601
819
|
}
|
|
602
820
|
};
|
|
603
|
-
|
|
604
|
-
// src/multi.ts
|
|
605
|
-
function randomId() {
|
|
606
|
-
let result = "";
|
|
607
|
-
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
608
|
-
const charactersLength = characters.length;
|
|
609
|
-
for (let i = 0; i < 16; i++) {
|
|
610
|
-
result += characters.charAt(Math.floor(Math.random() * charactersLength));
|
|
611
|
-
}
|
|
612
|
-
return result;
|
|
613
|
-
}
|
|
614
|
-
var MultiRegionRatelimit = class extends Ratelimit {
|
|
615
|
-
/**
|
|
616
|
-
* Create a new Ratelimit instance by providing a `@upstash/redis` instance and the algorithn of your choice.
|
|
617
|
-
*/
|
|
618
|
-
constructor(config) {
|
|
619
|
-
super({
|
|
620
|
-
prefix: config.prefix,
|
|
621
|
-
limiter: config.limiter,
|
|
622
|
-
timeout: config.timeout,
|
|
623
|
-
analytics: config.analytics,
|
|
624
|
-
ctx: {
|
|
625
|
-
redis: config.redis,
|
|
626
|
-
cache: config.ephemeralCache ? new Cache(config.ephemeralCache) : void 0
|
|
627
|
-
}
|
|
628
|
-
});
|
|
629
|
-
}
|
|
630
|
-
/**
|
|
631
|
-
* Each requests inside a fixed time increases a counter.
|
|
632
|
-
* Once the counter reaches a maxmimum allowed number, all further requests are
|
|
633
|
-
* rejected.
|
|
634
|
-
*
|
|
635
|
-
* **Pro:**
|
|
636
|
-
*
|
|
637
|
-
* - Newer requests are not starved by old ones.
|
|
638
|
-
* - Low storage cost.
|
|
639
|
-
*
|
|
640
|
-
* **Con:**
|
|
641
|
-
*
|
|
642
|
-
* A burst of requests near the boundary of a window can result in a very
|
|
643
|
-
* high request rate because two windows will be filled with requests quickly.
|
|
644
|
-
*
|
|
645
|
-
* @param tokens - How many requests a user can make in each time window.
|
|
646
|
-
* @param window - A fixed timeframe
|
|
647
|
-
*/
|
|
648
|
-
static fixedWindow(tokens, window) {
|
|
649
|
-
const windowDuration = ms(window);
|
|
650
|
-
const script = `
|
|
651
|
-
local key = KEYS[1]
|
|
652
|
-
local id = ARGV[1]
|
|
653
|
-
local window = ARGV[2]
|
|
654
|
-
|
|
655
|
-
redis.call("SADD", key, id)
|
|
656
|
-
local members = redis.call("SMEMBERS", key)
|
|
657
|
-
if #members == 1 then
|
|
658
|
-
-- The first time this key is set, the value will be 1.
|
|
659
|
-
-- So we only need the expire command once
|
|
660
|
-
redis.call("PEXPIRE", key, window)
|
|
661
|
-
end
|
|
662
|
-
|
|
663
|
-
return members
|
|
664
|
-
`;
|
|
665
|
-
return async function(ctx, identifier) {
|
|
666
|
-
if (ctx.cache) {
|
|
667
|
-
const { blocked, reset: reset2 } = ctx.cache.isBlocked(identifier);
|
|
668
|
-
if (blocked) {
|
|
669
|
-
return {
|
|
670
|
-
success: false,
|
|
671
|
-
limit: tokens,
|
|
672
|
-
remaining: 0,
|
|
673
|
-
reset: reset2,
|
|
674
|
-
pending: Promise.resolve()
|
|
675
|
-
};
|
|
676
|
-
}
|
|
677
|
-
}
|
|
678
|
-
const requestID = randomId();
|
|
679
|
-
const bucket = Math.floor(Date.now() / windowDuration);
|
|
680
|
-
const key = [identifier, bucket].join(":");
|
|
681
|
-
const dbs = ctx.redis.map((redis) => ({
|
|
682
|
-
redis,
|
|
683
|
-
request: redis.eval(script, [key], [requestID, windowDuration])
|
|
684
|
-
}));
|
|
685
|
-
const firstResponse = await Promise.any(dbs.map((s) => s.request));
|
|
686
|
-
const usedTokens = firstResponse.length;
|
|
687
|
-
const remaining = tokens - usedTokens - 1;
|
|
688
|
-
async function sync() {
|
|
689
|
-
const individualIDs = await Promise.all(dbs.map((s) => s.request));
|
|
690
|
-
const allIDs = Array.from(new Set(individualIDs.flatMap((_) => _)).values());
|
|
691
|
-
for (const db of dbs) {
|
|
692
|
-
const ids = await db.request;
|
|
693
|
-
if (ids.length >= tokens) {
|
|
694
|
-
continue;
|
|
695
|
-
}
|
|
696
|
-
const diff = allIDs.filter((id) => !ids.includes(id));
|
|
697
|
-
if (diff.length === 0) {
|
|
698
|
-
continue;
|
|
699
|
-
}
|
|
700
|
-
await db.redis.sadd(key, ...allIDs);
|
|
701
|
-
}
|
|
702
|
-
}
|
|
703
|
-
const success = remaining > 0;
|
|
704
|
-
const reset = (bucket + 1) * windowDuration;
|
|
705
|
-
if (ctx.cache && !success) {
|
|
706
|
-
ctx.cache.blockUntil(identifier, reset);
|
|
707
|
-
}
|
|
708
|
-
return {
|
|
709
|
-
success,
|
|
710
|
-
limit: tokens,
|
|
711
|
-
remaining,
|
|
712
|
-
reset,
|
|
713
|
-
pending: sync()
|
|
714
|
-
};
|
|
715
|
-
};
|
|
716
|
-
}
|
|
717
|
-
/**
|
|
718
|
-
* Combined approach of `slidingLogs` and `fixedWindow` with lower storage
|
|
719
|
-
* costs than `slidingLogs` and improved boundary behavior by calcualting a
|
|
720
|
-
* weighted score between two windows.
|
|
721
|
-
*
|
|
722
|
-
* **Pro:**
|
|
723
|
-
*
|
|
724
|
-
* Good performance allows this to scale to very high loads.
|
|
725
|
-
*
|
|
726
|
-
* **Con:**
|
|
727
|
-
*
|
|
728
|
-
* Nothing major.
|
|
729
|
-
*
|
|
730
|
-
* @param tokens - How many requests a user can make in each time window.
|
|
731
|
-
* @param window - The duration in which the user can max X requests.
|
|
732
|
-
*/
|
|
733
|
-
static slidingWindow(tokens, window) {
|
|
734
|
-
const windowSize = ms(window);
|
|
735
|
-
const script = `
|
|
736
|
-
local currentKey = KEYS[1] -- identifier including prefixes
|
|
737
|
-
local previousKey = KEYS[2] -- key of the previous bucket
|
|
738
|
-
local tokens = tonumber(ARGV[1]) -- tokens per window
|
|
739
|
-
local now = ARGV[2] -- current timestamp in milliseconds
|
|
740
|
-
local window = ARGV[3] -- interval in milliseconds
|
|
741
|
-
local requestID = ARGV[4] -- uuid for this request
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
local currentMembers = redis.call("SMEMBERS", currentKey)
|
|
745
|
-
local requestsInCurrentWindow = #currentMembers
|
|
746
|
-
local previousMembers = redis.call("SMEMBERS", previousKey)
|
|
747
|
-
local requestsInPreviousWindow = #previousMembers
|
|
748
|
-
|
|
749
|
-
local percentageInCurrent = ( now % window) / window
|
|
750
|
-
if requestsInPreviousWindow * ( 1 - percentageInCurrent ) + requestsInCurrentWindow >= tokens then
|
|
751
|
-
return {currentMembers, previousMembers}
|
|
752
|
-
end
|
|
753
|
-
|
|
754
|
-
redis.call("SADD", currentKey, requestID)
|
|
755
|
-
table.insert(currentMembers, requestID)
|
|
756
|
-
if requestsInCurrentWindow == 0 then
|
|
757
|
-
-- The first time this key is set, the value will be 1.
|
|
758
|
-
-- So we only need the expire command once
|
|
759
|
-
redis.call("PEXPIRE", currentKey, window * 2 + 1000) -- Enough time to overlap with a new window + 1 second
|
|
760
|
-
end
|
|
761
|
-
return {currentMembers, previousMembers}
|
|
762
|
-
`;
|
|
763
|
-
const windowDuration = ms(window);
|
|
764
|
-
return async function(ctx, identifier) {
|
|
765
|
-
if (ctx.cache) {
|
|
766
|
-
const { blocked, reset: reset2 } = ctx.cache.isBlocked(identifier);
|
|
767
|
-
if (blocked) {
|
|
768
|
-
return {
|
|
769
|
-
success: false,
|
|
770
|
-
limit: tokens,
|
|
771
|
-
remaining: 0,
|
|
772
|
-
reset: reset2,
|
|
773
|
-
pending: Promise.resolve()
|
|
774
|
-
};
|
|
775
|
-
}
|
|
776
|
-
}
|
|
777
|
-
const requestID = randomId();
|
|
778
|
-
const now = Date.now();
|
|
779
|
-
const currentWindow = Math.floor(now / windowSize);
|
|
780
|
-
const currentKey = [identifier, currentWindow].join(":");
|
|
781
|
-
const previousWindow = currentWindow - windowSize;
|
|
782
|
-
const previousKey = [identifier, previousWindow].join(":");
|
|
783
|
-
const dbs = ctx.redis.map((redis) => ({
|
|
784
|
-
redis,
|
|
785
|
-
request: redis.eval(script, [currentKey, previousKey], [tokens, now, windowDuration, requestID])
|
|
786
|
-
}));
|
|
787
|
-
const percentageInCurrent = now % windowDuration / windowDuration;
|
|
788
|
-
const [current, previous] = await Promise.any(dbs.map((s) => s.request));
|
|
789
|
-
const usedTokens = previous.length * (1 - percentageInCurrent) + current.length;
|
|
790
|
-
const remaining = tokens - usedTokens;
|
|
791
|
-
async function sync() {
|
|
792
|
-
const [individualIDs] = await Promise.all(dbs.map((s) => s.request));
|
|
793
|
-
const allIDs = Array.from(new Set(individualIDs.flatMap((_) => _)).values());
|
|
794
|
-
for (const db of dbs) {
|
|
795
|
-
const [ids] = await db.request;
|
|
796
|
-
if (ids.length >= tokens) {
|
|
797
|
-
continue;
|
|
798
|
-
}
|
|
799
|
-
const diff = allIDs.filter((id) => !ids.includes(id));
|
|
800
|
-
if (diff.length === 0) {
|
|
801
|
-
continue;
|
|
802
|
-
}
|
|
803
|
-
await db.redis.sadd(currentKey, ...allIDs);
|
|
804
|
-
}
|
|
805
|
-
}
|
|
806
|
-
const success = remaining > 0;
|
|
807
|
-
const reset = (currentWindow + 1) * windowDuration;
|
|
808
|
-
if (ctx.cache && !success) {
|
|
809
|
-
ctx.cache.blockUntil(identifier, reset);
|
|
810
|
-
}
|
|
811
|
-
return {
|
|
812
|
-
success,
|
|
813
|
-
limit: tokens,
|
|
814
|
-
remaining,
|
|
815
|
-
reset,
|
|
816
|
-
pending: sync()
|
|
817
|
-
};
|
|
818
|
-
};
|
|
819
|
-
}
|
|
820
|
-
};
|
|
821
821
|
// Annotate the CommonJS export names for ESM import in node:
|
|
822
822
|
0 && (module.exports = {
|
|
823
823
|
Analytics,
|