Closed ivan-kleshnin closed 8 years ago
Readable streams are a tricky one to work with. In order to properly support their expectations, I would potentially need a #write()
method which returns either true or false, depending on whether or not backpressure should be assumed. Then, we would also potentially need a way to support Channel#on('drain', callback)
to tell the reads to continue piping. Both of these items would be very specific to the nodejs stream expectations. I believe they could be implemented, but it would tie us to support a specific external library as well.
If we do decide to support something like your proposal, I would probably call it #drain()
, and mimic how nodejs streams handle the drain event. I'm not convinced it would be enough, given your example code though -- I'm really not sure how a ReadStream#on('readable', callback)
would handle a callback which returns a promise. You could try something like this, instead of your callback for #on('readable')
(attaching a data
event handler puts the stream into flowing mode):
inStream.on('data', async data => {
inStream.pause();
await outChan.put(data);
inStream.resume();
});
But a glance at that has me slightly concerned about performance, especially if you have to pause/resume the instream every time.
But a glance at that has me slightly concerned about performance, especially if you have to pause/resume the instream every time.
Absolutely. In this way we can try to achieve radical memory efficiency (no buffer usage) for the cost of awful performance. As I said, it's just for learning purposes.
In general, I agree. Node Stream API is very specific and this single use case should not dictate us how the library API should look like. We definitely need to collect more usage experience. So let's close this issue for now and if (or when) we hit something similar it may be reconsidered.
I'm trying to replace Node Stream API with Channel API and get real backpressure (not buffering, throttling, highWaterMark or other heuristic approximation Node and RxJS do) for learning purpose.
Here is my attempt with comments about the problem I've encountered:
The API of
ReadableStream
made in a way that as soon as you call.read()
it immediately resumes with a next chunk. I need to pause reading somehow but there is no API to check ifChannel
is writable right now. I can't justawait channel.put(foo);
in this case. Do I miss something?