Skip to content

Commit

Permalink
fix: dont mark recycled workers as available
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Jun 28, 2023
1 parent 85f09dd commit 5a37f5e
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,10 @@ class ThreadPool {
const taskInfo = workerInfo.taskInfos.get(taskId)
workerInfo.taskInfos.delete(taskId)

if (!this.options.isolateWorkers) pool.workers.maybeAvailable(workerInfo)
// Mark worker as available if it's not about to be removed
if (!this.shouldRecycleWorker(taskInfo)) {
pool.workers.maybeAvailable(workerInfo)
}

/* istanbul ignore if */
if (taskInfo === undefined) {
Expand Down Expand Up @@ -894,18 +897,7 @@ class ThreadPool {
reject(err)
}

// When `isolateWorkers` is enabled, remove the worker after task is finished
const shouldIsolateWorker =
this.options.isolateWorkers && taskInfo.workerInfo

// When `maxMemoryLimitBeforeRecycle` is enabled, remove workers that have exceeded the memory limit
const shouldRecycleWorker =
!this.options.isolateWorkers &&
this.options.maxMemoryLimitBeforeRecycle !== undefined &&
(taskInfo.workerInfo?.usedMemory || 0) >
this.options.maxMemoryLimitBeforeRecycle

if (shouldIsolateWorker || shouldRecycleWorker) {
if (this.shouldRecycleWorker(taskInfo)) {
this._removeWorker(taskInfo.workerInfo!)
.then(() => this._ensureMinimumWorkers())
.then(() => this._ensureEnoughWorkersForTaskQueue())
Expand Down Expand Up @@ -1002,6 +994,20 @@ class ThreadPool {
return ret
}

shouldRecycleWorker(taskInfo?: TaskInfo): boolean {
// When `isolateWorkers` is enabled, remove the worker after task is finished
const isWorkerIsolated = this.options.isolateWorkers && taskInfo?.workerInfo

// When `maxMemoryLimitBeforeRecycle` is enabled, remove workers that have exceeded the memory limit
const isWorkersMemoryLimitReached =
!this.options.isolateWorkers &&
this.options.maxMemoryLimitBeforeRecycle !== undefined &&
(taskInfo?.workerInfo?.usedMemory || 0) >
this.options.maxMemoryLimitBeforeRecycle

return Boolean(isWorkerIsolated || isWorkersMemoryLimitReached)
}

pendingCapacity(): number {
return (
this.workers.pendingItems.size * this.options.concurrentTasksPerWorker
Expand Down

0 comments on commit 5a37f5e

Please sign in to comment.