UNPKG

relief-valve

Version:

This is a simple library for Redis Streams data type, which is used to accumulate messages until a specified threshold is reached, post which the same is available to consumer stream.

47 lines (46 loc) 2.36 kB
import { IRedisClientPool } from 'redis-abstraction'; /** * Abstraction representing collection of multiple message into a batch */ export interface IBatch extends IBatchIdentity { /** Number of time the message was delivered to a certain consumer group. */ readsInCurrentGroup: number; /** Represents accumalated messages the Key is the ID given at the time of publishing and the value is object*/ payload: Map<string, object>; } /** * Provides identity to the batch accumalated */ export interface IBatchIdentity { /** Stream Id used to acknowledge the message. */ id: string; } export declare class ReliefValve { private redisPool; private name; private countThreshold; private timeThresholdInSeconds; private groupName; private clientName; private indexKey; private accumalatorKey; private cappedStreamLength; private systemIdPropName; private releaseCount; private writeScript; private timePurgeScript; private groupsCreated; /** Used to contruct and instance of the class. * @param redisPool Connector through which this instance will talk to redis. * @param name A unique name for the Queue/Stream for the consumers to subscribe on. * @param countThreshold A positive number which acts as a setpoint for the relief valve(pressure release point), negative numbers will be converted to positive and zero to 1. * @param timeThresholdInSeconds A positive number in seconds which acts as elapsed time in future post which the valve will be opened even if count threshold is not reached from the last time of write, negative numbers will be converted to positive and zero to 1. * */ constructor(redisPool: IRedisClientPool, name: string, countThreshold: number, timeThresholdInSeconds: number, groupName: string, clientName: string, indexKey?: string, accumalatorKey?: (data: object) => Promise<string>, cappedStreamLength?: number, systemIdPropName?: string, releaseCount?: number); publish(data: object, id?: string): Promise<string>; recheckTimeThreshold(refreshTimeOnSucessfullPurge?: number): Promise<void>; consumeFreshOrStale(batchIdealThresholdInSeconds: number): Promise<IBatch | undefined>; acknowledge(batch: IBatchIdentity, dropBatch?: boolean): Promise<boolean>; private createStreamGroupIfNotExists; }