Skip to content

Typings — Concurrent

Package: @litert/concurrentSource: packages/partials/concurrent/src/Types.ts, packages/partials/concurrent/src/Classes/


Core Interfaces

ICounter

A generic counter interface.

ts
interface ICounter {
    getTotal(): number;
    increase(step?: number): void;
    reset(): void;
}
MethodDescription
getTotal()Returns the current count
increase(step?)Increments the counter by step (default 1)
reset()Resets the counter to zero

ISimpleFn

A function that takes no parameters.

ts
type ISimpleFn = IFunction<[]>;

IBreaker

Interface for all circuit breaker implementations.

ts
interface IBreaker {
    call<T extends ISimpleFn>(fn: T): ReturnType<T>;
    wrap<T extends ISimpleFn>(fn: T): T;
}
MethodDescription
call<T>(fn)Calls fn if the breaker is closed; throws if open
wrap<T>(fn)Returns a new function that passes through the breaker

ISyncRateLimiter

Interface for synchronous rate limiters.

ts
interface ISyncRateLimiter {
    isBlocking(): boolean;
    isIdle(): boolean;
    challenge(): void;
    reset(): void;
    call<T extends IFunction>(fn: T): ReturnType<T>;
    wrap<T extends IFunction>(fn: T): T;
}
MethodDescription
isBlocking()Returns true if the limiter is currently blocking all calls
isIdle()Returns true if there is zero traffic
challenge()Passes or throws; increments internal state on pass
reset()Resets internal state
call<T>(fn)Calls fn after a challenge(); throws if limited
wrap<T>(fn)Wraps fn so every call goes through challenge()

ISyncRateLimiterManager

Interface for managing multiple keyed synchronous rate limiters.

ts
interface ISyncRateLimiterManager {
    clean(): void;
    isBlocking(key: string): boolean;
    challenge(key: string): void;
    reset(key: string): void;
    call<T extends IFunction>(key: string, fn: T): ReturnType<T>;
}
MethodDescription
clean()Removes unused/idle limiter contexts
isBlocking(key)Returns true if key is currently blocked
challenge(key)Passes or throws for key
reset(key)Resets state for key
call<T>(key, fn)Calls fn after challenging key

IAsyncRateLimiter

Interface for asynchronous rate limiters.

ts
interface IAsyncRateLimiter {
    isBlocking(): boolean;
    isIdle(): boolean;
    challenge(): Promise<void>;
    reset(): void;
    call<T extends IFunction>(fn: T): Promise<Awaited<ReturnType<T>>>;
    wrap<T extends IFunction>(fn: T): IToPromise<T>;
}
MethodDescription
isBlocking()Returns true if the limiter would cause any call to wait
isIdle()Returns true if there is zero pending traffic
challenge()Resolves (possibly after a delay) or throws if over capacity
reset()Resets internal queue/state
call<T>(fn)Calls fn after awaiting challenge()
wrap<T>(fn)Wraps fn as an async function going through challenge()

IAsyncRateLimiterManager

Interface for managing multiple keyed asynchronous rate limiters.

ts
interface IAsyncRateLimiterManager {
    isBlocking(key: string): boolean;
    challenge(key: string): Promise<void>;
    reset(key: string): void;
    clean(): void;
    call<T extends IFunction>(key: string, fn: T): Promise<Awaited<ReturnType<T>>>;
}
MethodDescription
isBlocking(key)Returns true if key is currently at capacity
challenge(key)Awaits until key's slot opens, or throws if over capacity
reset(key)Resets state for key
clean()Removes idle contexts
call<T>(key, fn)Calls fn after awaiting the challenge for key

Option Interfaces

IDebounceOptions

Used by DebounceController.

ts
interface IDebounceOptions {
    function: IDebouncingFunction;
    delayMs: number;
    maxDelayMs?: number;
}
PropertyTypeDescription
function() => voidThe function to debounce
delayMsnumberMilliseconds to wait after the last schedule() call
maxDelayMsnumber?Hard deadline — triggers immediately if elapsed exceeds this; default Number.MAX_SAFE_INTEGER

