This is from stackoverflow question
- There are N channels.
- Every receiver wants to get the next message from one of the subsets of the channels.
- Need not to loose the messages and keep the messages order by the time.
Here is an example.
- We have two channels C1 and C2.
- There is a new message in the C2 channel. No receivers. Waiting.
- The first receiver wants to read messages from C1 only. No messages in C1. Waiting.
- A new message appears in the C1. The first receiver gets this message.
- There is a new message in the C1 channel. No receivers. We have messages in C1 and C2 channels.
- The second receiver wants to read messages from C1 and C2. The following is very important. The second receiver should get the message exactly from C2 (not a random from C1 or C2) because the message in the C2 channel appeared earlier!
Output:
$ go run main.go 12:05:50
want from Chan CH1
want from Chan CH1
[CH1] waiting for 2 seconds before checking again
want from Chan CH1
result from CH1: 1
want from Chan CH2
result from CH1 & CH2: 2
[CH1] waiting for 2 seconds before checking again
[CH1] waiting for 2 seconds before checking again
waiting for 5 minute before sending int to CH2
Press CTRL+C to exit
result from CH1: 1
^CExited