Options
All
  • Public
  • Public/Protected
  • All
Menu

Class DynamicClusterPool<Data, Response>

A cluster pool with a dynamic number of workers, but a guaranteed minimum number of workers.

This cluster pool creates new workers when the others are busy, up to the maximum number of workers. When the maximum number of workers 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

Christopher Quadflieg

since

2.0.0

Type parameters

  • Data = unknown

  • Response = unknown

Hierarchy

Index

Constructors

constructor

  • Constructs a new poolifier dynamic cluster pool.

    Type parameters

    • Data = unknown

    • Response = unknown

    Parameters

    • min: number

      Minimum number of workers which are always active.

    • max: number

      Maximum number of workers that can be created by this pool.

    • filePath: string

      Path to an implementation of a ClusterWorker file, which can be relative or absolute.

    • opts: ClusterPoolOptions = {}

    Returns DynamicClusterPool<Data, Response>

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<Worker, 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<Worker, number> = ...

Protected workerChoiceStrategyContext

workerChoiceStrategyContext: WorkerChoiceStrategyContext<Worker, Data, Response>

Worker choice strategy instance implementing the worker choice algorithm.

Default to a strategy implementing a round robin algorithm.

Readonly workers

workers: Worker[] = []

Accessors

busy

  • get busy(): boolean

numberOfRunningTasks

  • get numberOfRunningTasks(): number

type

  • get type(): PoolType

Methods

Protected afterWorkerSetup

  • afterWorkerSetup(worker: Worker): void

Protected chooseWorker

  • chooseWorker(): Worker

Protected createAndSetupWorker

  • createAndSetupWorker(): Worker

Protected createWorker

  • createWorker(): Worker

Protected decreaseWorkersTasks

  • decreaseWorkersTasks(worker: Worker): void

destroy

  • destroy(): Promise<void>

destroyWorker

  • destroyWorker(worker: Worker): void

execute

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

findFreeTasksMapEntry

  • findFreeTasksMapEntry(): false | [Worker, number]

Protected increaseWorkersTask

  • increaseWorkersTask(worker: Worker): void

Protected internalExecute

  • internalExecute(worker: Worker, messageId: number): Promise<Response>

Protected internalGetBusyStatus

  • internalGetBusyStatus(): boolean

Protected isMain

  • isMain(): boolean

registerWorkerMessageListener

  • registerWorkerMessageListener<Message>(worker: Worker, listener: (message: MessageValue<Message, unknown>) => void): void

Protected removeWorker

  • removeWorker(worker: Worker): void

Protected sendToWorker

  • sendToWorker(worker: Worker, message: MessageValue<Data, unknown>): void

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