There are some cases where you need to read enough items from a channel to begin processing. However, you also want to a TTL to expire with a smaller batch if there are not more items for a while.
Here’s an example of streaming a channel of
string into an output channel of
string that guarantees:
- Each batch contains at least one string.
- A batch will never contain more than
- If there is at least one string to be read at any given time, it will always send a new batch within
- A batch will contain less than
maxTimeouthas occurred since the the last batch finished (not since the first item in the batch). This is important because it means that if the stream is scarce and infrequent — such as one item per
BatchStringswill prefer to send a consistently timed batch of one item rather than more volatile intervals that might contain two items.
Try it now: https://play.golang.org/p/5NqYwehuxr-
The above example will halt when the channel is closed. It can also be written in a way that is context aware:
Now the stream can be stopped by either cancelling the context or closing the channel.
Try it now: https://play.golang.org/p/O7Wq1faP5qw