export class TokenBucket {
rate;
burst;
#tokens;
#last;
constructor(rate, burst, now = performance.now()) {
this.rate = rate;
this.burst = burst;
this.#tokens = burst;
this.#last = now;
}
take(now = performance.now()) {
this.#tokens = Math.min(this.burst, this.#tokens + ((now - this.#last) / 1000) * this.rate);
this.#last = now;
if (this.#tokens < 1)
return false;
this.#tokens -= 1;
return true;
}
}
export class WindowCounter {
limit;
windowMs;
#hits = new Map();
constructor(limit, windowMs) {
this.limit = limit;
this.windowMs = windowMs;
}
hit(key, now = performance.now()) {
const since = now - this.windowMs;
const list = (this.#hits.get(key) ?? []).filter((t) => t > since);
if (list.length >= this.limit) {
this.#hits.set(key, list);
return false;
}
list.push(now);
this.#hits.set(key, list);
if (this.#hits.size > 10_000)
this.#prune(since);
return true;
}
over(key, now = performance.now()) {
const since = now - this.windowMs;
return (this.#hits.get(key) ?? []).filter((t) => t > since).length >= this.limit;
}
#prune(since) {
for (const [k, v] of this.#hits) {
if (v.every((t) => t <= since))
this.#hits.delete(k);
}
}
}