import { WorkerAbstract } from './WorkerAbstract';
import type { WorkerData, WorkerOptions } from './WorkerTypes';
-import { sleep } from './WorkerUtils';
+import { randomizeDelay, sleep } from './WorkerUtils';
export class WorkerDynamicPool extends WorkerAbstract<WorkerData> {
private readonly pool: DynamicThreadPool<WorkerData>;
* @param workerScript -
* @param workerOptions -
*/
- constructor(workerScript: string, workerOptions?: WorkerOptions) {
+ constructor(workerScript: string, workerOptions: WorkerOptions) {
super(workerScript, workerOptions);
this.pool = new DynamicThreadPool<WorkerData>(
this.workerOptions.poolMinSize,
public async addElement(elementData: WorkerData): Promise<void> {
await this.pool.execute(elementData);
// Start element sequentially to optimize memory at startup
- this.workerOptions.elementStartDelay > 0 && (await sleep(this.workerOptions.elementStartDelay));
+ this.workerOptions.elementStartDelay! > 0 &&
+ (await sleep(randomizeDelay(this.workerOptions.elementStartDelay!)));
}
}