feat: mutual friend graph (#1491)

This commit is contained in:
pa
2025-11-18 23:30:00 +09:00
committed by Natsumi
parent 0bc9980cae
commit 424edb04e0
12 changed files with 1073 additions and 35 deletions

View File

@@ -0,0 +1,28 @@
export function createRateLimiter({ limitPerInterval, intervalMs }) {
const stamps = [];
async function throttle() {
const now = Date.now();
while (stamps.length && now - stamps[0] > intervalMs) {
stamps.shift();
}
if (stamps.length >= limitPerInterval) {
const wait = intervalMs - (now - stamps[0]);
await new Promise((resolve) => setTimeout(resolve, wait));
}
stamps.push(Date.now());
}
return {
async schedule(fn) {
await throttle();
return fn();
},
async wait() {
await throttle();
},
clear() {
stamps.length = 0;
}
};
}