How to know a buffered channel is full? I don\'t know to be blocked when the buffered channel is full, instead I choose to drop the item sent to the buffered channel.
instead I choose to drop the item sent to the buffered channel.
That is called "overflowing channel", and you find ANisus's answer implemented in eapache/channels/overflowing_channel.go:
for elem := range ch.input {
// if we can't write it immediately, drop it and move on
select {
case ch.output <- elem:
default:
}
}
close(ch.output)
But that project eapache/channels implements other strategies as well:
Channel
interface in a way that never blocks the writer.OverflowingChannel
when its buffer is fullFor the opposite behaviour (discarding the oldest element, not the newest) see RingChannel.