1 declare module 'poolifier' {
2 import { AsyncResource
} from
'async_hooks'
3 import { EventEmitter
} from
'events'
4 import { Worker
} from
'worker_threads'
6 export interface FixedThreadPoolOptions
{
8 * A function that will listen for error event on each worker thread.
10 errorHandler
?: (this: Worker
, e
: Error) => void
12 * A function that will listen for online event on each worker thread.
14 onlineHandler
?: (this: Worker
) => void
16 * A function that will listen for exit event on each worker thread.
18 exitHandler
?: (this: Worker
, code
: number) => void
20 * This is just to avoid not useful warnings message, is used to set `maxListeners` on event emitters (workers are event emitters).
28 * A thread pool with a static number of threads, is possible to execute tasks in sync or async mode as you prefer.
30 * This pool will select the worker thread in a round robin fashion.
32 * @author [Alessandro Pio Ardizio](https://github.com/pioardi)
35 export class FixedThreadPool
<Data
= any, Response
= any> {
37 * Num of threads for this worker pool.
42 opts
: FixedThreadPoolOptions
44 tasks
: Map
<Worker
, number>
49 * @param numThreads Num of threads for this worker pool.
50 * @param filePath A file path with implementation of `ThreadWorker` class, relative path is fine.
51 * @param opts An object with possible options for example `errorHandler`, `onlineHandler`. Default: `{ maxTasks: 1000 }`
56 opts
?: FixedThreadPoolOptions
59 destroy (): Promise
<void>
62 * Execute the task specified into the constructor with the data parameter.
64 * @param data The input for the task specified.
65 * @returns Promise that is resolved when the task is done.
67 execute (data
: Data
): Promise
<Response
>
69 _execute (worker
: Worker
, id
: number): Promise
<unknown
>
70 _chooseWorker (): Worker
74 export type DynamicThreadPoolOptions
= FixedThreadPoolOptions
76 class MyEmitter
extends EventEmitter
{}
79 * A thread pool with a min/max number of threads, is possible to execute tasks in sync or async mode as you prefer.
81 * This thread pool will create new workers when the other ones are busy, until the max number of threads,
82 * when the max number of threads is reached, an event will be emitted, if you want to listen this event use the emitter method.
84 * @author [Alessandro Pio Ardizio](https://github.com/pioardi)
87 export class DynamicThreadPool
<
90 > extends FixedThreadPool
<Data
, Response
> {
95 * @param min Min number of threads that will be always active
96 * @param max Max number of threads that will be active
97 * @param filename A file path with implementation of `ThreadWorker` class, relative path is fine.
98 * @param opts An object with possible options for example `errorHandler`, `onlineHandler`. Default: `{ maxTasks: 1000 }`
104 opts
?: DynamicThreadPoolOptions
108 export interface ThreadWorkerOptions
{
110 * Max time to wait tasks to work on (in ms), after this period the new worker threads will die.
114 maxInactiveTime
?: number
116 * `true` if your function contains async pieces, else `false`.
124 * An example worker that will be always alive, you just need to **extend** this class if you want a static pool.
126 * When this worker is inactive for more than 1 minute, it will send this info to the main thread,
127 * if you are using DynamicThreadPool, the workers created after will be killed, the min num of thread will be guaranteed.
129 * @author [Alessandro Pio Ardizio](https://github.com/pioardi)
132 export class ThreadWorker
<Data
= any, Response
= any> extends AsyncResource
{
133 opts
: ThreadWorkerOptions
134 maxInactiveTime
: number
140 constructor (fn
: (data
: Data
) => Response
, opts
?: ThreadWorkerOptions
)
142 _checkAlive (): boolean
143 _run (fn
: (data
: Data
) => Response
, value
: { data
: Data
}): void
145 fn
: (data
: Data
) => Promise
<Response
>,
146 value
: { data
: Data
}