Skip to content

Instantly share code, notes, and snippets.

@WhisperingChaos
Created February 15, 2018 03:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WhisperingChaos/d3547a0e9fe81c5b6004e5f73d622963 to your computer and use it in GitHub Desktop.
Save WhisperingChaos/d3547a0e9fe81c5b6004e5f73d622963 to your computer and use it in GitHub Desktop.
Nil Channel favor/prioritize channels: order by bias
func consumeOrderedByBias(msg_1ch chan msg_1, msg_2ch chan msg_2) {
// copy channels to enable their restoration
msg_1_save := msg_1ch
msg_2_save := msg_2ch
// bias function encoded as a for loop
for msgCnt := 0; msgCnt < 21; msgCnt++ {
// use modulus math to help implement bias function
if msgCnt%3 == 0 {
// favor channel 1 when processing muliples of 3
msg_1ch = msg_1_save
// bias channel 2
msg_2ch = nil
} else {
// favor channel 2 when not a multiple of 3
msg_2ch = msg_2_save
// bias channel 1
msg_1ch = nil
}
select {
case msg, ok := <-msg_1ch:
if ok {
msg.msgType()
}
case msg, ok := <-msg_2ch:
if ok {
msg.msgType()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment