import { Duration } from '@moojo/duration'; import { Clock } from '@moojo/misc-clock'; export type RateLimiterOptions = { /** * The interval in milliseconds at which the rate limiter performs cleanup of expired entries. Defaults to 10m. */ cleanupInterval: Duration; }; /** * RateLimiter provides a simple way to control operation frequency by enforcing minimum time intervals * between operations of the same category. It's useful for scenarios like throttling API calls, * limiting user actions, or controlling background task frequency. * * Each operation is identified by a category key, and the limiter ensures sufficient time has passed * since the last operation in that category. The class includes automatic cleanup to prevent memory leaks. * * @example * ```typescript * const limiter = new RateLimiter(clock); * * // Limit API calls to once per second * if (limiter.isAllowed('api_calls', 1000)) { * // Make API call * } * ``` */ export declare class RateLimiter { private readonly clock; private readonly lastAllowedByCategory; private readonly cleanupInterval; private lastCleanupTimestamp; /** * Creates a new RateLimiter instance. * @param clock - provides time measurements * @param options - Optional configuration */ constructor(clock: Clock, options?: Partial); /** * Checks if some arbitrary operation is allowed based on the specified minimum interval. * @param categoryKey - An identifier of the category to which the operation "belongs" * @param minInterval - The minimum time interval required to elapse between operations (of the same category) * @returns true if the operation is allowed, false otherwise */ isAllowed(categoryKey: string, minInterval: Duration): boolean; /** * Returns the current number of categories tracked by the rate limiter. Useful for mostly for testing/debugging * purposes. */ countEntries(): number; private cleanup; }