UNPKG

p-ratelimit

Version:

Promise-based utility to make sure you don’t call rate-limited APIs too quickly.

40 lines (39 loc) 1.73 kB
import { Quota } from './quota'; import { QuotaManager } from './quotaManager'; import { RedisClient } from 'redis'; import * as IORedis from 'ioredis'; declare type RedisCompatibleClient = RedisClient | IORedis.Redis | IORedis.Cluster; /** QuotaManager that coordinates rate limits across servers. */ export declare class RedisQuotaManager extends QuotaManager { private readonly channelQuota; private readonly heartbeatInterval; private readonly uniqueId; private readonly pubSubClient; private readonly pingsReceived; private readonly channelName; private readonly client; private _ready; private heartbeatTimer; /** * @param channelQuota the overall quota to be split among all clients * @param channelName unique name for this quota - the Redis pub/sub channel name * @param client a Redis client (or a pair of Redis clients if using a specialty Redis library) * @param heartbeatInterval how often to ping the Redis channel (milliseconds) */ constructor(channelQuota: Quota, channelName: string, client: RedisCompatibleClient | RedisCompatibleClient[], heartbeatInterval?: number); /** true once the Quota Manager has discovered its peers and calculated its quota */ get ready(): boolean; /** Join the client pool, coordinated by the shared channel on Redis */ private register; /** Send a ping to the shared Redis channel */ private ping; /** Receive client pings */ private message; /** Remove outdated clients */ private removeOutdatedClients; /** Calculate our portion of the overall channel quota */ private updateQuota; /** Let the others know we’re here */ private heartbeat; } export {};