A thread pool with a dynamic number of threads, but a guaranteed minimum number of threads.
This thread pool creates new threads when the others are busy, up to the maximum number of threads.
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.
Pool event emitter integrated with async resource.
+The async tracking tooling identifier is poolifier:<PoolType>-<WorkerType>-pool.
Events that can currently be listened to:
-
'full': Emitted when the pool is dynamic and full.
-
'busy': Emitted when the pool is busy.
+
'ready': Emitted when the number of workers created in the pool has reached the minimum size expected and are ready. If the pool is dynamic with a minimum number of workers is set to zero, this event is emitted when at least one dynamic worker is ready.
+
'busy': Emitted when the number of workers created in the pool has reached the maximum size expected and are executing concurrently their tasks quota.
+
'full': Emitted when the pool is dynamic and the number of workers created has reached the maximum size expected.
+
'empty': Emitted when the pool is dynamic with a minimum number of workers set to zero and the number of workers has reached the minimum size expected.
+
'destroy': Emitted when the pool is destroyed.
'error': Emitted when an uncaught error occurs.
'taskError': Emitted when an error occurs while executing a task.
+
'backPressure': Emitted when all worker nodes have back pressure (i.e. their tasks queue is full: queue size >= maximum queue size).
-
-
-
-
ProtectedReadonlyfilePath
-
filePath:string
-
Path to the worker file.
-
-
-
-
ProtectedReadonlymax
-
max:number
-
Maximum number of threads that can be created by this pool.
A thread pool with a dynamic number of threads, but a guaranteed minimum number of threads.
+- Preparing search index...
- The search index is not available
poolifier - v4.0.6Class DynamicThreadPool<Data, Response>
A thread pool with a dynamic number of threads, but a guaranteed minimum number of threads.
This thread pool creates new threads when the others are busy, up to the maximum number of threads. 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
.Author
Alessandro Pio Ardizio
- +Author
Alessandro Pio Ardizio
Since
0.0.1
-Type Parameters
---
-
-
--
-
-
Data = unknown
-Type of data sent to the worker. This can only be serializable data.
-Response = unknown
-Type of execution response. This can only be serializable data.
-Hierarchy
--- FixedThreadPool<Data, Response>
-
-- DynamicThreadPool
-
-Index
Constructors
-Properties
-Accessors
-Methods
-Constructors
-constructor
--- new
Dynamic Thread Pool<Data, Response>(min, max, filePath, opts?): DynamicThreadPool<Data, Response>
--
-
-
-
-
-
-
Constructs a new poolifier dynamic thread pool.
-Type Parameters
---
-
--
-
Data = unknown
Response = unknown
Parameters
---
-
-
--
-
-
--
-
-
--
-
-
min: number
-Minimum number of threads which are always active.
-max: number
-Maximum number of threads that can be created by this pool.
-filePath: string
-Path to an implementation of a
-ThreadWorker
file, which can be relative or absolute.opts: PoolOptions<ThreadWorkerWithMessageChannel> = {}
-Options for this dynamic thread pool.
-Returns DynamicThreadPool<Data, Response>
-Properties
-
-Optional
Readonly
emitterEmitter on which events can be listened to.
+Type Parameters
Type of data sent to the worker. This can only be structured-cloneable data.
+Type of execution response. This can only be structured-cloneable data.
+Hierarchy (view full)
Index
Constructors
Properties
Accessors
Methods
Constructors
constructor
Constructs a new poolifier dynamic thread pool.
+Type Parameters
Parameters
Minimum number of threads which are always active.
+Maximum number of threads that can be created by this pool.
+Path to an implementation of a
+ThreadWorker
file, which can be relative or absolute.Options for this dynamic thread pool.
+Returns DynamicThreadPool<Data, Response>
Properties
Optional
emitterPool event emitter integrated with async resource. +The async tracking tooling identifier is
poolifier:<PoolType>-<WorkerType>-pool
.Events that can currently be listened to:
-
-
+
+
+
+
+
+
-'full'
: Emitted when the pool is dynamic and full.'busy'
: Emitted when the pool is busy.'ready'
: Emitted when the number of workers created in the pool has reached the minimum size expected and are ready. If the pool is dynamic with a minimum number of workers is set to zero, this event is emitted when at least one dynamic worker is ready.'busy'
: Emitted when the number of workers created in the pool has reached the maximum size expected and are executing concurrently their tasks quota.'full'
: Emitted when the pool is dynamic and the number of workers created has reached the maximum size expected.'empty'
: Emitted when the pool is dynamic with a minimum number of workers set to zero and the number of workers has reached the minimum size expected.'destroy'
: Emitted when the pool is destroyed.'error'
: Emitted when an uncaught error occurs.'taskError'
: Emitted when an error occurs while executing a task.'backPressure'
: Emitted when all worker nodes have back pressure (i.e. their tasks queue is full: queue size >= maximum queue size). Path
-Protected
Readonly
filePath to the worker file.
-
-Protected
Readonly
maxMaximum number of threads that can be created by this pool.
- Of Workers
-Protected
Readonly
numberNumber of workers that this pool should manage.
-
-Protected
Readonly
optsOptions for the pool.
- Response Map
-Protected
promiseThe execution response promise map.
+Protected
Readonly
filePath to the worker file.
+Protected
Optional
Readonly
maximumMaximum number of workers that this pool manages.
+Protected
Readonly
minimumMinimum number of workers that this pool manages.
+Protected
Readonly
optsOptions for the pool.
+Protected
promiseThe task execution response promise map:
key
: The message id of each submitted task.value
: An object that contains the worker, the execution response promise resolve and reject callbacks.value
: An object that contains task's worker node key, execution response promise resolve and reject callbacks, async resource.When we receive a message from the worker, we get a map entry with the promise resolve/reject bound to the message id.
- Choice Strategy Context
-Protected
workerWorker choice strategy context referencing a worker choice algorithm implementation.
-Default to a round robin algorithm.
- Nodes
-Readonly
workerPool worker nodes.
-Accessors
-
-Protected
busy-- get busy(): boolean
--
-
Whether the pool is busy or not.
+Protected
Optional
workerWorker choice strategies context referencing worker choice algorithms implementation.
+Readonly
Internal
workerPool worker nodes.
+Accessors
Protected
busyWhether the pool is busy or not.
The pool busyness boolean status.
-Returns boolean
-
-Protected
full-- get full(): boolean
--
-
Whether the pool is full or not.
+Returns boolean
Protected
emptyThe pool emptiness boolean status.
+Returns boolean
Protected
fullWhether the pool is full or not.
The pool filling boolean status.
-Returns boolean
-info
--- get info(): PoolInfo
--
-
-
Pool information.
-Returns PoolInfo
- Size
-Protected
max-- get maxSize(): number
--
-
-
Pool maximum size.
-Returns number
- Size
-Protected
min-- get minSize(): number
--
-
-
Pool minimum size.
-Returns number
-
-Protected
type-- get type(): "fixed" | "dynamic"
--
-
Pool type.
+Returns boolean
info
Pool information.
+Returns PoolInfo
Private
readyThe pool readiness boolean status.
+Returns boolean
Protected
typeThe pool type.
If it is
-'dynamic'
, it provides themax
property.Returns "fixed" | "dynamic"
-
-Protected
worker-- get worker(): "cluster" | "thread"
--
-
-
Gets the worker type.
-Returns "cluster" | "thread"
-Methods
- Task Execution Hook
-Protected
after-- after
Task Execution Hook(worker, message): void
--
-
Hook executed after the worker task execution. +
Returns "fixed" | "dynamic"
Private
utilizationThe approximate pool utilization.
+Returns number
The pool utilization.
+Protected
workerThe worker type.
+Returns "thread" | "cluster"
Methods
add Task Function
Parameters
Returns Promise<boolean>
Inherit Doc
Protected
afterHook executed after the worker task execution. Can be overridden.
-Parameters
---
-
-
--
-
-
worker: ThreadWorkerWithMessageChannel
-The worker.
-message: MessageValue<Response, Worker | MessagePort>
-The received message.
-Returns void
- Worker Setup
-Protected
after-- after
Worker Setup(worker): void
--
-
-
-
-
Function that can be hooked up when a worker has been newly created and moved to the pool worker nodes.
-Can be used to update the
-maxListeners
or binding themain-worker
<->worker
connection if not bind by default.Parameters
---
-
-
worker: ThreadWorkerWithMessageChannel
-The newly created worker.
-Returns void
- Task Execution Hook
-Protected
before-- before
Task Execution Hook(workerNodeKey): void
--
-
- workerNodeKey: number
- message: MessageValue<Response, unknown>
Hook executed before the worker task execution. +
Parameters
The worker node key.
+The received message.
+Returns void
Protected
afterMethod hooked up after a worker node has been newly created. Can be overridden.
-Parameters
---
-
-
workerNodeKey: number
-The worker node key.
-Returns void
- Worker Node
-Protected
choose-- choose
Worker Node(): number
--
-
-
Chooses a worker node for the next task.
-The default worker choice strategy uses a round robin algorithm to distribute the load.
-Returns number
The worker node key
- - And Setup Worker
-Protected
create-- create
And Setup Worker(): ThreadWorkerWithMessageChannel
--
-
-
Creates a new worker and sets it up completely in the pool worker nodes.
-Returns ThreadWorkerWithMessageChannel
New, completely set up worker.
- - Worker
-Protected
create-- create
Worker(): ThreadWorkerWithMessageChannel
--
-
-
Returns a newly created worker.
-Returns ThreadWorkerWithMessageChannel
-destroy
--- destroy(): Promise<void>
--
-
-
Shutdowns every current worker in this pool.
-Returns Promise<void>
- Worker
-Protected
destroy-- destroy
Worker(worker): Promise<void>
--
-
-
-
-
Shutdowns the given worker.
-Parameters
---
-
-
worker: ThreadWorkerWithMessageChannel
-A worker within
-workerNodes
.Returns Promise<void>
-enable Tasks Queue
--- enable
Tasks Queue(enable, tasksQueueOptions?): void
--
-
-
-
-
Enables/disables the worker tasks queue in this pool.
-Parameters
---
-
-
--
-
-
enable: boolean
-Whether to enable or disable the worker tasks queue.
-
-Optional
tasksQueueOptions: TasksQueueOptionsThe worker tasks queue options.
-Returns void
-execute
--- execute(data?, name?): Promise<Response>
--
-
-
-
-
Executes the specified function in the worker constructor with the task data input parameter.
-Parameters
---
-
-
--
-
-
-Optional
data: DataThe task input data for the specified worker function. This can only be serializable data.
-
-Optional
name: stringThe name of the worker function to execute. If not specified, the default worker function will be executed.
-Returns Promise<Response>
Promise that will be fulfilled when the task is completed.
- - Busy
-Protected
internal-- internal
Busy(): boolean
--
-
Returns boolean
Main
-Protected
is-- is
Main(): boolean
--
-
-
Should return whether the worker is the main worker or not.
-Returns boolean
- Worker Message Listener
-Protected
register-- register
Worker Message Listener<Message>(worker, listener): void
--
-
-
-
-
-
-
Registers a listener callback on the given worker.
-Type Parameters
---
-
Message
Parameters
---
-
-
--
-
-
-
worker: ThreadWorkerWithMessageChannel
-The worker which should register a listener.
-listener: ((message) => void)
-The message listener callback.
---
-
-- (message): void
--
-
-
-
Parameters
---
-
message: MessageValue<Message, Worker | MessagePort>
Returns void
Returns void
- To Worker
-Protected
send-- send
To Worker(worker, message): void
--
-
-
-
-
Sends a message to the given worker.
-Parameters
---
-
-
--
-
-
worker: ThreadWorkerWithMessageChannel
-The worker which should receive the message.
-message: MessageValue<Data, Worker | MessagePort>
-The message.
-Returns void
-set Tasks Queue Options
--- set
Tasks Queue Options(tasksQueueOptions): void
--
-
-
-
-
Sets the worker tasks queue options in this pool.
-Parameters
---
-
-
tasksQueueOptions: TasksQueueOptions
-The worker tasks queue options.
-Returns void
-set Worker Choice Strategy
--- set
Worker Choice Strategy(workerChoiceStrategy, workerChoiceStrategyOptions?): void
--
-
-
-
-
Sets the worker choice strategy in this pool.
-Parameters
---
-
-
--
-
-
workerChoiceStrategy: "ROUND_ROBIN" | "LEAST_USED" | "LEAST_BUSY" | "FAIR_SHARE" | "WEIGHTED_ROUND_ROBIN" | "INTERLEAVED_WEIGHTED_ROUND_ROBIN"
-The worker choice strategy.
-
-Optional
workerChoiceStrategyOptions: WorkerChoiceStrategyOptionsThe worker choice strategy options.
-Returns void
-set Worker Choice Strategy Options
--- set
Worker Choice Strategy Options(workerChoiceStrategyOptions): void
--
-
-
-
-
Sets the worker choice strategy options in this pool.
-Parameters
---
-
-
workerChoiceStrategyOptions: WorkerChoiceStrategyOptions
-The worker choice strategy options.
-Returns void
- Hook
-Protected
setup-- setup
Hook(): void
--
-
-
Setup hook to execute code before worker node are created in the abstract constructor. -Can be overridden
-Returns void
- Listener
-Protected
worker-- worker
Listener(): ((message) => void)
--
-
-
This function is the listener registered for each worker message.
-Returns ((message) => void)
The listener function to execute when a message is received from a worker.
- ---
-
--- (message): void
--
-
-
-
-
This function is the listener registered for each worker message.
-Parameters
---
-
message: MessageValue<Response, Worker | MessagePort>
Returns void
The listener function to execute when a message is received from a worker.
- --
-Settings
Member Visibility
Theme
-
-On This Page
-- constructor
-- emitter
-- file
Path
-- max
-- number
Of Workers
-- opts
-- promise
Response Map
-- worker
Choice Strategy Context
-- worker
Nodes
-- busy
-- full
-- info
-- max
Size
-- min
Size
-- type
-- worker
-- after
Task Execution Hook
-- after
Worker Setup
-- before
Task Execution Hook
-- choose
Worker Node
-- create
And Setup Worker
-- create
Worker
-- destroy
-- destroy
Worker
-- enable
Tasks Queue
-- execute
-- internal
Busy
-- is
Main
-- register
Worker Message Listener
-- send
To Worker
-- set
Tasks Queue Options
-- set
Worker Choice Strategy
-- set
Worker Choice Strategy Options
-- setup
Hook
-- worker
Listener
Generated using TypeDoc
Parameters
The newly created worker node key.
+Returns void