amphp / byte-stream

A non-blocking stream abstraction for PHP based on Amp.
https://amphp.org/byte-stream
MIT License
367 stars 30 forks source link

InputStream throttling #36

Closed ghost closed 6 years ago

ghost commented 6 years ago

Typically I read from stream like this while (($data = yield $stream->read()) !== null) and if I have readed null stream is closed.

How can I know/find that stream is closed without read from it ? How can I throttle stream ?

P.S: I want to implement something like pause(), resume() that will permit throttling, very useful for testing

kelunik commented 6 years ago

You can't know whether a stream closed without reading from it.

You can throttle a stream by just not immediately keeping to read() from it, e.g. yielding a throttling promise inside the loop.

ghost commented 6 years ago

@kelunik why not to use Emitter, I think it will be more clear when stream is closed or there is some exceptions/errors ?

kelunik commented 6 years ago

@umbri Then it's the emitter that needs to read. You're free to use another stream abstraction such as ReactPHP's implementation, that's completely compatible. We don't like callbacks, that's why we have a callback-free API for streams.

ghost commented 6 years ago

@kelunik Roger that! Thanks