UNPKG

@naturalcycles/js-lib

Version:

Standard library for universal (browser + Node.js) javascript

62 lines (61 loc) 1.99 kB
import { ErrorMode } from '../error/errorMode.js'; import { type CommonLogger, type CommonLogLevel } from '../log/commonLogger.js'; import type { AsyncFunction, PositiveInteger } from '../types.js'; export interface PGradualQueueCfg { concurrency: PositiveInteger; /** * Time in seconds to gradually increase concurrency from 1 to cfg.concurrency. * After this period, the queue operates at full concurrency. * * Set to 0 to disable warmup (behaves like regular PQueue). */ warmupSeconds: number; /** * Default: THROW_IMMEDIATELY * * THROW_AGGREGATED is not supported. * * SUPPRESS_ERRORS will still log errors via logger. It will resolve the `.push` promise with void. */ errorMode?: ErrorMode; /** * Default to `console` */ logger?: CommonLogger; /** * Default is 'log'. */ logLevel?: CommonLogLevel; } /** * A queue similar to PQueue that gradually increases concurrency from 1 to the configured * maximum over a warmup period. Useful for scenarios where you want to avoid overwhelming * a system at startup (e.g., database connections, API rate limits). * * API is @experimental */ export declare class PGradualQueue { constructor(cfg: PGradualQueueCfg); private readonly cfg; private readonly logger; private readonly warmupMs; private startTime; private warmupComplete; inFlight: number; private queue; /** * Get current allowed concurrency based on warmup progress. * Returns cfg.concurrency if warmup is complete (fast-path). */ private getCurrentConcurrency; /** * Push PromiseReturningFunction to the Queue. * Returns a Promise that resolves (or rejects) with the return value from the Promise. */ push<R>(fn_: AsyncFunction<R>): Promise<R>; get queueSize(): number; /** * Current concurrency limit based on warmup progress. */ get currentConcurrency(): number; }