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.

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

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

    Returns FixedThreadPool<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<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

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

    Parameters

    • workerChoiceStrategy: "ROUND_ROBIN" | "LESS_RECENTLY_USED"

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