UNPKG

@da440dil/js-counter

Version:

Distributed rate limiting using Redis

49 lines (48 loc) 2.55 kB
import { IRedisClient, INodeRedisClient } from '@da440dil/js-redis-script'; import { Counter, Algorithm } from './Counter'; import { Result } from './Result'; import { ILimiter } from './ILimiter'; export { IRedisClient, INodeRedisClient, Counter, Result, Algorithm, ILimiter }; /** * Creates new counter which implements distributed counter using fixed window algorithm. * @param client Minimal Redis client interface: [node-redis](https://github.com/NodeRedis/node-redis) v3 or v4 or [ioredis](https://github.com/luin/ioredis) v4. * @param size Window size in milliseconds. Must be greater than 0. * @param limit Maximum key value per window. Must be greater than 0. */ export declare const fixedWindow: (client: IRedisClient | INodeRedisClient, size: number, limit: number) => Counter; /** * Creates new counter which implements distributed counter using sliding window algorithm. * @param client Minimal Redis client interface: [node-redis](https://github.com/NodeRedis/node-redis) v3 or v4 or [ioredis](https://github.com/luin/ioredis) v4. * @param size Window size in milliseconds. Must be greater than 0. * @param limit Maximum key value per window. Must be greater than 0. */ export declare const slidingWindow: (client: IRedisClient | INodeRedisClient, size: number, limit: number) => Counter; /** * Creates new limiter which implements distributed rate limiting. * @param client Minimal Redis client interface: [node-redis](https://github.com/NodeRedis/node-redis) v3 or v4 or [ioredis](https://github.com/luin/ioredis) v4. * @param first Params of the first limit. * @param rest Params of the rest limits. */ export declare const createLimiter: (client: IRedisClient | INodeRedisClient, first: Params, ...rest: Params[]) => ILimiter; /** Parameters to build a limit. */ export declare type Params = { /** Window size in milliseconds. Must be greater than 0. */ size: number; /** Maximum key value per window. Must be greater than 0. */ limit: number; /** * Unique limit name, every Redis key will be prefixed with this name. * By default is pseudo-random string. */ name?: string; /** * The rate of decreasing the window size on each next application of the limit. * Must be greater than 0. By default equal 1. */ rate?: number; /** * One of algorithms: 1 stands for the fixed window algorithm, 2 stands for the sliding window algorithm. * By default equal 1. */ algorithm?: Algorithm; };