Use prettierx and eslint native (#85)
[poolifier.git] / src / dynamic.ts
1 import {
2 FixedThreadPool,
3 FixedThreadPoolOptions,
4 WorkerWithMessageChannel
5 } from './fixed'
6
7 import { EventEmitter } from 'events'
8
9 class MyEmitter extends EventEmitter {}
10
11 export type DynamicThreadPoolOptions = FixedThreadPoolOptions
12
13 /**
14 * A thread pool with a min/max number of threads, is possible to execute tasks in sync or async mode as you prefer.
15 *
16 * This thread pool will create new workers when the other ones are busy, until the max number of threads,
17 * when the max number of threads is reached, an event will be emitted, if you want to listen this event use the emitter method.
18 *
19 * @author [Alessandro Pio Ardizio](https://github.com/pioardi)
20 * @since 0.0.1
21 */
22 /* eslint-disable @typescript-eslint/no-explicit-any */
23 export class DynamicThreadPool<
24 Data = any,
25 Response = any
26 > extends FixedThreadPool<Data, Response> {
27 /* eslint-enable @typescript-eslint/no-explicit-any */
28 public readonly emitter: MyEmitter
29
30 /**
31 * @param min Min number of threads that will be always active
32 * @param max Max number of threads that will be active
33 * @param filename A file path with implementation of `ThreadWorker` class, relative path is fine.
34 * @param opts An object with possible options for example `errorHandler`, `onlineHandler`. Default: `{ maxTasks: 1000 }`
35 */
36 public constructor (
37 public readonly min: number,
38 public readonly max: number,
39 public readonly filename: string,
40 public readonly opts: DynamicThreadPoolOptions = { maxTasks: 1000 }
41 ) {
42 super(min, filename, opts)
43
44 this.emitter = new MyEmitter()
45 }
46
47 protected chooseWorker (): WorkerWithMessageChannel {
48 let worker: WorkerWithMessageChannel | undefined
49 for (const entry of this.tasks) {
50 if (entry[1] === 0) {
51 worker = entry[0]
52 break
53 }
54 }
55
56 if (worker) {
57 // a worker is free, use it
58 return worker
59 } else {
60 if (this.workers.length === this.max) {
61 this.emitter.emit('FullPool')
62 return super.chooseWorker()
63 }
64 // all workers are busy create a new worker
65 const worker = this.newWorker()
66 worker.port2?.on('message', (message: { kill?: number }) => {
67 if (message.kill) {
68 worker.postMessage({ kill: 1 })
69 void worker.terminate()
70 // clean workers from data structures
71 const workerIndex = this.workers.indexOf(worker)
72 this.workers.splice(workerIndex, 1)
73 this.tasks.delete(worker)
74 }
75 })
76 return worker
77 }
78 }
79 }