/** * Token-bucket rate limiter with per-key state tracking and automatic cleanup. * * Each key (IP address or tenant DID) has its own bucket that refills at a * fixed rate. When a request arrives, a token is consumed. If the bucket is * empty, the request is rejected with a retry-after hint. * * Stale buckets are periodically purged to prevent unbounded memory growth. * * @see https://github.com/enboxorg/enbox/issues/326 */ export type RateLimiterConfig = { /** Tokens added per second (i.e. sustained request rate). */ refillRate: number; /** Maximum burst size (bucket capacity). */ maxTokens: number; }; export declare class RateLimiter { #private; /** Stale buckets older than 5 minutes are purged. */ private static readonly STALE_THRESHOLD_MS; /** Cleanup runs every 60 seconds. */ private static readonly CLEANUP_INTERVAL_MS; constructor(config: RateLimiterConfig); /** * Attempts to consume a token for the given key. * @returns `{ allowed: true }` if the request is permitted, or * `{ allowed: false, retryAfterMs }` if the rate limit is exceeded. */ consume(key: string): { allowed: true; } | { allowed: false; retryAfterMs: number; }; /** * Returns the number of keys currently being tracked. */ get size(): number; /** * Returns the current token count for a key, or `undefined` if not tracked. */ getTokens(key: string): number | undefined; /** * Reconfigures the rate limiter with new settings. Existing buckets are * retained but will use the new `refillRate` and `maxTokens` going forward. * * @see https://github.com/enboxorg/enbox/issues/389 */ reconfigure(config: RateLimiterConfig): void; /** * Returns the current configuration of this rate limiter. */ get config(): RateLimiterConfig; /** * Stops the periodic cleanup timer. Call this when shutting down. */ destroy(): void; } //# sourceMappingURL=rate-limiter.d.ts.map