0

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.

"use strict";

const rs = getReadableStreamSomehow();
const ws = getWritableStreamSomehow();

rs.on("data", function handler(data) {
  if (!ws.write(data)) {
    ws.once("drain", handler);
  }
});

it seems that the above source code has some problem. because I encountered memory leak warning from console.

(node:29788) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 drain listeners added to [WriteStream]. Use emitter.setMaxListeners() to increase limit

Is there anyone knows how to write drain event handler?
Thank you.

1 Answers1

0

I think the following is the right way to handle backpressure and drain event.

const rs = getReadableStreamSomehow();
const ws = getWritableStreamSomehow();

rs.on("data", function (data) {
  if (!ws.write(data)) {
    rs.pause();
    ws.once("drain", function () {
      rs.resume();
    });
  }
});

is it correct?

if it is not correct, please leave a comment.