I have a chunk of channels that all receive the same message:
func broadcast(c <-chan string, chans []chan<- string) { for msg := range c { for _, ch := range chans { ch <- msg } } }
However, since each of the channels in chans potentially read at a different speed, I do not want to block the other channels when I receive a slow consumer. I solved this with goroutines:
func broadcast(c <-chan string, chans []chan<- string) { for msg := range c { for _, ch := range chans { go func() { ch <- msg }() } } }
However, the order of the messages transmitted to each channel is important. I looked at the specification to see if the channels keep order when locked, and all I found was the following:
If the capacity is greater than zero, the channel is asynchronous: communication operations are successful without blocking, if the buffer is not full (sent) or empty (receives), and the elements are received in the order in which they are sent.
For me, if a record is locked, it is not sent, but is waiting for sending. In this assumption, the above does not say anything about the sending order, when several letters are blocked during recording.
Are there any guarantees regarding the sending procedure after unlocking the channel?
tjameson
source share