1 import type { JSONValue
} from
'../../utility-types'
2 import type { PoolOptions
} from
'../abstract-pool'
3 import type { ThreadWorkerWithMessageChannel
} from
'./fixed'
4 import { FixedThreadPool
} from
'./fixed'
7 * A thread pool with a dynamic number of threads, but a guaranteed minimum number of threads.
9 * This thread pool creates new threads when the others are busy, up to the maximum number of threads.
10 * When the maximum number of threads is reached, an event is emitted. If you want to listen to this event, use the pool's `emitter`.
12 * @template Data Type of data sent to the worker.
13 * @template Response Type of response of execution.
15 * @author [Alessandro Pio Ardizio](https://github.com/pioardi)
18 export class DynamicThreadPool
<
19 Data
extends JSONValue
= JSONValue
,
20 Response
extends JSONValue
= JSONValue
21 > extends FixedThreadPool
<Data
, Response
> {
23 * Constructs a new poolifier dynamic thread pool.
25 * @param min Minimum number of threads which are always active.
26 * @param max Maximum number of threads that can be created by this pool.
27 * @param filename Path to an implementation of a `ThreadWorker` file, which can be relative or absolute.
28 * @param opts Options for this fixed thread pool. Default: `{ maxTasks: 1000 }`
32 public readonly max
: number,
34 opts
: PoolOptions
<ThreadWorkerWithMessageChannel
> = { maxTasks
: 1000 }
36 super(min
, filename
, opts
)
40 * Choose a thread for the next task.
42 * It will first check for and return an idle thread.
43 * If all threads are busy, then it will try to create a new one up to the `max` thread count.
44 * If the max thread count is reached, the emitter will emit a `FullPool` event and it will fall back to using a round robin algorithm to distribute the load.
46 * @returns Thread worker.
48 protected chooseWorker (): ThreadWorkerWithMessageChannel
{
49 for (const [worker
, numberOfTasks
] of this.tasks
) {
50 if (numberOfTasks
=== 0) {
51 // A worker is free, use it
56 if (this.workers
.length
=== this.max
) {
57 this.emitter
.emit('FullPool')
58 return super.chooseWorker()
61 // All workers are busy, create a new worker
62 const worker
= this.createAndSetupWorker()
63 this.registerWorkerMessageListener
<Data
>(worker
, message
=> {
64 const tasksInProgress
= this.tasks
.get(worker
)
65 if (message
.kill
&& !tasksInProgress
) {
66 // Kill received from the worker, means that no new tasks are submitted to that worker for a while( > maxInactiveTime)
67 // To handle the case of a long-running task we will check if the there is any active task
68 console
.log('Here we are')
69 this.sendToWorker(worker
, { kill
: 1 })
70 void this.destroyWorker(worker
)