import type Redis from "ioredis"; import type { Cluster } from "ioredis"; import type { Pacer, PacerOutcome } from "./Pacer"; /** * Options which identify each lightweight pacer instance. */ export interface DistributedPacerOptions { /** Redis key for the pacer. */ key: string; /** Target maximum allowed QPS. */ qps: number; /** How much accumulated weight of the requests is allowed on top of an idle * pacer before turning on pacing. */ maxBurst?: number; /** At what factor of QPS do we earn back burst allowance. This makes sense * for pacing use case; for rate limiting, the common value passed here is 1, * since we start rejecting requests after the bucket is full and do not queue * them above the bucket. */ burstAllowanceFactor?: number; } /** * A lightweight class which wraps a Redis client and implements distributed * pacing algorithm on top of it. */ export declare class DistributedPacer implements Pacer { /** Configuration options. */ readonly options: DistributedPacerOptions; /** Namespace to prepend all the keys with. */ readonly namespace?: string | undefined; private redis; /** * Initializes a pacer instance. You can create instances of this class as * often as you want. */ constructor( /** Redis client instance (connection) to use. */ redis: Redis | Cluster, /** Configuration options. */ options: DistributedPacerOptions, /** Namespace to prepend all the keys with. */ namespace?: string | undefined); /** * Returns the Redis key (aka name) of this pacer. */ get key(): string; /** * Calling this method signals the pacer that we want to send a request. The * method predicts the delay on which the worker needs to await before * actually sending the request. */ pace( /** If the request is a batch of sub-operations, and its timing or execution * cost depends on the batch size, pass the size of the batch here. */ weight?: number): Promise; /** * Implements rare limiting use case (i.e. on receiver side), when the * requests which go out of qps & maxBurst quote are rejected instead of being * delayed. For rate limiting, we use the same algorithm which handles bursts * in pace() method. */ rateLimit( /** If the request is a batch of sub-operations, and its timing or execution * cost depends on the batch size, pass the size of the batch here. */ weight?: number): Promise; /** * @ignore * Returns the current timestamp in milliseconds. Useful for mocking in tests. */ now(): number; /** * Builds a namespaced key for the Redis storage. */ private buildNamespacedKey; /** * Handles both pacing and rate limiting use cases. For rate limiting, we pass * saveOnDelay=true flag to denote that the rejected requests (aka * requests with nonzero delay returned) should not alter the bucket state in * the database (since we reject them, we are not gonna process them, thus, * they should not add water to the bucket). */ private push; } //# sourceMappingURL=DistributedPacer.d.ts.map