Commit | Line | Data |
---|---|---|
bdaf31cd | 1 | import type { PoolOptions } from '../pool' |
c4855468 | 2 | import { PoolType } from '../pool' |
c97c7edb | 3 | import type { ThreadWorkerWithMessageChannel } from './fixed' |
325f50bc | 4 | import { FixedThreadPool } from './fixed' |
f045358d | 5 | |
4ade5f1f | 6 | /** |
729c563d | 7 | * A thread pool with a dynamic number of threads, but a guaranteed minimum number of threads. |
4ade5f1f | 8 | * |
729c563d | 9 | * This thread pool creates new threads when the others are busy, up to the maximum number of threads. |
9cd39dd4 | 10 | * When the maximum number of threads is reached and workers are busy, an event is emitted. If you want to listen to this event, use the pool's `emitter`. |
729c563d | 11 | * |
38e795c1 | 12 | * @typeParam Data - Type of data sent to the worker. This can only be serializable data. |
02706357 | 13 | * @typeParam Response - Type of execution response. This can only be serializable data. |
4ade5f1f S |
14 | * @author [Alessandro Pio Ardizio](https://github.com/pioardi) |
15 | * @since 0.0.1 | |
16 | */ | |
60fbd6d6 | 17 | export class DynamicThreadPool< |
deb85c12 JB |
18 | Data = unknown, |
19 | Response = unknown | |
4ade5f1f | 20 | > extends FixedThreadPool<Data, Response> { |
4ade5f1f | 21 | /** |
729c563d S |
22 | * Constructs a new poolifier dynamic thread pool. |
23 | * | |
38e795c1 JB |
24 | * @param min - Minimum number of threads which are always active. |
25 | * @param max - Maximum number of threads that can be created by this pool. | |
26 | * @param filePath - Path to an implementation of a `ThreadWorker` file, which can be relative or absolute. | |
27 | * @param opts - Options for this dynamic thread pool. | |
4ade5f1f S |
28 | */ |
29 | public constructor ( | |
c97c7edb | 30 | min: number, |
d2097c13 | 31 | public readonly max: number, |
31b90205 | 32 | filePath: string, |
1927ee67 | 33 | opts: PoolOptions<ThreadWorkerWithMessageChannel> = {} |
4ade5f1f | 34 | ) { |
31b90205 | 35 | super(min, filePath, opts) |
4ade5f1f S |
36 | } |
37 | ||
afc003b2 | 38 | /** @inheritDoc */ |
7c0ba920 JB |
39 | public get type (): PoolType { |
40 | return PoolType.DYNAMIC | |
41 | } | |
42 | ||
afc003b2 | 43 | /** @inheritDoc */ |
c319c66b | 44 | protected get full (): boolean { |
f06e48d8 | 45 | return this.workerNodes.length === this.max |
4ade5f1f | 46 | } |
c2ade475 | 47 | |
08f3f44c JB |
48 | /** @inheritDoc */ |
49 | public get size (): number { | |
50 | return this.max | |
51 | } | |
52 | ||
afc003b2 | 53 | /** @inheritDoc */ |
c319c66b | 54 | protected get busy (): boolean { |
0527b6db | 55 | return this.full && this.internalBusy() |
c2ade475 | 56 | } |
4ade5f1f | 57 | } |