UNPKG

@evolplus/evo-utils

Version:
72 lines (71 loc) 2.9 kB
export interface RateLimiter { hit(key: string): boolean; } /** * Type definition for the configuration of a TimeBasedLimiter. * Maps timeframes (in milliseconds) to the number of allowed requests within that timeframe. * * @typedef {Object} TimeBasedLimiterConfig * * @property {number} key - The timeframe in milliseconds. * @property {number} value - The number of allowed requests within the timeframe. */ export type TimeBasedLimiterConfig = { [key: number]: number; }; /** * Class implementing a time-based rate limiter. * Allows requests based on specified timeframes and their respective limits. */ export declare class TimeBasedLimiter implements RateLimiter { private timeframes; private limits; private queues; /** * Creates an instance of the TimeBasedLimiter class with specified timeframes and limits. * * @param {TimeBasedLimiterConfig} config - The configuration mapping timeframes to their respective limits. */ constructor(config: TimeBasedLimiterConfig, capacity: number); /** * Initializes the request queues for a specific key. * Each key will have multiple queues, each corresponding to a specific timeframe and its limit. * * @private * @returns {Dequeue<number>[]} An array of deques representing the request queues for the key. */ private initKey; /** * Makes a request within the rate limiter. * Checks if the request is allowed based on the timeframes and their limits. * * @returns {boolean} True if the request is allowed, otherwise false. */ hit(key: string): boolean; } /** * Class implementing a decay-based rate limiter. * Limits requests based on a decay function over time. */ export declare class DecayLimiter implements RateLimiter { private decays; private limit; /** * Creates an instance of the DecayLimiter class with specified configuration. * Initializes the decay cache to manage the request scores and sets the half-life and limit properties. * * @param {number} capacity - The maximum capacity for the decay cache to store request scores. * @param {number} halfLife - The time period (in milliseconds) over which the scores decay. * @param {number} limit - The maximum score allowed for a request to be considered valid. */ constructor(capacity: number, halfLife: number, limit: number); /** * Processes a request for a specific key within the rate limiter. * The request's score decays over time and is adjusted based on subsequent requests. * Checks if the request's score is below the set limit to determine if it's allowed. * * @param {string} key - The key for which the request is made. * @returns {boolean} True if the request is allowed based on its decayed score, otherwise false. */ hit(key: string): boolean; }