fortify2-js
Version:
MOST POWERFUL JavaScript Security Library! Military-grade cryptography + 19 enhanced object methods + quantum-resistant algorithms + perfect TypeScript support. More powerful than Lodash with built-in security.
57 lines (54 loc) • 1.56 kB
JavaScript
'use strict';
/**
* FortifyJS - Worker Pool
* Manages worker threads for CPU and I/O intensive tasks
*/
class WorkerPool {
constructor(config) {
this.activeTasks = 0;
this.taskQueue = [];
this.cpuWorkers = config.cpu;
this.ioWorkers = config.io;
this.maxTasks = config.maxConcurrentTasks;
}
async execute(task, type = 'io') {
if (this.activeTasks >= this.maxTasks) {
return new Promise((resolve, reject) => {
this.taskQueue.push(async () => {
try {
const result = await task();
resolve(result);
}
catch (error) {
reject(error);
}
});
});
}
this.activeTasks++;
try {
const result = await task();
return result;
}
finally {
this.activeTasks--;
if (this.taskQueue.length > 0) {
const nextTask = this.taskQueue.shift();
if (nextTask) {
setImmediate(() => nextTask());
}
}
}
}
getStats() {
return {
cpuWorkers: this.cpuWorkers,
ioWorkers: this.ioWorkers,
activeTasks: this.activeTasks,
queuedTasks: this.taskQueue.length,
maxTasks: this.maxTasks,
};
}
}
exports.WorkerPool = WorkerPool;
//# sourceMappingURL=WorkerPool.js.map