Commit | Line | Data |
---|---|---|
c97c7edb | 1 | import type { PoolOptions } from '../abstract-pool' |
7c0ba920 | 2 | import { PoolType } from '../pool-internal' |
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 S |
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`. | |
11 | * | |
deb85c12 JB |
12 | * @template Data Type of data sent to the worker. This can only be serializable data. |
13 | * @template Response Type of response of execution. This can only be serializable data. | |
4ade5f1f S |
14 | * |
15 | * @author [Alessandro Pio Ardizio](https://github.com/pioardi) | |
16 | * @since 0.0.1 | |
17 | */ | |
60fbd6d6 | 18 | export class DynamicThreadPool< |
deb85c12 JB |
19 | Data = unknown, |
20 | Response = unknown | |
4ade5f1f | 21 | > extends FixedThreadPool<Data, Response> { |
4ade5f1f | 22 | /** |
729c563d S |
23 | * Constructs a new poolifier dynamic thread pool. |
24 | * | |
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. | |
31b90205 | 27 | * @param filePath Path to an implementation of a `ThreadWorker` file, which can be relative or absolute. |
1927ee67 | 28 | * @param opts Options for this dynamic thread pool. Default: `{}` |
4ade5f1f S |
29 | */ |
30 | public constructor ( | |
c97c7edb | 31 | min: number, |
4ade5f1f | 32 | public readonly max: number, |
31b90205 | 33 | filePath: string, |
1927ee67 | 34 | opts: PoolOptions<ThreadWorkerWithMessageChannel> = {} |
4ade5f1f | 35 | ) { |
31b90205 | 36 | super(min, filePath, opts) |
4ade5f1f S |
37 | } |
38 | ||
a35560ba | 39 | /** @inheritdoc */ |
7c0ba920 JB |
40 | public get type (): PoolType { |
41 | return PoolType.DYNAMIC | |
42 | } | |
43 | ||
44 | /** @inheritdoc */ | |
45 | public get busy (): boolean { | |
46 | return this.workers.length === this.max | |
4ade5f1f S |
47 | } |
48 | } |