Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 10x 10x 10x 42x 38x 19x 38x 1x 1x 37x 37x 37x 37x 47x 47x | /**
* A sliding-window limit kept in memory, per replica: at most `perKey`
* calls per key and `inTotal` calls overall within `windowMs`. Enough for an
* anonymous form (ADR-048); not a shared or durable quota.
*/
export function createRateLimiter(opts: {
windowMs: number;
perKey: number;
inTotal: number;
}) {
const byKey = new Map<string, number[]>();
let all: number[] = [];
return {
/** Counts the call and returns true, or returns false over the limit. */
allow(key: string, now: number = Date.now()): boolean {
all = all.filter((t) => now - t < opts.windowMs);
const mine = (byKey.get(key) ?? []).filter(
(t) => now - t < opts.windowMs,
);
if (mine.length >= opts.perKey || all.length >= opts.inTotal) {
if (mine.length) byKey.set(key, mine);
else EbyKey.delete(key);
return false;
}
mine.push(now);
byKey.set(key, mine);
all.push(now);
return true;
},
reset(): void {
byKey.clear();
all = [];
},
};
}
|