IBatchBufferOptions<T>

Used by BatchBuffer<T>.

ts
interface IBatchBufferOptions<T> extends Pick<IDebounceOptions, 'delayMs' | 'maxDelayMs'> {
    maxSize: number;
    callback: (items: T[]) => void;
}
PropertyTypeDescription
delayMsnumberFlush delay in milliseconds (from last push())
maxDelayMsnumber?Max flush delay from the first push() after an empty buffer
maxSizenumberFlush immediately when buffer reaches this count
callback(items: T[]) => voidReceives the flushed batch

ISlideWindowCounterOptions

Used by SlideWindowCounter.

ts
interface ISlideWindowCounterOptions {
    windowSizeMs?: number;
    windowQty?: number;
}
PropertyTypeDefaultDescription
windowSizeMsnumber?10000Total slide window duration in milliseconds
windowQtynumber?3Number of sub-windows to divide the total window into

ICountingRateLimiterOptions

Used by CountingRateLimiter.

ts
interface ICountingRateLimiterOptions {
    limits: number;
    counter: ICounter;
    errorCtorOnLimited?: IConstructor<Error>;
}
PropertyTypeDescription
limitsnumberMaximum count before blocking
counterICounterThe counter instance to track calls
errorCtorOnLimitedIConstructor<Error>?Custom error class; defaults to E_RATE_LIMITED

ITokenBucketRateLimiterOptions

Used by TokenBucketRateLimiter.

ts
interface ITokenBucketRateLimiterOptions {
    capacity: number;
    initialTokens?: number;
    refillIntervalMs: number;
    errorCtorOnLimited?: IConstructor<Error>;
}
PropertyTypeDefaultDescription
capacitynumberMax token count in the bucket
initialTokensnumber?capacityStarting token count
refillIntervalMsnumberMilliseconds per token refill
errorCtorOnLimitedIConstructor<Error>?E_RATE_LIMITEDCustom error class

ILeakyBucketRateLimiterOptions

Used by LeakyBucketRateLimiter.

ts
interface ILeakyBucketRateLimiterOptions {
    capacity: number;
    leakIntervalMs: number;
    errorCtorOnLimited?: IConstructor<Error>;
}
PropertyTypeDescription
capacitynumberMaximum pending tasks (queue depth)
leakIntervalMsnumberMilliseconds between each task being "leaked" through
errorCtorOnLimitedIConstructor<Error>?Custom error class; defaults to E_RATE_LIMITED

ITokenBucketRateLimiterManagerOptions

Used by TokenBucketRateLimiterManager.

ts
interface ITokenBucketRateLimiterManagerOptions {
    capacity: number;
    initialTokens?: number;
    refillIntervalMs: number;
    cleanDelayMs?: number;
    errorCtorOnLimited?: IConstructor<Error>;
}
PropertyTypeDefaultDescription
capacitynumberBucket capacity per key
initialTokensnumber?capacityInitial tokens per key
refillIntervalMsnumberMilliseconds per token refill
cleanDelayMsnumber?0How long (ms) to keep a full, unused context before cleaning; 0 = disabled
errorCtorOnLimitedIConstructor<Error>?E_RATE_LIMITEDCustom error class

ILeakyBucketRateLimiterManagerOptions

Used by LeakyBucketRateLimiterManager.

ts
interface ILeakyBucketRateLimiterManagerOptions {
    capacity: number;
    leakIntervalMs: number;
    cleanDelayMs?: number;
    errorCtorOnLimited?: IConstructor<Error>;
}
PropertyTypeDefaultDescription
capacitynumberMax pending tasks per key
leakIntervalMsnumberMilliseconds per task leakage
cleanDelayMsnumber?0Keep idle context for this long before cleaning; 0 = disabled
errorCtorOnLimitedIConstructor<Error>?E_RATE_LIMITEDCustom error class

