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