Options
All
  • Public
  • Public/Protected
  • All
Menu

Class 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, an event is emitted. If you want to listen to this event, use the pool's emitter.

template DataType

of data sent to the worker. This can only be serializable data.

template ResponseType

of response of execution. This can only be serializable data.

author

Alessandro Pio Ardizio

since

0.0.1

Type parameters

  • Data = unknown

  • Response = unknown

Hierarchy

Index

Constructors

constructor

Properties

Optional Readonly emitter

emitter?: PoolEmitter

Readonly filePath

filePath: string

Readonly max

max: number

Protected nextMessageId

nextMessageId: number = 0

Id of the next message.

Readonly numberOfWorkers

numberOfWorkers: number

Readonly opts

Protected promiseMap

promiseMap: Map<number, PromiseWorkerResponseWrapper<ThreadWorkerWithMessageChannel, Response>> = ...

The promise map.

  • key: This is the message Id of each submitted task.
  • value: An object that contains the worker, the resolve function and the reject function.

When we receive a message from the worker we get a map entry and resolve/reject the promise based on the message.

Readonly tasks

tasks: Map<ThreadWorkerWithMessageChannel, number> = ...

Protected workerChoiceStrategyContext

workerChoiceStrategyContext: WorkerChoiceStrategyContext<ThreadWorkerWithMessageChannel, Data, Response>

Worker choice strategy instance implementing the worker choice algorithm.

Default to a strategy implementing a round robin algorithm.

Readonly workers

Accessors

busy

  • get busy(): boolean

numberOfRunningTasks

  • get numberOfRunningTasks(): number

type

  • get type(): PoolType

Methods

Protected afterWorkerSetup

Protected chooseWorker

Protected createAndSetupWorker

Protected createWorker

Protected decreaseWorkersTasks

destroy

  • destroy(): Promise<void>

destroyWorker

execute

  • execute(data: Data): Promise<Response>

findFreeTasksMapEntry

Protected increaseWorkersTask

Protected internalExecute

Protected internalGetBusyStatus

  • internalGetBusyStatus(): boolean

Protected isMain

  • isMain(): boolean

registerWorkerMessageListener

  • registerWorkerMessageListener<Message>(messageChannel: ThreadWorkerWithMessageChannel, listener: (message: MessageValue<Message, unknown>) => void): void

Protected removeWorker

Protected sendToWorker

setWorkerChoiceStrategy

  • setWorkerChoiceStrategy(workerChoiceStrategy: "ROUND_ROBIN" | "LESS_RECENTLY_USED"): void

Protected setupHook

  • setupHook(): void

Protected workerListener

  • workerListener(): (message: MessageValue<Response, unknown>) => void
  • This function is the listener registered for each worker.

    Returns (message: MessageValue<Response, unknown>) => void

    The listener function to execute when a message is sent from a worker.

      • (message: MessageValue<Response, unknown>): void
      • This function is the listener registered for each worker.

        Parameters

        • message: MessageValue<Response, unknown>

        Returns void

        The listener function to execute when a message is sent from a worker.

Generated using TypeDoc