Options
All
  • Public
  • Public/Protected
  • All
Menu

Class FixedClusterPool<Data, Response>

A cluster pool with a fixed number of workers.

It is possible to perform tasks in sync or asynchronous mode as you prefer.

This pool selects the workers in a round robin fashion.

author

Christopher Quadflieg

since

2.0.0

Type parameters

  • Data = unknown

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

  • Response = unknown

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

Hierarchy

Index

Constructors

constructor

  • Constructs a new poolifier fixed cluster pool.

    Type parameters

    • Data = unknown

    • Response = unknown

    Parameters

    • numberOfWorkers: number

      Number of workers for this pool.

    • filePath: string

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

    • opts: ClusterPoolOptions = ...

      Options for this fixed cluster pool. Default: {}

    Returns FixedClusterPool<Data, Response>

Properties

Optional Readonly emitter

emitter: undefined | PoolEmitter
inheritdoc

Readonly filePath

filePath: string

Optional Readonly max

max: undefined | number
inheritdoc

Protected nextMessageId

nextMessageId: number = 0

ID of the next message.

Readonly numberOfWorkers

numberOfWorkers: number

Readonly opts

opts: ClusterPoolOptions = ...

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> = ...
inheritdoc

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[] = ...
inheritdoc

Accessors

busy

  • get busy(): boolean

numberOfRunningTasks

  • get numberOfRunningTasks(): number

type

  • get type(): PoolType

Methods

Protected afterWorkerSetup

  • afterWorkerSetup(worker: Worker): void

Protected chooseWorker

  • chooseWorker(): Worker
  • Choose a worker for the next task.

    The default implementation uses a round robin algorithm to distribute the load.

    Returns Worker

    Worker.

Protected createAndSetupWorker

  • createAndSetupWorker(): Worker
  • Creates a new worker for this pool and sets it up completely.

    Returns Worker

    New, completely set up worker.

Protected createWorker

  • createWorker(): Worker

Protected decreaseWorkersTasks

  • decreaseWorkersTasks(worker: Worker): void
  • Decrease the number of tasks that the given workers has applied.

    Parameters

    • worker: Worker

      Worker whose tasks are decreased.

    Returns 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
  • Increase the number of tasks that the given workers has applied.

    Parameters

    • worker: Worker

      Worker whose tasks are increased.

    Returns 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
  • inheritdoc

    Type parameters

    • Message

    Parameters

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

          • message: MessageValue<Message, unknown>

          Returns void

    Returns void

Protected removeWorker

  • removeWorker(worker: Worker): void
  • Removes the given worker from the pool.

    Parameters

    • worker: Worker

      Worker that will be removed.

    Returns 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
      • Parameters

        • message: MessageValue<Response, unknown>

        Returns void

Generated using TypeDoc