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