dvlsg / async-csp

CSP style channels using ES7 async/await
MIT License
317 stars 18 forks source link

Possible uses for channel buffers? #25

Closed johnloy closed 6 years ago

johnloy commented 6 years ago

After trying out async-csp—thank you so much for developing it by the way—I've been searching around for practical use cases for buffered channels, but without much success. So far in my experimentation with CSP, and async-csp in particular, I've only been using non-buffered ("rendezvous"?) blocking put/take. It seems to suffice for everything I've tried to achieve so far. I can't help but think I might be missing some crucial insight that could lead to more elegant solutions through buffering, though.

The only possible example that comes to my mind is awaiting puts from multiple channels for the purpose of aggregation, and then taking the latest from each once puts have been made to all.

Much appreciation in advance for any help you can offer.

dvlsg commented 6 years ago

I'm sure there's a large number of uses, but I find myself typically using it in one of two scenarios:

  1. I have multiple producers / consumers working concurrently, and I don't want to have each of them wait for a single position to become available. It's slightly less useful in this context compared to some other languages, since concurrently doesn't turn into parallel very easily with javascript, but it can still be useful. Say for example you want to make sure you only have 5 async calls in flight at any given time - you could use a buffer size of 5 to assist with that.
  2. I am expecting bursts of incoming information, and since I'm expecting it, I'd like to just put all of those pieces onto the internal buffer. Of course if the input is still fairly one sided, you're likely to just fill up the buffer and then block at, say, 100 items instead of 1. Still, it's occasionally nice to dump things into the channel in a way that will resolve when there's space for it, as opposed to when there's a consumer ready to take it.
johnloy commented 6 years ago

@dvlsg Thanks! Very helpful. The possibility of constraining concurrent activity is one that could really come in handy.

dvlsg commented 6 years ago

Of course!

I'll go ahead and close this out, but feel to ask any other questions as you run across them, and I'll do my best to answer.