Options
All
  • Public
  • Public/Protected
  • All
Menu

Class FixedThreadPool<Data, Response>

A thread pool with a fixed number of threads.

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

This pool selects the threads in a round robin fashion.

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 response of execution. This can only be serializable data.

Hierarchy

Index

Constructors

constructor

  • Constructs a new poolifier fixed thread pool.

    Type parameters

    • Data = unknown

    • Response = unknown

    Parameters

    • numberOfThreads: number

      Number of threads for this pool.

    • filePath: string

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

    • opts: PoolOptions<ThreadWorkerWithMessageChannel> = ...

      Options for this fixed thread pool. Default: {}

    Returns FixedThreadPool<Data, Response>

Properties

Readonly emitter

emitter: PoolEmitter
inheritdoc

Readonly filePath

filePath: string

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

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

inheritdoc

Accessors

dynamic

  • get dynamic(): boolean

Methods

Protected afterWorkerSetup

Protected chooseWorker

Protected createAndSetupWorker

Protected createWorker

Protected decreaseWorkersTasks

destroy

  • destroy(): Promise<void>

destroyWorker

execute

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

Protected increaseWorkersTask

Protected internalExecute

Protected isMain

  • isMain(): boolean

registerWorkerMessageListener

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

    Type parameters

    • Message

    Parameters

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

          • message: MessageValue<Message, unknown>

          Returns void

    Returns void

Protected removeWorker

Protected sendToWorker

setWorkerChoiceStrategy

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

Protected setupHook

  • setupHook(): void
  • Setup hook that can be overridden by a Poolifier pool implementation to run code before workers are created in the abstract constructor.

    Returns 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