The following content is a small extract from my latest book Beyond Effective Go – Part 1 – Achieving High-Performance Code.
Slow consumers
When designing function and method APIs that publish data to a channel, we should consider how the code will respond to slow consumers. Will the producer block until there is additional space in the channel (effectively waiting for the consumer to catch up), or will the producer discard the result and continue processing? Whichever behavior we choose should be documented on the method so that it is evident to users.
When the channel is used for event signaling, an appropriate response to slow consumers is to drop the data rather than block the producer. To achieve this, we would apply the leaky bucket write like this:
func Example(signalCh chan struct{}) {
select {
case signalCh <- struct{}{}:
// send signal
default:
// drop signal as one is already pending
}
}
By combining the above code with a buffered channel with a buffer size of one, we ensure that the first event is buffered, but any events that arrive before the first is read are dropped. With this approach, the consumer is not able to influence the producer.
A great example of this approach is time.Ticker from the standard library, whose documentation includes the line: the ticker will adjust the time interval or drop ticks to make up for slow receivers.
This pattern is not appropriate for all situations. For example, blocking the producer, often referred to as back pressure, is a better choice when the channel is used to pass data. When applying back pressure to a producer, we should consider whether it is helpful for the producer to buffer some data by using a buffered channel.
If you have enjoyed this content and would like more, please pick up a copy of Beyond Effective Go – Part 1 – Achieving High-Performance Code, and don’t forget to also check out the book’s companion source code.
Also, if you would like to be notified when there are new posts or to be kept informed regarding the upcoming book launch please join my Google Group (very low traffic and no spam).