fs2 icon indicating copy to clipboard operation
fs2 copied to clipboard

Buffer through

Open RaasAhsan opened this issue 4 years ago • 2 comments
trafficstars

I'm starting to explore some preliminary ideas for an fs2-oriented Queue. FS2 provides an integration with the Cats Effect Queue, but it is neither stream- nor chunk-aware, so there are some inefficiencies and workarounds in place to get things to play well. Based on conversation in Gitter, some desirable properties of such fs2-oriented queue are: stream termination, chunk awareness, error propagation, and backpressure.

Before I get any hopes up, I haven't actually implement a Queue in this PR. I add a new combinator, bufferThrough, which is implemented in terms of the CE queue and I think satisfies most of the properties laid out above.

bufferThrough is pretty much the identity function, but elements are buffered through a queue. Actually, there is another function that already does exactly this: prefetch. The main difference is that bufferThrough gives you more freedom to control backpressure semantics by supplying whatever kind of queue you want, using a trick similar to the one that @SystemFw used for Async.cont. Accordingly, prefetch can be implemented in terms of bufferThrough.

So I'm not actually introducing anything groundbreaking here! My goal is rather to draw attention to yet another dimension of streaming queues: the topology of the system. prefetch and bufferThrough are both single-producer, single-consumer systems. I'm not sure yet how to generalize over the remaining three corners of the matrix, if that's even possible.

An interesting question is how stream termination would work in multiple-producer systems, if it even makes sense at all. The only behavior that makes sense to me is terminating all producers and consumers whenever a single producer terminates, but that doesn't seem very useful. At that point, I think you would just use enqueueUnterminated and dequeueUnterminated freely.

I should've probably opened a separate issue or discussion for this, but oh well.

RaasAhsan avatar Jan 08 '21 06:01 RaasAhsan

Looks good to me. I ran in to the same issue when thinking about a new fs2 queue type.

Re: bufferThrough, I'm a bit worried that it would be confused with the various buffer methods we have now. Strangely, our existing buffer operations really only do prefetching and our existing prefetch operations do buffering... I'm open to changing this for 3.0 release though, as I don't think either of those operations are heavily used.

mpilquist avatar Jan 21 '21 14:01 mpilquist

Sounds good, I can try to come up with some other names. I also don't think we really need the MakeQueue trait. It's a neat trick, but passing in an empty Queue is probably sufficient with some documentation

RaasAhsan avatar Jan 22 '21 02:01 RaasAhsan