openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
30 lines (29 loc) • 803 B
JavaScript
import { P as resolveIntegerOption } from "./number-coercion-D6u-4YD8.mjs";
//#region src/infra/fixed-window-rate-limit.ts
/** Process-local fixed-window quota; distributed limits require caller-owned persistence. */
function createFixedWindowBudget(e) {
const t = resolveIntegerOption(e.maxRequests, 1, { min: 1 });
const n = resolveIntegerOption(e.windowMs, 1, { min: 1 });
const r = e.now ?? Date.now;
let i = 0;
let a = 0;
return { consume() {
const e = r();
if (e - a >= n) {
a = e;
i = 0;
}
if (i >= t) return {
allowed: false,
/** Milliseconds until the next fixed window when quota is exhausted. */
retryAfterMs: Math.max(0, a + n - e)
};
i += 1;
return {
allowed: true,
retryAfterMs: 0
};
} };
}
//#endregion
export { createFixedWindowBudget as t };