IMemoryMutexOptions

Used by MemoryMutex.

ts
interface IMemoryMutexOptions {
    reentrant?: boolean;
}
PropertyTypeDefaultDescription
reentrantboolean?falseIf true, the same instance can re-acquire the lock it already holds

IFiberPoolOptions

Used by FiberPool.

ts
interface IFiberPoolOptions {
    maxFibers: number;
    maxIdleFibers?: number;
    minIdleFibers?: number;
    idleTimeout?: number;
    defaultWaitTimeout?: number;
    maxWaits?: number;
}
PropertyTypeDefaultDescription
maxFibersnumberMaximum total fibers (idle + busy)
maxIdleFibersnumber?1Maximum idle fibers to keep alive
minIdleFibersnumber?1Minimum idle fibers — never closed even after idle timeout
idleTimeoutnumber?10000Milliseconds before an idle fiber above minIdleFibers is released
defaultWaitTimeoutnumber?10000Default wait timeout for run() when no fiber is available
maxWaitsnumber?10Maximum concurrent waiters for a free fiber

IRunOptions<TData, TResult>

Used by FiberPool.run().

ts
interface IRunOptions<TData, TResult> {
    waitTimeout?: number;
    data: TData;
    function: IFiberFunction<TData, TResult>;
}
PropertyTypeDescription
waitTimeoutnumber?Override defaultWaitTimeout for this call
dataTDataArgument passed to function
function(data: TData) => Promise<TResult>The task to run inside the fiber

ICircuitBreakerOptions

Used by CircuitBreaker.

ts
interface ICircuitBreakerOptions {
    cooldownTimeMs?: number;
    breakThreshold?: number;
    warmupThreshold?: number;
    isFailure?: (error: unknown) => boolean;
    errorCtorOnOpen?: IConstructor<Error>;
    counter?: ICounter;
}
PropertyTypeDefaultDescription
cooldownTimeMsnumber?60000Time (ms) after which the breaker enters half-open state
breakThresholdnumber?5Failure count to trigger open state
warmupThresholdnumber?3Consecutive successes needed to close from half-open
isFailure(err) => boolean?() => trueDetermines whether a thrown error counts as a failure
errorCtorOnOpenIConstructor<Error>?E_BREAKER_OPENEDCustom error class
counterICounter?SlideWindowCounter({ windowSizeMs: 10000, windowQty: 6 })Custom counter for failure tracking

Event Interfaces

IDebounceControllerEvents

Emitted by DebounceController.

ts
interface IDebounceControllerEvents {
    'error': [error: unknown];
    'triggered': [];
}
EventPayloadDescription
'error'error: unknownEmitted when the debounced function throws
'triggered'Emitted after the debounced function runs successfully

IFiberPoolEvents

Emitted by FiberPool.

ts
interface IFiberPoolEvents {
    'error': [error: unknown];
}
EventPayloadDescription
'error'error: unknownEmitted when a fiber encounters an unhandled error

ICircuitBreakerEvents

Emitted by CircuitBreaker.

ts
interface ICircuitBreakerEvents {
    'error': [error: unknown];
    'opened': [];
    'half_opened': [];
    'closed': [];
}
EventPayloadDescription
'error'error: unknownEmitted when the guarded function throws (failures counted)
'opened'Emitted when the breaker transitions to open state
'half_opened'Emitted when the breaker enters half-open state
'closed'Emitted when the breaker returns to closed (normal) state

Type Aliases

IDebouncingFunction

ts
type IDebouncingFunction = () => void;

The type for functions that can be wrapped by DebounceController.


IFiberFunction<TData, TResult>

ts
type IFiberFunction<TData, TResult> = (data: TData) => Promise<TResult>;

The type for async functions executed inside a FiberPool fiber.