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
new FiberPool(options: IFiberPoolOptions)See IFiberPoolOptions.
Constants
| Constant | Value | Description |
|---|---|---|
DEFAULT_MIN_IDLE_FIBERS | 1 | Default minIdleFibers |
DEFAULT_MAX_IDLE_FIBERS | 1 | Default maxIdleFibers |
DEFAULT_IDLE_TIMEOUT | 10000 | Default idle timeout (ms) |
DEFAULT_WAIT_TIMEOUT | 10000 | Default wait timeout for run() (ms) |
DEFAULT_MAX_WAITS | 10 | Default max concurrent callers waiting for a free fiber |
Properties
| Property | Type | Description |
|---|---|---|
idleFibers | number (readonly) | Current number of idle fibers |
busyFibers | number (readonly) | Current number of fibers executing a task |
Methods
run(opts)
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.
| Parameter | Type | Description |
|---|---|---|
opts | IRunOptions<TData, TResult> | Task options — see IRunOptions |
Returns: Promise<TResult> — the value returned by opts.function.
Throws:
TimeoutError— if no fiber became available withinwaitTimeoutms.- Any error thrown by
opts.function.
close()
close(): voidCloses all idle fibers and prevents new tasks from being submitted.
isClosed()
isClosed(): booleanReturns true if the pool has been closed.
Events
See IFiberPoolEvents.
| Event | Description |
|---|---|
'error' | Emitted on internal unhandled fiber errors |
Example
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();