poolifier 5.1.7 → 5.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.d.ts CHANGED
@@ -88,11 +88,11 @@ declare const Measurements: Readonly<{
88
88
  interface IWorkerChoiceStrategy {
89
89
  /**
90
90
  * Chooses a worker node in the pool and returns its key.
91
- * If no worker nodes are not eligible, `undefined` is returned.
92
- * If `undefined` is returned, the caller retry.
91
+ * If no worker nodes are eligible, `undefined` is returned and the caller retries.
92
+ * @param workerNodeKeysSet - The worker node keys affinity set. If undefined, all workers are eligible.
93
93
  * @returns The worker node key or `undefined`.
94
94
  */
95
- readonly choose: () => number | undefined;
95
+ readonly choose: (workerNodeKeysSet?: ReadonlySet<number>) => number | undefined;
96
96
  /**
97
97
  * The worker choice strategy name.
98
98
  */
@@ -276,8 +276,8 @@ interface WorkerOptions {
276
276
 
277
277
  /**
278
278
  * Message object that is passed between main worker and worker.
279
- * @typeParam Data - Type of data sent to the worker or execution response. This can only be structured-cloneable data.
280
- * @typeParam ErrorData - Type of data sent to the worker triggering an error. This can only be structured-cloneable data.
279
+ * @template Data - Type of data sent to the worker or execution response. This can only be structured-cloneable data.
280
+ * @template ErrorData - Type of data sent to the worker triggering an error. This can only be structured-cloneable data.
281
281
  * @internal
282
282
  */
283
283
  interface MessageValue<Data = unknown, ErrorData = unknown> extends Task<Data> {
@@ -344,7 +344,7 @@ interface MessageValue<Data = unknown, ErrorData = unknown> extends Task<Data> {
344
344
  }
345
345
  /**
346
346
  * An object holding the task execution response promise resolve/reject callbacks.
347
- * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
347
+ * @template Response - Type of execution response. This can only be structured-cloneable data.
348
348
  * @internal
349
349
  */
350
350
  interface PromiseResponseWrapper<Response = unknown> {
@@ -371,7 +371,7 @@ interface PromiseResponseWrapper<Response = unknown> {
371
371
  }
372
372
  /**
373
373
  * Message object that is passed as a task between main worker and worker.
374
- * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
374
+ * @template Data - Type of data sent to the worker. This can only be structured-cloneable data.
375
375
  * @internal
376
376
  */
377
377
  interface Task<Data = unknown> {
@@ -425,6 +425,13 @@ interface TaskFunctionProperties {
425
425
  * Task function worker choice strategy.
426
426
  */
427
427
  readonly strategy?: WorkerChoiceStrategy;
428
+ /**
429
+ * Task function worker node keys affinity.
430
+ * Restricts task execution to specified worker nodes by their indices.
431
+ * Must contain valid indices within [0, pool max size - 1].
432
+ * If undefined, task can execute on any worker node.
433
+ */
434
+ readonly workerNodeKeys?: number[];
428
435
  }
429
436
  /**
430
437
  * Task performance.
@@ -450,7 +457,7 @@ interface TaskPerformance {
450
457
  }
451
458
  /**
452
459
  * Worker error.
453
- * @typeParam Data - Type of data sent to the worker triggering an error. This can only be structured-cloneable data.
460
+ * @template Data - Type of data sent to the worker triggering an error. This can only be structured-cloneable data.
454
461
  */
455
462
  interface WorkerError<Data = unknown> {
456
463
  /**
@@ -494,7 +501,7 @@ interface WorkerStatistics {
494
501
  }
495
502
  /**
496
503
  * Remove readonly modifier from all properties of T.
497
- * @typeParam T - Type to remove readonly modifier.
504
+ * @template T - Type to remove readonly modifier.
498
505
  * @internal
499
506
  */
500
507
  type Writable<T> = {
@@ -506,21 +513,21 @@ type Writable<T> = {
506
513
  * This function must return a promise.
507
514
  * @param data - Data sent to the worker.
508
515
  * @returns Execution response promise.
509
- * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
510
- * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
516
+ * @template Data - Type of data sent to the worker. This can only be structured-cloneable data.
517
+ * @template Response - Type of execution response. This can only be structured-cloneable data.
511
518
  */
512
519
  type TaskAsyncFunction<Data = unknown, Response = unknown> = (data?: Data) => Promise<Response>;
513
520
  /**
514
521
  * Task function that can be executed.
515
522
  * This function can be synchronous or asynchronous.
516
- * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
517
- * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
523
+ * @template Data - Type of data sent to the worker. This can only be structured-cloneable data.
524
+ * @template Response - Type of execution response. This can only be structured-cloneable data.
518
525
  */
519
526
  type TaskFunction<Data = unknown, Response = unknown> = TaskAsyncFunction<Data, Response> | TaskSyncFunction<Data, Response>;
520
527
  /**
521
528
  * Task function object.
522
- * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
523
- * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
529
+ * @template Data - Type of data sent to the worker. This can only be structured-cloneable data.
530
+ * @template Response - Type of execution response. This can only be structured-cloneable data.
524
531
  */
525
532
  interface TaskFunctionObject<Data = unknown, Response = unknown> {
526
533
  /**
@@ -535,6 +542,13 @@ interface TaskFunctionObject<Data = unknown, Response = unknown> {
535
542
  * Task function.
536
543
  */
537
544
  taskFunction: TaskFunction<Data, Response>;
545
+ /**
546
+ * Task function worker node keys affinity.
547
+ * Restricts task execution to specified worker nodes by their indices.
548
+ * Must contain valid indices within [0, pool max size - 1].
549
+ * If undefined, task can execute on any worker node.
550
+ */
551
+ workerNodeKeys?: number[];
538
552
  }
539
553
  /**
540
554
  * Task function operation result.
@@ -547,22 +561,22 @@ interface TaskFunctionOperationResult {
547
561
  * Tasks functions that can be executed.
548
562
  * The key is the name of the task function or task function object.
549
563
  * The value is the task function or task function object.
550
- * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
551
- * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
564
+ * @template Data - Type of data sent to the worker. This can only be structured-cloneable data.
565
+ * @template Response - Type of execution response. This can only be structured-cloneable data.
552
566
  */
553
567
  type TaskFunctions<Data = unknown, Response = unknown> = Record<string, TaskFunction<Data, Response> | TaskFunctionObject<Data, Response>>;
554
568
  /**
555
569
  * Task synchronous function that can be executed.
556
570
  * @param data - Data sent to the worker.
557
571
  * @returns Execution response.
558
- * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
559
- * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
572
+ * @template Data - Type of data sent to the worker. This can only be structured-cloneable data.
573
+ * @template Response - Type of execution response. This can only be structured-cloneable data.
560
574
  */
561
575
  type TaskSyncFunction<Data = unknown, Response = unknown> = (data?: Data) => Response;
562
576
 
563
577
  /**
564
578
  * Priority queue.
565
- * @typeParam T - Type of priority queue data.
579
+ * @template T - Type of priority queue data.
566
580
  * @internal
567
581
  */
568
582
  declare class PriorityQueue<T> {
@@ -633,27 +647,27 @@ declare class PriorityQueue<T> {
633
647
 
634
648
  /**
635
649
  * Callback invoked if the worker raised an error.
636
- * @typeParam Worker - Type of worker.
650
+ * @template Worker - Type of worker.
637
651
  */
638
652
  type ErrorHandler<Worker extends IWorker> = (this: Worker, error: Error) => void;
639
653
  /**
640
654
  * Worker event handler.
641
- * @typeParam Worker - Type of worker.
655
+ * @template Worker - Type of worker.
642
656
  */
643
657
  type EventHandler<Worker extends IWorker> = ErrorHandler<Worker> | ExitHandler<Worker> | MessageHandler<Worker> | OnlineHandler<Worker>;
644
658
  /**
645
659
  * Callback invoked when the worker exits successfully.
646
- * @typeParam Worker - Type of worker.
660
+ * @template Worker - Type of worker.
647
661
  */
648
662
  type ExitHandler<Worker extends IWorker> = (this: Worker, exitCode: number) => void;
649
663
  /**
650
664
  * Callback invoked if the worker has received a message.
651
- * @typeParam Worker - Type of worker.
665
+ * @template Worker - Type of worker.
652
666
  */
653
667
  type MessageHandler<Worker extends IWorker> = (this: Worker, message: unknown) => void;
654
668
  /**
655
669
  * Callback invoked when the worker has started successfully.
656
- * @typeParam Worker - Type of worker.
670
+ * @template Worker - Type of worker.
657
671
  */
658
672
  type OnlineHandler<Worker extends IWorker> = (this: Worker) => void;
659
673
  /**
@@ -783,8 +797,8 @@ interface IWorker extends EventEmitter {
783
797
  }
784
798
  /**
785
799
  * Worker node interface.
786
- * @typeParam Worker - Type of worker.
787
- * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
800
+ * @template Worker - Type of worker.
801
+ * @template Data - Type of data sent to the worker. This can only be structured-cloneable data.
788
802
  * @internal
789
803
  */
790
804
  interface IWorkerNode<Worker extends IWorker, Data = unknown> extends EventEmitter {
@@ -1024,9 +1038,9 @@ declare const PoolEvents: Readonly<{
1024
1038
  }>;
1025
1039
  /**
1026
1040
  * Contract definition for a poolifier pool.
1027
- * @typeParam Worker - Type of worker which manages this pool.
1028
- * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
1029
- * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
1041
+ * @template Worker - Type of worker which manages this pool.
1042
+ * @template Data - Type of data sent to the worker. This can only be structured-cloneable data.
1043
+ * @template Response - Type of execution response. This can only be structured-cloneable data.
1030
1044
  */
1031
1045
  interface IPool<Worker extends IWorker, Data = unknown, Response = unknown> {
1032
1046
  /**
@@ -1211,7 +1225,7 @@ interface PoolInfo {
1211
1225
  }
1212
1226
  /**
1213
1227
  * Options for a poolifier pool.
1214
- * @typeParam Worker - Type of worker.
1228
+ * @template Worker - Type of worker.
1215
1229
  */
1216
1230
  interface PoolOptions<Worker extends IWorker> {
1217
1231
  /**
@@ -1320,9 +1334,9 @@ interface TasksQueueOptions {
1320
1334
 
1321
1335
  /**
1322
1336
  * The worker choice strategies context.
1323
- * @typeParam Worker - Type of worker.
1324
- * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
1325
- * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
1337
+ * @template Worker - Type of worker.
1338
+ * @template Data - Type of data sent to the worker. This can only be structured-cloneable data.
1339
+ * @template Response - Type of execution response. This can only be structured-cloneable data.
1326
1340
  * @internal
1327
1341
  */
1328
1342
  declare class WorkerChoiceStrategiesContext<Worker extends IWorker, Data = unknown, Response = unknown> {
@@ -1356,13 +1370,13 @@ declare class WorkerChoiceStrategiesContext<Worker extends IWorker, Data = unkno
1356
1370
  */
1357
1371
  constructor(pool: IPool<Worker, Data, Response>, workerChoiceStrategies?: WorkerChoiceStrategy[], opts?: WorkerChoiceStrategyOptions);
1358
1372
  /**
1359
- * Executes the given worker choice strategy in the context algorithm.
1360
- * @param workerChoiceStrategy - The worker choice strategy algorithm to execute.
1361
- * @defaultValue this.defaultWorkerChoiceStrategy
1373
+ * Executes the given worker choice strategy.
1374
+ * @param workerChoiceStrategy - The worker choice strategy.
1375
+ * @param workerNodeKeysSet - The worker node keys affinity set. If undefined, all workers are eligible.
1362
1376
  * @returns The key of the worker node.
1363
1377
  * @throws {Error} If after computed retries the worker node key is null or undefined.
1364
1378
  */
1365
- execute(workerChoiceStrategy?: WorkerChoiceStrategy): number;
1379
+ execute(workerChoiceStrategy?: WorkerChoiceStrategy, workerNodeKeysSet?: ReadonlySet<number>): number;
1366
1380
  /**
1367
1381
  * Gets the active worker choice strategies in the context policy.
1368
1382
  * @returns The strategies policy.
@@ -1416,8 +1430,9 @@ declare class WorkerChoiceStrategiesContext<Worker extends IWorker, Data = unkno
1416
1430
  */
1417
1431
  private addWorkerChoiceStrategy;
1418
1432
  /**
1419
- * Executes the given worker choice strategy.
1420
- * @param workerChoiceStrategy - The worker choice strategy.
1433
+ * Executes the given worker choice strategy in the context algorithm.
1434
+ * @param workerChoiceStrategy - The worker choice strategy algorithm to execute.
1435
+ * @param workerNodeKeysSet - The worker node keys affinity set. If undefined, all workers are eligible.
1421
1436
  * @returns The key of the worker node.
1422
1437
  * @throws {Error} If after computed retries the worker node key is null or undefined.
1423
1438
  */
@@ -1432,9 +1447,9 @@ declare class WorkerChoiceStrategiesContext<Worker extends IWorker, Data = unkno
1432
1447
 
1433
1448
  /**
1434
1449
  * Base class that implements some shared logic for all poolifier pools.
1435
- * @typeParam Worker - Type of worker which manages this pool.
1436
- * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
1437
- * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
1450
+ * @template Worker - Type of worker which manages this pool.
1451
+ * @template Data - Type of data sent to the worker. This can only be structured-cloneable data.
1452
+ * @template Response - Type of execution response. This can only be structured-cloneable data.
1438
1453
  */
1439
1454
  declare abstract class AbstractPool<Worker extends IWorker, Data = unknown, Response = unknown> implements IPool<Worker, Data, Response> {
1440
1455
  protected readonly minimumNumberOfWorkers: number;
@@ -1726,6 +1741,12 @@ declare abstract class AbstractPool<Worker extends IWorker, Data = unknown, Resp
1726
1741
  * @returns The task function worker choice strategy if the task function worker choice strategy is defined, `undefined` otherwise.
1727
1742
  */
1728
1743
  private readonly getTaskFunctionWorkerChoiceStrategy;
1744
+ /**
1745
+ * Gets task function worker node keys affinity set, if any.
1746
+ * @param name - The task function name.
1747
+ * @returns The task function worker node keys affinity set, or `undefined` if not defined.
1748
+ */
1749
+ private readonly getTaskFunctionWorkerNodeKeysSet;
1729
1750
  private getTasksQueuePriority;
1730
1751
  /**
1731
1752
  * Gets the worker choice strategies registered in this pool.
@@ -1817,8 +1838,8 @@ declare abstract class AbstractPool<Worker extends IWorker, Data = unknown, Resp
1817
1838
  type ClusterPoolOptions = PoolOptions<Worker>;
1818
1839
  /**
1819
1840
  * A cluster pool with a fixed number of workers.
1820
- * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
1821
- * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
1841
+ * @template Data - Type of data sent to the worker. This can only be structured-cloneable data.
1842
+ * @template Response - Type of execution response. This can only be structured-cloneable data.
1822
1843
  * @author [Christopher Quadflieg](https://github.com/Shinigami92)
1823
1844
  * @since 2.0.0
1824
1845
  */
@@ -1866,8 +1887,8 @@ declare class FixedClusterPool<Data = unknown, Response = unknown> extends Abstr
1866
1887
  *
1867
1888
  * This cluster pool creates new workers when the others are busy, up to the maximum number of workers.
1868
1889
  * When the maximum number of workers is reached and workers are busy, an event is emitted. If you want to listen to this event, use the pool's `emitter`.
1869
- * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
1870
- * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
1890
+ * @template Data - Type of data sent to the worker. This can only be structured-cloneable data.
1891
+ * @template Response - Type of execution response. This can only be structured-cloneable data.
1871
1892
  * @author [Christopher Quadflieg](https://github.com/Shinigami92)
1872
1893
  * @since 2.0.0
1873
1894
  */
@@ -1918,8 +1939,8 @@ declare class DynamicClusterPool<Data = unknown, Response = unknown> extends Fix
1918
1939
  type ThreadPoolOptions = PoolOptions<Worker$1>;
1919
1940
  /**
1920
1941
  * A thread pool with a fixed number of threads.
1921
- * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
1922
- * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
1942
+ * @template Data - Type of data sent to the worker. This can only be structured-cloneable data.
1943
+ * @template Response - Type of execution response. This can only be structured-cloneable data.
1923
1944
  * @author [Alessandro Pio Ardizio](https://github.com/pioardi)
1924
1945
  * @since 0.0.1
1925
1946
  */
@@ -1965,8 +1986,8 @@ declare class FixedThreadPool<Data = unknown, Response = unknown> extends Abstra
1965
1986
  *
1966
1987
  * This thread pool creates new threads when the others are busy, up to the maximum number of threads.
1967
1988
  * When the maximum number of threads is reached and workers are busy, an event is emitted. If you want to listen to this event, use the pool's `emitter`.
1968
- * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
1969
- * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
1989
+ * @template Data - Type of data sent to the worker. This can only be structured-cloneable data.
1990
+ * @template Response - Type of execution response. This can only be structured-cloneable data.
1970
1991
  * @author [Alessandro Pio Ardizio](https://github.com/pioardi)
1971
1992
  * @since 0.0.1
1972
1993
  */
@@ -2013,7 +2034,7 @@ declare class DynamicThreadPool<Data = unknown, Response = unknown> extends Fixe
2013
2034
 
2014
2035
  /**
2015
2036
  * Fixed queue node.
2016
- * @typeParam T - Type of fixed queue node data.
2037
+ * @template T - Type of fixed queue node data.
2017
2038
  * @internal
2018
2039
  */
2019
2040
  interface FixedQueueNode<T> {
@@ -2023,7 +2044,7 @@ interface FixedQueueNode<T> {
2023
2044
  }
2024
2045
  /**
2025
2046
  * Fixed queue.
2026
- * @typeParam T - Type of fixed queue data.
2047
+ * @template T - Type of fixed queue data.
2027
2048
  * @internal
2028
2049
  */
2029
2050
  interface IFixedQueue<T> {
@@ -2089,9 +2110,9 @@ declare const availableParallelism: () => number;
2089
2110
 
2090
2111
  /**
2091
2112
  * Base class that implements some shared logic for all poolifier workers.
2092
- * @typeParam MainWorker - Type of main worker.
2093
- * @typeParam Data - Type of data this worker receives from pool's execution. This can only be structured-cloneable data.
2094
- * @typeParam Response - Type of response the worker sends back to the main worker. This can only be structured-cloneable data.
2113
+ * @template MainWorker - Type of main worker.
2114
+ * @template Data - Type of data this worker receives from pool's execution. This can only be structured-cloneable data.
2115
+ * @template Response - Type of response the worker sends back to the main worker. This can only be structured-cloneable data.
2095
2116
  */
2096
2117
  declare abstract class AbstractWorker<MainWorker extends MessagePort | Worker, Data = unknown, Response = unknown> {
2097
2118
  protected readonly isMain: boolean | undefined;
@@ -2264,8 +2285,8 @@ declare abstract class AbstractWorker<MainWorker extends MessagePort | Worker, D
2264
2285
  *
2265
2286
  * If you use a `DynamicClusterPool` the extra workers that were created will be terminated,
2266
2287
  * but the minimum number of workers will be guaranteed.
2267
- * @typeParam Data - Type of data this worker receives from pool's execution. This can only be structured-cloneable data.
2268
- * @typeParam Response - Type of response the worker sends back to the main worker. This can only be structured-cloneable data.
2288
+ * @template Data - Type of data this worker receives from pool's execution. This can only be structured-cloneable data.
2289
+ * @template Response - Type of response the worker sends back to the main worker. This can only be structured-cloneable data.
2269
2290
  * @author [Christopher Quadflieg](https://github.com/Shinigami92)
2270
2291
  * @since 2.0.0
2271
2292
  */
@@ -2300,8 +2321,8 @@ declare class ClusterWorker<Data = unknown, Response = unknown> extends Abstract
2300
2321
  *
2301
2322
  * If you use a `DynamicThreadPool` the extra workers that were created will be terminated,
2302
2323
  * but the minimum number of workers will be guaranteed.
2303
- * @typeParam Data - Type of data this worker receives from pool's execution. This can only be structured-cloneable data.
2304
- * @typeParam Response - Type of response the worker sends back to the main thread. This can only be structured-cloneable data.
2324
+ * @template Data - Type of data this worker receives from pool's execution. This can only be structured-cloneable data.
2325
+ * @template Response - Type of response the worker sends back to the main thread. This can only be structured-cloneable data.
2305
2326
  * @author [Alessandro Pio Ardizio](https://github.com/pioardi)
2306
2327
  * @since 0.0.1
2307
2328
  */