Skip to content

Commit

Permalink
doc: avoid memory leak warning in async_hooks example
Browse files Browse the repository at this point in the history
Fixes: #35952

PR-URL: #36783
Reviewed-By: Gerhard Stöbich <[email protected]>
Reviewed-By: Juan José Arboleda <[email protected]>
  • Loading branch information
jasnell authored and targos committed May 1, 2021
1 parent 70cbe4a commit e973bdc
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion doc/api/async_hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -833,9 +833,19 @@ class WorkerPool extends EventEmitter {
this.numThreads = numThreads;
this.workers = [];
this.freeWorkers = [];
this.tasks = [];

for (let i = 0; i < numThreads; i++)
this.addNewWorker();

// Any time the kWorkerFreedEvent is emitted, dispatch
// the next task pending in the queue, if any.
this.on(kWorkerFreedEvent, () => {
if (this.tasks.length > 0) {
const { task, callback } = this.tasks.shift();
this.runTask(task, callback);
}
});
}

addNewWorker() {
Expand Down Expand Up @@ -869,7 +879,7 @@ class WorkerPool extends EventEmitter {
runTask(task, callback) {
if (this.freeWorkers.length === 0) {
// No free threads, wait until a worker thread becomes free.
this.once(kWorkerFreedEvent, () => this.runTask(task, callback));
this.tasks.push({ task, callback });
return;
}

Expand Down

0 comments on commit e973bdc

Please sign in to comment.