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.

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 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 = {}

    Returns FixedClusterPool<Data, Response>

Properties

Optional Readonly emitter

emitter?: PoolEmitter
inheritdoc

Readonly filePath

filePath: string

Optional Readonly max

max?: number
inheritdoc

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> = ...
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 worker 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 worker has applied.

    Parameters

    • worker: Worker

      Worker whose tasks are increased.

    Returns void

Protected internalExecute

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

    • worker: Worker
    • messageId: number

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

    Parameters

    • worker: Worker
    • message: MessageValue<Data, unknown>

    Returns void

setWorkerChoiceStrategy

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

    Parameters

    • workerChoiceStrategy: "ROUND_ROBIN" | "LESS_RECENTLY_USED"

    Returns 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