Offload functions to worker threads with shared memory primitives for Node.js.
8
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: add Balancer interface, thread and activeCount to WorkerHandle

+17
+17
src/runner.ts
··· 1 + import type { Worker } from 'node:worker_threads'; 1 2 import type { Task } from './task.ts'; 2 3 import type { StreamTask } from './stream-task.ts'; 3 4 import type { ChannelOptions } from './channel.ts'; 4 5 5 6 type TaskResults<T extends Task<any>[]> = { [K in keyof T]: T[K] extends Task<infer R> ? R : never }; 6 7 8 + /** A load balancing strategy for choosing which worker runs a task. */ 9 + export interface Balancer { 10 + /** Choose a worker for the given task. Called synchronously on every dispatch. */ 11 + select(workers: readonly WorkerHandle[], task: Task<any> | StreamTask<any>): WorkerHandle; 12 + /** Optional cleanup on sync dispose. */ 13 + [Symbol.dispose]?(): void; 14 + /** Optional cleanup on async dispose. */ 15 + [Symbol.asyncDispose]?(): Promise<void>; 16 + } 17 + 7 18 /** Options for configuring a worker pool. */ 8 19 export interface WorkerOptions { 9 20 /** Maximum time in ms to wait for in-flight tasks during async dispose. If exceeded, workers are force-terminated. */ 10 21 shutdownTimeout?: number; 22 + /** Load balancing strategy. Defaults to round-robin. */ 23 + balance?: Balancer; 11 24 } 12 25 13 26 /** A handle to a specific worker in a pool. */ ··· 16 29 exec<T>(task: Task<T>): Promise<T>; 17 30 /** Dispatches a streaming task pinned to this worker. */ 18 31 exec<T>(task: StreamTask<T>, opts?: ChannelOptions): AsyncIterable<T>; 32 + /** The underlying worker thread. */ 33 + readonly thread: Worker; 34 + /** Number of currently in-flight tasks on this worker. */ 35 + readonly activeCount: number; 19 36 } 20 37 21 38 /**