diff options
author | James M Snell <jasnell@gmail.com> | 2021-01-04 22:14:15 +0300 |
---|---|---|
committer | James M Snell <jasnell@gmail.com> | 2021-01-06 22:00:14 +0300 |
commit | 3518919cb7a8fca324f67c3fa3c61f1d820f61b2 (patch) | |
tree | 37401375f57e8cd994eaf700c52b1ff5c72298a1 /doc/api/async_hooks.md | |
parent | fffdaeb4c9d79031d74f1e036484c85cad51b46e (diff) |
doc: avoid memory leak warning in async_hooks example
Fixes: https://github.com/nodejs/node/issues/35952
PR-URL: https://github.com/nodejs/node/pull/36783
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
Diffstat (limited to 'doc/api/async_hooks.md')
-rw-r--r-- | doc/api/async_hooks.md | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/doc/api/async_hooks.md b/doc/api/async_hooks.md index 9a46b41d7f4..acc5d671711 100644 --- a/doc/api/async_hooks.md +++ b/doc/api/async_hooks.md @@ -839,9 +839,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() { @@ -875,7 +885,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; } |