Skip to content

Commit

Permalink
Merge pull request #25 from penwern/fix-multipart-checksums
Browse files Browse the repository at this point in the history
spawn additional workers on request rather than immediately
  • Loading branch information
Sunday-Crunk authored Nov 20, 2024
2 parents 2ee6a9d + fcdeec8 commit 45208ae
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/js/core/WorkerManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,26 @@ class CurateWorkerManager {
return new Promise((resolve, reject) => {
const task = { file, resolve, reject };
this.taskQueue.push(task);
this.ensureWorkers();
});
}

ensureWorkers() {
// Create workers up to pool size if there are pending tasks
if (this.taskQueue.length > 0) {
while (this.workers.size < this.poolSize) {
// Only create a new worker if we have more tasks than workers
// and we haven't reached the pool size limit
if (
this.taskQueue.length > this.workers.size &&
this.workers.size < this.poolSize
) {
const workerId = this.initWorker();
this.processNextTask(workerId, this.workers.get(workerId));
}
}
// If we have available workers, find one and process the task
else if (this.workers.size > 0) {
for (const [workerId, worker] of this.workers) {
if (!this.currentTasks.has(workerId)) {
this.processNextTask(workerId, worker);
break;
}
}
}
});
}

processNextTask(workerId, worker) {
Expand All @@ -74,7 +82,6 @@ class CurateWorkerManager {
this.workers.clear();
}

// Optional: Method to manually cleanup if needed
terminate() {
this.cleanupWorkers();
this.taskQueue = [];
Expand Down

0 comments on commit 45208ae

Please sign in to comment.