Skip to content

Class: FiberPool

Package: @litert/concurrentImport path: @litert/concurrentSource: packages/partials/concurrent/src/Classes/FiberPool.tsExtends: EventEmitter<IFiberPoolEvents>

A pool of reusable FiberController workers. Fibers are created on demand up to maxFibers and kept idle (up to maxIdleFibers) for reuse. Idle fibers above minIdleFibers are released after idleTimeout ms.


Constructor

ts
new FiberPool(options: IFiberPoolOptions)

See IFiberPoolOptions.


Constants

ConstantValueDescription
DEFAULT_MIN_IDLE_FIBERS1Default minIdleFibers
DEFAULT_MAX_IDLE_FIBERS1Default maxIdleFibers
DEFAULT_IDLE_TIMEOUT10000Default idle timeout (ms)
DEFAULT_WAIT_TIMEOUT10000Default wait timeout for run() (ms)
DEFAULT_MAX_WAITS10Default max concurrent callers waiting for a free fiber

Properties

PropertyTypeDescription
idleFibersnumber (readonly)Current number of idle fibers
busyFibersnumber (readonly)Current number of fibers executing a task

Methods

run(opts)

ts
run<TData, TResult>(opts: IRunOptions<TData, TResult>): Promise<TResult>

Picks an idle fiber (or creates a new one if under maxFibers), executes opts.function(opts.data) inside it, and returns the result.

If all fibers are busy, waits up to waitTimeout ms for one to become available. If maxWaits callers are already waiting, the call is rejected immediately.

ParameterTypeDescription
optsIRunOptions<TData, TResult>Task options — see IRunOptions

Returns: Promise<TResult> — the value returned by opts.function.

Throws:

  • E_TIMEOUT — if no fiber became available within waitTimeout ms.
  • Any error thrown by opts.function.

close()

ts
close(): void

Closes all idle fibers and prevents new tasks from being submitted.


isClosed()

ts
isClosed(): boolean

Returns true if the pool has been closed.


Events

Event 'error'

The 'error' event is emitted when an internal error occurs that the fiber pool cannot handle by itself.

WARNING

To prevent unhandled exceptions that may crash the program, you MUST ALWAYS listen on the 'error' event.

ts
type IErrorEventCallback = (error: unknown) => void;

Scoped Types

Interface IFiberPoolOptions

Source: FiberPool.ts

ts
import type { IFiberPoolOptions } from '@litert/concurrent';
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

Interface IRunOptions<TData, TResult>

Source: FiberPool.ts

ts
import type { IRunOptions } from '@litert/concurrent';
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

Interface IFiberPoolEvents

Source: FiberPool.ts

ts
import type { IFiberPoolEvents } from '@litert/concurrent';
ts
interface IFiberPoolEvents {
    'error': [error: unknown];
}
EventPayloadDescription
'error'error: unknownEmitted when a fiber encounters an unhandled error

Type Alias IFiberFunction<TData, TResult>

Source: FiberPool.ts

ts
import type { IFiberFunction } from '@litert/concurrent';
ts
type IFiberFunction<TData, TResult> = (data: TData) => Promise<TResult>;

The type for async functions executed inside a FiberPool fiber.


Example

ts
import { FiberPool } from '@litert/concurrent';

const pool = new FiberPool({
    maxFibers: 4,
    maxIdleFibers: 2,
    minIdleFibers: 1,
});

const result = await pool.run({
    data: { userId: 42 },
    function: async ({ userId }) => {
        return fetchUser(userId);
    },
});

console.log(result);

pool.close();