puniverse / pulsar

Fibers, Channels and Actors for Clojure
http://docs.paralleluniverse.co/pulsar/
Other
911 stars 53 forks source link

Mechanism for detecting when sending to a closed channel #79

Open sandhu opened 7 years ago

sandhu commented 7 years ago

There is currently no way to detect when you're snding to a closed channel.

In core.async, a nil is returned if the channel being "put" to is closed and a non-nil value is returned to indicate that the channel was still open and the "put" succeeded.

It would be useful to have snd behave the same way.

The use case that I have is one where a producer returns a buffered blocking channel that will contain a large stream of data. The wrinkle is that it is very expensive to produce the entire sequence of data and the client will not need to consume the entire sequence.

The producer only produces the size of the buffer in the channel and blocks until the client needs more.

In my core.async implementation, the consumer signals that it is no longer interested in the data by closing the channel. The producer detects this and stops producing. This pattern works really well and I'm using it in a variety of scenarios.

I can't implement the same pattern in pulsar, because as soon as the consumer closes the channel, the producer unblocks and ends up producing the entire sequence since there is no way to detect that the channel has been closed.

closed? returns false since there are items already in the buffer of the channel and those will never be consumed since the consumer is "done".

I realize this is a pretty fundamental change, but I'd appreciate you looking into it and considering it.

Thank you.