-
Notifications
You must be signed in to change notification settings - Fork 284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
I have a question about back-pressure and drain event handler #2426
Comments
You are trying to write a In the writer side: In the reader side: it has "pause" and "flowing" modes. In the moment you connect with Back to your code: if (!ws.write(data)) {
ws.once("drain", handler);
} When the writer is asking you to stop sending data (false), you are scheduling an event handler to run once, on drain. That doesn't cause the reader to stop. The correct would be stopping the reader: if (!ws.write(data)) {
rs.pause(); And resume on drain: ws.once("drain", function resume(){
rs.resume();
}); So your code would be closer to correct as const rs = getReadableStreamSomehow();
const ws = getWritableStreamSomehow();
rs.on("data", function handler(data) {
if (!ws.write(data)) {
rs.pause();
ws.once("drain", function resume() {
rs.resume();
});
}
}); That may be sufficient in terms of back-pressure; Getting error handlers correct is a bigger challenge. |
Thank you @renatomariscal . I accepted your feedback and I checked out that there will be no memory leak warning. I have a question. Could you give me your opinion about the following code snippet? const rs = getReadableStreamSomehow();
const ws = getWritableStreamSomehow();
rs.on("data", function handler(data) {
if (!ws.write(data)) {
ws.removeAllListeners("drain");
ws.once("drain", handler);
}
}); when I code like this, there are also no memory leak warning. so is it correct way? |
It is not correct, NodeJS is unable to detect the memory leak because you are resetting the listeners, but, as you are not pausing you still accumulate data in memory ignoring the ask to pause. In the current state, your code is in the best case, equivalent of: const rs = getReadableStreamSomehow();
const ws = getWritableStreamSomehow();
rs.on("data", function handler(data) {
ws.write(data);
}); |
@renatomariscal thank you. I've learned a lot. I will close this issue. |
I am currently studying node.js back-pressure myself.
I intend not to use
.pipe()
or.pipeline()
because I want to understand back-pressure and drain event.
but I don't know how to write appropriate drain handler.
let's see the below code.
it seems that the above source code has some problem. because I encountered memory leak warning from console.
Is there anyone knows how to write drain event handler?
Thank you.
The text was updated successfully, but these errors were encountered: