内容:优雅关闭channel
Channel关闭原则
1、不要在消费端关闭channel2、不要在有多个并行的生产者时对channel执行关闭 *** 作。3、只在唯一的或者最后唯一剩下的生产者协程中关闭channel,来通知消费者 已经没有值可以继续读了。只要坚持这个原则,就可以确保向一个已经关闭 的channel发送数据的情况不可能发生。
暴力关闭channel的正确方法
如果想要在消费端关闭channel,或者在多个生产者端关闭channel,可以使用recover机制来避免程序因为panic而崩溃。func SafeClose(ch chan T) (justClosed bool) { defer func() { if recover() != nil { justClosed = false } }() // assume ch != nil here. close(ch) // panic if ch is closed return true // <=> justClosed = true; return}
同理,生产者端使用:func SafeSend(ch chan T, value T) (closed bool) { defer func() { if recover() != nil { // The return result can be altered // in a defer function call. closed = true } }() ch <- value // panic if ch is closed return false // <=> closed = false; return}
礼貌的关闭channel方法
使用sync.Once来关闭channel,这样可以确保只会关闭一次type MyChannel struct { C chan T once sync.Once}func NewMyChannel() *MyChannel { return &MyChannel{C: make(chan T)}}func (mc *MyChannel) SafeClose() { mc.once.Do(func() { close(mc.C) })}
使用sync.Mutex达到同样的目的:type MyChannel struct { C chan T closed bool mutex sync.Mutex}func NewMyChannel() *MyChannel { return &MyChannel{C: make(chan T)}}func (mc *MyChannel) SafeClose() { mc.mutex.Lock() if !mc.closed { close(mc.C) mc.closed = true } mc.mutex.Unlock()}func (mc *MyChannel) IsClosed() bool { mc.mutex.Lock() defer mc.mutex.Unlock() return mc.closed}
优雅的关闭channel的方法
多个消费者,单个生产者。直接让生产者关闭channelpackage main@R_502_5565@ ( "time" "math/rand" "sync" "log")func main() { rand.Seed(time.Now().UnixNano()) log.SetFlags(0) // ... const MaxRandomNumber = 100000 const NumReceivers = 100 wgReceivers := sync.WaitGroup{} wgReceivers.Add(NumReceivers) // ... dataCh := make(chan int, 100) // the sender go func() { 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++ { go func() { 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()}
多个生产者,单个消费者,不能在消费端关闭channel,因为这违背了channel关闭原则。但是我们可以让消费端关闭一个附加的信号来通知发送端停止生产数据。package main@R_502_5565@ ( "time" "math/rand" "sync" "log")func main() { rand.Seed(time.Now().UnixNano()) log.SetFlags(0) // ... const MaxRandomNumber = 100000 const NumSenders = 1000 wgReceivers := sync.WaitGroup{} wgReceivers.Add(1) // ... dataCh := make(chan int, 100) stopCh := make(chan struct{}) // 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++ { go func() { for { // The first select here is to try to exit the goroutine // as early as possible. In fact, it is not essential // for this example, so it can be omitted. select { case <- stopCh: return default: } // Even if stopCh is closed, the first branch in the // second select may be still not selected for some // loops if the send to dataCh is also unblocked. // But this is acceptable, so the first select // can be omitted. select { case <- stopCh: return case dataCh <- rand.Intn(MaxRandomNumber): } } }() } // the receiver go func() { 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()}生产者同时也是退出信号channel的接受者,退出信号channel仍然是由它的生产端关闭的,所以这仍然没有违背channel关闭原则。值得注意的是,这个例子中生产端和接受端都没有关闭消息数据的channel,channel在没有goroutine引用的时候会自行关闭,而不需显示进行关闭。
多个生产者,多个消费者不能让接受端或发送端关闭channel。我们甚至都不能让接受者关闭一个退出信号来通知生产者停止生产。因为不能违反channel关闭原则。但可以引入一个额外的协调者来关闭附加的退出信号channel。package main@R_502_5565@ ( "time" "math/rand" "sync" "log" "strconv")func main() { 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(chan int, 100) stopCh := make(chan struct{}) // 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(chan string, 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 go func() { stoppedBy = <- toStop close(stopCh) }() // senders for i := 0; i < NumSenders; i++ { go func(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. This select blocks with one // receive operation case and one default branches will // be optimized as a try-receive operation by the // official Go compiler. select { case <- stopCh: return default: } // Even if stopCh is closed, the first branch in the // second select may be still not selected for some // loops (and for ever in theory) if the send to // dataCh is also unblocked. // This is why the first select block is needed. select { case <- stopCh: return case dataCh <- value: } } }(strconv.Itoa(i)) } // receivers for i := 0; i < NumReceivers; i++ { go func(ID string) { defer wgReceivers.Done() for { // Same as the sender goroutine, the first select here // is to try to exit the goroutine as early as possible. select { case <- stopCh: return default: } // Even if stopCh is closed, the first branch in the // second select may be still not selected for some // loops (and for ever in theory) if the receive from // dataCh is also unblocked. // This is why the first select block is needed. 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)}
总结 以上是内存溢出为你收集整理的【博客413】Go 优雅关闭channel全部内容,希望文章能够帮你解决【博客413】Go 优雅关闭channel所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)