If I had to process messages from both high and low priority channels I would serialise then with a min heap and create a consumer process for the min heap. The min heap seems to be the canonical way of solving the priority queues anyway, so I think it makes sense here.
Basically: goroutine 1 reads both channels and insert them into the min-heap. goroutine 2 reads from min heap and do the processing. Daniela Petruzalek Software Engineer github.com/danicat twitter.com/danicat83 Em qui, 14 de fev de 2019 às 15:34, <kangli...@gmail.com> escreveu: > select { > case highVal := <- high: > case lowVal := <- low: > if len(high) > 0 { > for len(high) > 0 { > highVal := <- high > } > } > // process lowVal > } > > > > On Tuesday, July 24, 2012 at 5:16:16 AM UTC+8, Erwin Driessens wrote: >> >> Hello, >> >> i wonder how to implement channel priorities nicely. Say there are two >> channels, one with a higher priority than the other, and a goroutine >> waiting for incoming data on these channels. I have read that select picks >> a random case when multiple channels are ready. I thought of nesting >> selects: putting the lower priority select in the default case of the >> higher priority select, and have the default case of the inner (low >> priority) select do nothing. This leads to a kind of busy wait loop. I >> could call a short sleep, but that still isn't very clean. Is there a >> better way? >> >> Why aren't the select cases evaluated in order? >> > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CA%2B%3Diygt2bxH1E8fkD5JpPV1vKrz2ns%2BD5KpbhZ_SSm2vK%3Dds3w%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.