import { type Milliseconds } from '../date/date'; import { type Maybe } from '../value/maybe.type'; /** * Interface for a rate limiter. */ export interface PromiseRateLimiter { /** * Returns the expected wait time for the next wait. * * Can optionally provide an increase that updates the limiter to behave like the number of items is now being waited on. */ getNextWaitTime(increase?: number): Milliseconds; /** * Waits for the rate limited to allow the promise to continue. */ waitForRateLimit(): Promise; } /** * Interface for a PromiseRateLimiter that can be enabled or disabled. */ export interface EnableTogglePromiseRateLimiter extends PromiseRateLimiter { /** * Returns true if the rate limiter is enabled or not. */ getEnabled(): boolean; /** * Enables or disables the rate limiter based on the inputs. * * @param enable */ setEnabled(enable: boolean): void; } export interface ExponentialPromiseRateLimiterConfig { /** * The base count to start limiting requests at. Minimum of 0 allowed. * * Defaults to 0. */ readonly startLimitAt?: number; /** * How fast the cooldown occurs. * * Defaults to 1. */ readonly cooldownRate?: number; /** * The maximum amount of wait time to limit exponential requests to, if applicable. * * Defaults to 1 hour. */ readonly maxWaitTime?: Milliseconds; /** * The base exponent of the growth. * * Defaults to 2. */ readonly exponentRate?: number; } /** * A fully resolved {@link ExponentialPromiseRateLimiterConfig} with all fields required. */ export type FullExponentialPromiseRateLimiterConfig = Required; /** * An exponential backoff rate limiter that increases wait times exponentially based on usage, * with a configurable cooldown that reduces the effective count over time. */ export interface ExponentialPromiseRateLimiter extends EnableTogglePromiseRateLimiter { /** * Returns the current config. */ getConfig(): FullExponentialPromiseRateLimiterConfig; /** * Updates the configuration. */ setConfig(config: Partial, andReset?: boolean): void; /** * Manually resets the limit */ reset(): void; } /** * Creates an {@link ExponentialPromiseRateLimiter} that computes wait times using exponential * growth with a configurable cooldown that reduces the effective count over time. * * @param initialConfig - Optional initial configuration. Defaults are applied for any omitted fields. * @returns An ExponentialPromiseRateLimiter instance. */ export declare function exponentialPromiseRateLimiter(initialConfig?: Maybe): ExponentialPromiseRateLimiter; /** * Configuration for a {@link ResetPeriodPromiseRateLimiter} that resets its remaining count * periodically and applies exponential backoff when the limit is exhausted. */ export interface ResetPeriodPromiseRateLimiterConfig extends Partial { /** * The number of times the rate limiter can be used before it needs to be reset. */ readonly limit: number; /** * Optional specific Date/Time at which to reset the remaining count back to the limit. */ readonly resetAt?: Maybe; /** * Amount of time in milliseconds to set the next resetAt time when reset is called. */ readonly resetPeriod: Milliseconds; } /** * A rate limiter that resets every specific period of time and has a limited amount of items that can go out. */ export interface ResetPeriodPromiseRateLimiter extends EnableTogglePromiseRateLimiter { /** * Returns the current limit details with the amount remaining. */ getRemainingLimit(): number; /** * Sets the remaining limit number. * * @param limit */ setRemainingLimit(limit: number): void; /** * Returns the number of milliseconds until the next reset. */ getTimeUntilNextReset(): Milliseconds; /** * Returns the next reset at date/time. */ getResetAt(): Date; /** * Sets the next reset at Date. */ setNextResetAt(date: Date): void; /** * Sets the new config. * * @param limit */ setConfig(config: Partial, andReset?: boolean): void; /** * Manually resets the "remaining" amount on the limit back to the limit amount. */ reset(): void; } /** * Creates a {@link ResetPeriodPromiseRateLimiter} that limits the number of operations per time period, * resetting the count at regular intervals and applying exponential backoff when the limit is exceeded. * * @param initialConfig - Configuration specifying the limit count, reset period, and optional exponential backoff settings. * @returns A ResetPeriodPromiseRateLimiter instance. */ export declare function resetPeriodPromiseRateLimiter(initialConfig: ResetPeriodPromiseRateLimiterConfig): ResetPeriodPromiseRateLimiter;