funcSafeSend(ch chan T, value T)(closed bool) { deferfunc() { ifrecover() != nil { // the return result can be altered // in a defer function call closed = true } }() ch <- value // panic if ch is closed returnfalse// <=> closed = false; return }
funcmain() { rand.Seed(time.Now().UnixNano()) log.SetFlags(0) // ... const MaxRandomNumber = 100000 const NumReceivers = 100 wgReceivers := sync.WaitGroup{} wgReceivers.Add(NumReceivers) // ... dataCh := make(chanint, 100) // the sender gofunc() { for { if value := rand.Intn(MaxRandomNumber); value == 0 { // the only sender can close the channel safely. close(dataCh) return } else { dataCh <- value } } }() // receivers for i := 0; i < NumReceivers; i++ { gofunc() { defer wgReceivers.Done() // receive values until dataCh is closed and // the value buffer queue of dataCh is empty. for value := range dataCh { log.Println(value) } }() } wgReceivers.Wait() }
一个 receiver,N 个 sender,receiver 通过关闭一个额外的 signal channel 说“请停止发送”
这种场景比上一个要复杂一点。我们不能让 receiver 关闭 data channel,因为这么做将会打破 channel closing principle。但是我们可以让 receiver 关闭一个额外的 signal channel 来通知 sender 停止发送值:
funcmain() { rand.Seed(time.Now().UnixNano()) log.SetFlags(0) // ... const MaxRandomNumber = 100000 const NumSenders = 1000 wgReceivers := sync.WaitGroup{} wgReceivers.Add(1) // ... dataCh := make(chanint, 100) stopCh := make(chanstruct{}) // stopCh is an additional signal channel. // Its sender is the receiver of channel dataCh. // Its reveivers are the senders of channel dataCh. // senders for i := 0; i < NumSenders; i++ { gofunc() { for { value := rand.Intn(MaxRandomNumber) select { case <- stopCh: return case dataCh <- value: } } }() } // the receiver gofunc() { defer wgReceivers.Done() for value := range dataCh { if value == MaxRandomNumber-1 { // the receiver of the dataCh channel is // also the sender of the stopCh cahnnel. // It is safe to close the stop channel here. close(stopCh) return } log.Println(value) } }() // ... wgReceivers.Wait() }
M 个 receiver,N 个 sender,它们当中任意一个通过通知一个 moderator(仲裁者)关闭额外的 signal channel 来说“让我们结束游戏吧”
这是最复杂的场景了。我们不能让任意的 receivers 和 senders 关闭 data channel,也不能让任何一个 receivers 通过关闭一个额外的 signal channel 来通知所有的 senders 和 receivers 退出游戏。这么做的话会打破 channel closing principle。但是,我们可以引入一个 moderator 来关闭一个额外的 signal channel。这个例子的一个技巧是怎么通知 moderator 去关闭额外的 signal channel:
funcmain() { rand.Seed(time.Now().UnixNano()) log.SetFlags(0) // ... const MaxRandomNumber = 100000 const NumReceivers = 10 const NumSenders = 1000 wgReceivers := sync.WaitGroup{} wgReceivers.Add(NumReceivers) // ... dataCh := make(chanint, 100) stopCh := make(chanstruct{}) // stopCh is an additional signal channel. // Its sender is the moderator goroutine shown below. // Its reveivers are all senders and receivers of dataCh. toStop := make(chanstring, 1) // the channel toStop is used to notify the moderator // to close the additional signal channel (stopCh). // Its senders are any senders and receivers of dataCh. // Its reveiver is the moderator goroutine shown below. var stoppedBy string // moderator gofunc() { stoppedBy = <- toStop // part of the trick used to notify the moderator // to close the additional signal channel. close(stopCh) }() // senders for i := 0; i < NumSenders; i++ { gofunc(id string) { for { value := rand.Intn(MaxRandomNumber) if value == 0 { // here, a trick is used to notify the moderator // to close the additional signal channel. select { case toStop <- "sender#" + id: default: } return } // the first select here is to try to exit the // goroutine as early as possible. select { case <- stopCh: return default: } select { case <- stopCh: return case dataCh <- value: } } }(strconv.Itoa(i)) } // receivers for i := 0; i < NumReceivers; i++ { gofunc(id string) { defer wgReceivers.Done() for { // same as senders, the first select here is to // try to exit the goroutine as early as possible. select { case <- stopCh: return default: } select { case <- stopCh: return case value := <-dataCh: if value == MaxRandomNumber-1 { // the same trick is used to notify the moderator // to close the additional signal channel. select { case toStop <- "receiver#" + id: default: } return } log.Println(value) } } }(strconv.Itoa(i)) } // ... wgReceivers.Wait() log.Println("stopped by", stoppedBy) }