Semaphore
Semaphore
Async semaphore implementation that controls access to a limited pool of resources. Allows a specified number of concurrent operations while blocking additional requests.
Signature
class Semaphore {
constructor(options: SemaphoreOptions = {})
setStorage(storage: SemaphoreStorage) => ;
getStorage() => SemaphoreStorage;
acquire(key: string, timeout?: number, signal?: AbortSignal) => Promise<boolean>;
release(key: string) => Promise<void>;
getAvailablePermits(key: string) => Promise<number>;
getTotalPermits(key: string) => Promise<number>;
getAcquiredPermits(key: string) => Promise<number>;
withPermit(key: string, fn: () => Promise<T> | T, timeout?: number, signal?: AbortSignal) => Promise<T>;
getConfig() => Omit<SemaphoreOptions, 'storage'>;
}
constructor
method
(options: SemaphoreOptions = {}) => Semaphore
Creates a new semaphore instance
setStorage
method
(storage: SemaphoreStorage) =>
Sets the storage implementation for the semaphore
getStorage
method
() => SemaphoreStorage
Gets the storage implementation for the semaphore
acquire
method
(key: string, timeout?: number, signal?: AbortSignal) => Promise<boolean>
Acquires a permit for the given key
release
method
(key: string) => Promise<void>
Releases a permit for the given key
getAvailablePermits
method
(key: string) => Promise<number>
Gets the number of available permits for the given key
getTotalPermits
method
(key: string) => Promise<number>
Gets the total number of permits for the given key
getAcquiredPermits
method
(key: string) => Promise<number>
Gets the number of acquired permits for the given key
withPermit
method
(key: string, fn: () => Promise<T> | T, timeout?: number, signal?: AbortSignal) => Promise<T>
Executes a function with a permit from the semaphore
getConfig
method
() => Omit<SemaphoreOptions, 'storage'>
Gets the current configuration of the semaphore