-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
48 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Manage how quickly messages are delivered to the channel. In theory, we | ||
// should be able to call `send()` until it returns `false` but this leads to | ||
// crashes with advanced serialization, see | ||
// <https://github.com/nodejs/node/issues/34797>. | ||
// | ||
// Even if that's fixed (and the Node.js versions with the fixes are the | ||
// minimally supported versions) we need flow control based on `send()`'s return | ||
// value. | ||
|
||
function controlFlow(channel) { | ||
let sending = false; | ||
|
||
const buffer = []; | ||
const deliverNext = () => { | ||
if (!channel.connected) { | ||
buffer.length = 0; | ||
} | ||
|
||
if (buffer.length === 0) { | ||
sending = false; | ||
return; | ||
} | ||
|
||
channel.send(buffer.shift(), deliverNext); | ||
}; | ||
|
||
return message => { | ||
if (!channel.connected) { | ||
return; | ||
} | ||
|
||
buffer.push(message); | ||
if (!sending) { | ||
sending = true; | ||
setImmediate(deliverNext); | ||
} | ||
}; | ||
} | ||
|
||
exports.controlFlow = controlFlow; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters