Fix WRR worker choice strategy implementation
[poolifier.git] / src / pools / pool.ts
CommitLineData
bdaf31cd
JB
1import type {
2 ErrorHandler,
3 ExitHandler,
4 MessageHandler,
5 OnlineHandler
6} from './pool-worker'
7import type { WorkerChoiceStrategy } from './selection-strategies/selection-strategies-types'
8
9/**
10 * Options for a poolifier pool.
11 */
12export interface PoolOptions<Worker> {
13 /**
14 * A function that will listen for message event on each worker.
15 */
16 messageHandler?: MessageHandler<Worker>
17 /**
18 * A function that will listen for error event on each worker.
19 */
20 errorHandler?: ErrorHandler<Worker>
21 /**
22 * A function that will listen for online event on each worker.
23 */
24 onlineHandler?: OnlineHandler<Worker>
25 /**
26 * A function that will listen for exit event on each worker.
27 */
28 exitHandler?: ExitHandler<Worker>
29 /**
30 * The work choice strategy to use in this pool.
31 */
32 workerChoiceStrategy?: WorkerChoiceStrategy
33 /**
34 * Pool events emission.
35 *
36 * @default true
37 */
38 enableEvents?: boolean
39}
a35560ba 40
729c563d
S
41/**
42 * Contract definition for a poolifier pool.
43 *
deb85c12
JB
44 * @template Data Type of data sent to the worker. This can only be serializable data.
45 * @template Response Type of response of execution. This can only be serializable data.
729c563d 46 */
d3c8a1a8 47export interface IPool<Data = unknown, Response = unknown> {
729c563d
S
48 /**
49 * Perform the task specified in the constructor with the data parameter.
50 *
deb85c12 51 * @param data The input for the specified task. This can only be serializable data.
729c563d
S
52 * @returns Promise that will be resolved when the task is successfully completed.
53 */
c97c7edb 54 execute(data: Data): Promise<Response>
280c2a77 55 /**
675bb809 56 * Shutdowns every current worker in this pool.
280c2a77
S
57 */
58 destroy(): Promise<void>
a35560ba
S
59 /**
60 * Set the worker choice strategy in this pool.
61 *
62 * @param workerChoiceStrategy The worker choice strategy.
63 */
64 setWorkerChoiceStrategy(workerChoiceStrategy: WorkerChoiceStrategy): void
c97c7edb 65}