import { TimeSpan } from '../conversion/TimeSpan'; /** * Configuration for a [[Semaphore]] instance. */ export interface SemaphoreConfiguration { /** * The maximum number of operations that can access the [[Semaphore]] concurrently. */ maximumConcurrentAccess: number; /** * The interval at which to check if new operations can access the [[Semaphore]]. * * Defaults to 5 milliseconds. */ checkInterval?: TimeSpan; } /** * Control structure for limiting concurrent access to a resource to a * specified number of operations. * ```typescript * // Restricts the number of concurrent HTTP DELETE requests to 5 * export class SimpleExample { * readonly #semaphore = new Semaphore({ maximumConcurrentAccess: 5 }); * readonly #timeout = TimeSpan.fromMinutes(1); * * public async delete(id: number): Promise { * await this.#semaphore.wait(this.#timeout); * try { * await axios.delete(`https://rate-limited-domain.org/items/${id}`); * } finally { * this.#semaphore.release(); * } * } * * public async drain(): Promise { * // Wait for all processes to exit * await this.#semaphore.flush(this.#timeout); * } * * public async destroy(): Promise { * // Terminate any waiting processes * this.#semaphore.abort(); * // Wait for all processes to exit the Semaphore * await this.#semaphore.flush(this.#timeout); * } * } * ``` */ export declare class Semaphore { #private; /** * Initializes a new instance of the [[Semaphore]] class. * @param configuration Configuration options for the new [[Semaphore]] instance. */ constructor(configuration: SemaphoreConfiguration); /** * Maximum operations allowed to access the [[Semaphore]] concurrently. */ get maximumOperations(): number; /** * Number of operations currently accessing the [[Semaphore]]. */ get currentOperations(): number; /** * Waits for access to the [[Semaphore]]. * @param timeout The amount of time to wait before giving up (defaults to no timeout). * @throws [[TimeoutError]] If the [[Semaphore]] does not become available within the timeout period. * @throws [[SemaphoreAbortedError]] If [[wait]] is called after [[abort]] is called. */ wait(timeout?: TimeSpan): Promise; /** * Informs the [[Semaphore]] that the current operation has finished. * @throws [[InvalidOperationError]] If all operations have already called [[release]]. */ release(): void; /** * Waits for all operations to exit the [[Semaphore]]. * @param timeout The amount of time to wait before giving up (defaults to no timeout). * @throws [[TimeoutError]] If all operations don't call [[release]] within the timeout period. */ flush(timeout?: TimeSpan): Promise; /** * Aborts any operations currently waiting on the [[Semaphore]]. * * NOTE: This method does not wait for all operations to exit the [[Semaphore]]. * Call [[flush]] after calling [[abort]] to wait for any currently-running operations. */ abort(): void; }