workerStartDelay | | 500 | integer | milliseconds to wait at charging station worker threads startup
workerPoolMinSize | | 4 | integer | worker threads pool minimum number of threads
workerPoolMaxSize | | 16 | integer | worker threads pool maximum number of threads
+workerPoolStrategy | ROUND_ROBIN/LESS_RECENTLY_USED/... | [poolifier](https://github.com/pioardi/poolifier) default: ROUND_ROBBIN | string | worker threads pool [poolifier](https://github.com/pioardi/poolifier) worker choice strategy
chargingStationsPerWorker | | 1 | integer | number of charging stations per worker threads for the `workerSet` process type
logConsole | true/false | false | boolean | output logs on the console
logFormat | | simple | string | winston log format
startDelay: Configuration.getWorkerStartDelay(),
poolMaxSize: Configuration.getWorkerPoolMaxSize(),
poolMinSize: Configuration.getWorkerPoolMinSize(),
- elementsPerWorker: Configuration.getChargingStationsPerWorker()
+ elementsPerWorker: Configuration.getChargingStationsPerWorker(),
+ poolOptions: {
+ workerChoiceStrategy: Configuration.getWorkerPoolStrategy()
+ }
});
}
return this.workerImplementationInstance;
+import type { WorkerChoiceStrategy } from 'poolifier';
import { WorkerProcessType } from './Worker';
export interface StationTemplateURL {
workerStartDelay?: number;
workerPoolMinSize?: number;
workerPoolMaxSize?: number;
+ workerPoolStrategy?: WorkerChoiceStrategy;
chargingStationsPerWorker?: number;
logFormat?: string;
logLevel?: string;
+import { PoolOptions } from 'poolifier';
import { Worker } from 'worker_threads';
export enum WorkerProcessType {
poolMaxSize?: number;
poolMinSize?: number;
elementsPerWorker?: number;
+ poolOptions?: PoolOptions<Worker>;
}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
import ConfigurationData, { StationTemplateURL } from '../types/ConfigurationData';
import Constants from './Constants';
+import type { WorkerChoiceStrategy } from 'poolifier';
import { WorkerProcessType } from '../types/Worker';
import fs from 'fs';
import path from 'path';
return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerPoolMaxSize') ? Configuration.getConfig().workerPoolMaxSize : 16;
}
+ static getWorkerPoolStrategy(): WorkerChoiceStrategy {
+ return Configuration.getConfig().workerPoolStrategy;
+ }
+
static getChargingStationsPerWorker(): number {
return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'chargingStationsPerWorker') ? Configuration.getConfig().chargingStationsPerWorker : 1;
}
* @param {number} min
* @param {number} max
* @param {number} workerStartDelay
+ * @param {PoolOptions} opts
*/
- constructor(workerScript: string, min: number, max: number, workerStartDelay?: number) {
+ constructor(workerScript: string, min: number, max: number, workerStartDelay?: number, opts?: PoolOptions<Worker>) {
super(workerScript, workerStartDelay);
- this.pool = DynamicPool.getInstance(min, max, this.workerScript);
+ this.pool = DynamicPool.getInstance(min, max, this.workerScript, opts);
}
get size(): number {
super(min, max, workerScript, opts);
}
- public static getInstance(min: number, max: number, workerScript: string): DynamicPool {
+ public static getInstance(min: number, max: number, workerScript: string, opts?: PoolOptions<Worker>): DynamicPool {
if (!DynamicPool.instance) {
- DynamicPool.instance = new DynamicPool(min, max, workerScript,
- {
- exitHandler: (code) => {
- if (code !== 0) {
- console.error(`Worker stopped with exit code ${code}`);
- }
- }
+ opts.exitHandler = opts.exitHandler ?? ((code) => {
+ if (code !== 0) {
+ console.error(`Worker stopped with exit code ${code}`);
}
- );
+ });
+ DynamicPool.instance = new DynamicPool(min, max, workerScript, opts);
}
return DynamicPool.instance;
}
return new WorkerSet<T>(workerScript, options.elementsPerWorker, options.startDelay);
case WorkerProcessType.STATIC_POOL:
options.poolMaxSize = options.poolMaxSize ?? 16;
- return new WorkerStaticPool<T>(workerScript, options.poolMaxSize, options.startDelay);
+ return new WorkerStaticPool<T>(workerScript, options.poolMaxSize, options.startDelay, options.poolOptions);
case WorkerProcessType.DYNAMIC_POOL:
options.poolMinSize = options.poolMinSize ?? 4;
options.poolMaxSize = options.poolMaxSize ?? 16;
- return new WorkerDynamicPool<T>(workerScript, options.poolMinSize, options.poolMaxSize, options.startDelay);
+ return new WorkerDynamicPool<T>(workerScript, options.poolMinSize, options.poolMaxSize, options.startDelay, options.poolOptions);
default:
return null;
}
* @param {string} workerScript
* @param {number} numberOfThreads
* @param {number} startWorkerDelay
+ * @param {PoolOptions} opts
*/
- constructor(workerScript: string, numberOfThreads: number, startWorkerDelay?: number) {
+ constructor(workerScript: string, numberOfThreads: number, startWorkerDelay?: number, opts?: PoolOptions<Worker>) {
super(workerScript, startWorkerDelay);
- this.pool = StaticPool.getInstance(numberOfThreads, this.workerScript);
+ this.pool = StaticPool.getInstance(numberOfThreads, this.workerScript, opts);
}
get size(): number {
super(numberOfThreads, workerScript, opts);
}
- public static getInstance(numberOfThreads: number, workerScript: string): StaticPool {
+ public static getInstance(numberOfThreads: number, workerScript: string, opts?: PoolOptions<Worker>): StaticPool {
if (!StaticPool.instance) {
- StaticPool.instance = new StaticPool(numberOfThreads, workerScript,
- {
- exitHandler: (code) => {
- if (code !== 0) {
- console.error(`Worker stopped with exit code ${code}`);
- }
- }
+ opts.exitHandler = opts.exitHandler ?? ((code) => {
+ if (code !== 0) {
+ console.error(`Worker stopped with exit code ${code}`);
}
- );
+ });
+ StaticPool.instance = new StaticPool(numberOfThreads, workerScript, opts);
}
return StaticPool.instance;
}