/** * Concurrency Control Utilities * * Provides a lightweight promise queue for controlling concurrent operations. * Similar to p-queue but without external dependencies. * * Use VASPERA_SCAN_CONCURRENCY env var to configure default concurrency. * * @module util/concurrency */ /** * Default concurrency level for scanner operations */ export declare const DEFAULT_CONCURRENCY = 3; /** * Get the configured concurrency level from environment */ export declare function getConcurrencyLevel(): number; /** * Options for the promise queue */ export interface QueueOptions { /** Maximum concurrent operations (default: from env or 3) */ concurrency?: number; /** Whether to auto-start processing (default: true) */ autoStart?: boolean; /** Timeout per task in ms (optional) */ timeout?: number; /** Callback when queue is empty */ onEmpty?: () => void; /** Callback when a task fails */ onError?: (error: Error, task: QueueTask) => void; } /** * Task in the queue */ export interface QueueTask { /** Unique task ID */ id: string; /** Task function */ fn: () => Promise; /** Task priority (higher = runs first) */ priority: number; /** When task was added */ addedAt: Date; /** Task timeout in ms */ timeout?: number; } /** * Task result */ export interface TaskResult { /** Task ID */ id: string; /** Whether task succeeded */ success: boolean; /** Result value if successful */ value?: T; /** Error if failed */ error?: Error; /** Execution time in ms */ durationMs: number; } /** * Queue statistics */ export interface QueueStats { /** Number of pending tasks */ pending: number; /** Number of running tasks */ running: number; /** Number of completed tasks */ completed: number; /** Number of failed tasks */ failed: number; /** Average execution time in ms */ avgDurationMs: number; /** Total execution time in ms */ totalDurationMs: number; } /** * Promise Queue for controlled concurrency * * Limits the number of concurrent async operations to prevent * resource exhaustion and improve reliability. * * @example * ```typescript * const queue = new PromiseQueue({ concurrency: 3 }); * * // Add tasks * queue.add(() => scanFile("file1.ts"), { priority: 1 }); * queue.add(() => scanFile("file2.ts"), { priority: 2 }); // runs first * * // Wait for all tasks * const results = await queue.onIdle(); * ``` */ export declare class PromiseQueue { private readonly concurrency; private readonly autoStart; private readonly defaultTimeout?; private readonly onEmpty?; private readonly onError?; private queue; private running; private results; private taskIdCounter; private isPaused; private idlePromise; private idleResolve; constructor(options?: QueueOptions); /** * Add a task to the queue */ add(fn: () => Promise, options?: { priority?: number; id?: string; timeout?: number; }): Promise>; /** * Add multiple tasks at once */ addAll(tasks: Array<{ fn: () => Promise; priority?: number; id?: string; timeout?: number; }>): Promise[]>; /** * Process tasks from the queue */ private process; /** * Run a single task */ private runTask; /** * Start processing (if paused) */ start(): void; /** * Pause processing (current tasks continue, new tasks wait) */ pause(): void; /** * Clear pending tasks (running tasks continue) */ clear(): void; /** * Get queue statistics */ getStats(): QueueStats; /** * Get current queue size */ get size(): number; /** * Get number of running tasks */ get pending(): number; /** * Check if queue is idle (no pending or running tasks) */ get isIdle(): boolean; /** * Wait for queue to become idle */ onIdle(): Promise[]>; } /** * Run tasks with controlled concurrency * * @param tasks - Array of async functions to execute * @param concurrency - Maximum concurrent operations * @returns Array of results in order * * @example * ```typescript * const results = await runConcurrent( * files.map(f => () => scanFile(f)), * 3 * ); * ``` */ export declare function runConcurrent(tasks: Array<() => Promise>, concurrency?: number): Promise[]>; /** * Run tasks with controlled concurrency, returning only successful values * * @param tasks - Array of async functions to execute * @param concurrency - Maximum concurrent operations * @returns Array of successful results in order (null for failed) */ export declare function runConcurrentValues(tasks: Array<() => Promise>, concurrency?: number): Promise<(T | null)[]>; /** * Map over items with controlled concurrency * * @param items - Items to process * @param fn - Async function to apply to each item * @param concurrency - Maximum concurrent operations * @returns Array of results */ export declare function mapConcurrent(items: T[], fn: (item: T, index: number) => Promise, concurrency?: number): Promise[]>; /** * Create a throttled version of an async function * * @param fn - Async function to throttle * @param concurrency - Maximum concurrent calls * @returns Throttled function */ export declare function throttle(fn: (...args: T) => Promise, concurrency?: number): (...args: T) => Promise>; /** * Batch items and process batches with controlled concurrency * * @param items - Items to process * @param batchSize - Size of each batch * @param fn - Async function to process a batch * @param concurrency - Maximum concurrent batches */ export declare function batchConcurrent(items: T[], batchSize: number, fn: (batch: T[], batchIndex: number) => Promise, concurrency?: number): Promise[]>; //# sourceMappingURL=concurrency.d.ts.map