How to Gracefully Close Channels,这篇博客讲了如何优雅的关闭channel的技巧,好好研读,收获良多。

众所周知,在golang中,关闭或者向已关闭的channel发送数据都会引发panic。

谨遵优雅关闭channel的原则

  • 不要在接受一端关闭channel
  • 不要在有多个并发的senders中关闭channel。反过来说,如果只有一个协程充当sender,那么我们可以在这个sender协程内关闭掉channel。

一个简单的方法

  • SafeClose
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()
defer mc.mutex.Unlock()
if !mc.closed {
close(mc.C)
mc.closed = true
}
} func (mc *MyChannel) IsClosed() bool {
mc.mutex.Lock()
defer mc.mutex.Unlock()
return mc.closed
}
  • SafeSend
func SafeSend(ch chan T, value T) (closed bool) {
defer func() {
if recover() != nil {
closed = true
}
}() ch <- value // panic if ch is closed
return false // <=> closed = false; return
}
  • [x] 那边英文博客有一句话

One drawback of the above SafeSend function is that its calls can't be used as send operations which follow the case keyword in select blocks.

这里指的是SafeSend方法不能用在select...case...的case接受操作中,即

select {
case <- SafeSend(ch, 1)
}

因为case后面需要一个channel。

优雅关闭channel的设计

  • 多个receivers,一个sender的情况。
package main

import (
"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()
}
  • 一个receiver,多个senders的情况。
package main

import (
"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 receivers are the senders of channel dataCh. // senders
for i := 0; i < NumSenders; i++ {
go func() {
for {
// The try-receive operation is to try to exit
// the goroutine as early as possible. For this
// specified example, it is not essential.
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 for this example, so the
// first select block above 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 channel.
// It is safe to close the stop channel here.
close(stopCh)
return
} log.Println(value)
}
}() // ...
wgReceivers.Wait()
}
  • 多个receivers和多个senders
package main

import (
"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 receivers 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 receiver is the moderator goroutine shown below.
// It must be a buffered channel. 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, the try-send operation is to notify the
// moderator to close the additional signal channel.
select {
case toStop <- "sender#" + id:
default:
}
return
} // The try-receive operation here is to try to exit the
// sender goroutine as early as possible. Try-receive
// try-send select blocks are specially optimized by the
// standard Go compiler, so they are very efficient.
select {
case <- stopCh:
return
default:
} // Even if stopCh is closed, the first branch in this
// select block may be still not selected for some
// loops (and for ever in theory) if the send to dataCh
// is also non-blocking. If this is not acceptable,
// then the above try-receive operation is essential.
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 try-receive
// operation here is to try to exit the receiver
// goroutine as early as possible.
select {
case <- stopCh:
return
default:
} // Even if stopCh is closed, the first branch in this
// select block may be still not selected for some
// loops (and for ever in theory) if the receive from
// dataCh is also non-blocking. If this is not acceptable,
// then the above try-receive operation is essential.
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)
}

如何优雅的关闭golang的channel的更多相关文章

  1. 如何优雅的关闭Golang Channel?

    Channel关闭原则 不要在消费端关闭channel,不要在有多个并行的生产者时对channel执行关闭操作. 也就是说应该只在[唯一的或者最后唯一剩下]的生产者协程中关闭channel,来通知消费 ...

  2. golang的channel实现

    golang的channel实现位于src/runtime/chan.go文件.golang中的channel对应的结构是: // Invariants: // At least one of c.s ...

  3. golang的Channel

    golang的Channel Channel 是 golang 一个非常重要的概念,如果你是刚开始使用 golang 的开发者,你可能还没有真正接触这一概念,本篇我们将分析 golang 的Chann ...

  4. 如何优雅的关闭Java线程池

    面试中经常会问到,创建一个线程池需要哪些参数啊,线程池的工作原理啊,却很少会问到线程池如何安全关闭的. 也正是因为大家不是很关注这块,即便是工作三四年的人,也会有因为线程池关闭不合理,导致应用无法正常 ...

  5. Effective java 系列之更优雅的关闭资源-try-with-resources

    背景: 在Java编程过程中,如果打开了外部资源(文件.数据库连接.网络连接等),我们必须在这些外部资源使用完毕后,手动关闭它们.因为外部资源不由JVM管理,无法享用JVM的垃圾回收机制,如果我们不在 ...

  6. 更优雅地关闭资源 - try-with-resource及其异常抑制

    原文:https://www.cnblogs.com/itZhy/p/7636615.html 一.背景 我们知道,在Java编程过程中,如果打开了外部资源(文件.数据库连接.网络连接等),我们必须在 ...

  7. Java进阶知识点:更优雅地关闭资源 - try-with-resource

    一.背景 我们知道,在Java编程过程中,如果打开了外部资源(文件.数据库连接.网络连接等),我们必须在这些外部资源使用完毕后,手动关闭它们.因为外部资源不由JVM管理,无法享用JVM的垃圾回收机制, ...

  8. Java进阶知识点3:更优雅地关闭资源 - try-with-resource及其异常抑制

    一.背景 我们知道,在Java编程过程中,如果打开了外部资源(文件.数据库连接.网络连接等),我们必须在这些外部资源使用完毕后,手动关闭它们.因为外部资源不由JVM管理,无法享用JVM的垃圾回收机制, ...

  9. 如何优雅地关闭一个socket

    最近在windows编程时需要考虑到“如何优雅地关闭一个socket”,查阅了一些资料,现将查到的相关资料做个汇编,希望能对后来者有所帮助(比较懒,所以英文资料没有翻译:-)) 1. 关闭Socket ...

随机推荐

  1. C# WPF 父控件通过使用可视化树找到子控件

    在我们使用WPF设计前台界面时,经常会重写数据模板,或者把控件放到数据模板里.但是一旦将控件放到数据模板中,在后台就没有办法通过控件的名字来获取它了,更没办法对它进行操作(例如,隐藏,改变控件的某个值 ...

  2. keepalived高可用简介与配置

    keepalived简介 keepalived介绍 Keepalived 软件起初是专为LVS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了可以实现高可用的VRRP ...

  3. win10下使用powershell来获取文件MD5的命令

    Get-FileHash 文件路径 -Algorithm MD5| Format-List

  4. Appium+Python自动化 4 -appium元素定位

    appium定位app上的元素方式 在前面文章中有介绍,(通过UIautomator工具查看app元素) appium定位app上的元素,可以通过id,name,class这些属性定位到 1.id 定 ...

  5. hashtable and hashmap

    hashmap的bucket 和 ‘负载因子’的介绍 https://blog.csdn.net/wenyiqingnianiii/article/details/52204136

  6. 大前端学习笔记【七】关于CSS再次整理

    如果你在日常工作中使用 CSS,你的主要目标可能会重点围绕着使事情“看起来正确”.如何实现这一点经常是远不如最终结果那么重要.这意味着比起正确的语法和视觉结果来说,我们更少关心 CSS 的工作原理. ...

  7. 20175316盛茂淞 2018-2019-2《Java程序设计》结对编程项目-四则运算 第二周(6)

    20175316与20175329 结对编程练习_四则运算(第二周) 1.需求分析 实现一个命令行程序,要求: 自动生成指定数量的小学四则运算题目(加.减.乘.除) 支持整数 统计正确率 支持多运算符 ...

  8. 受限filterbanks

    2.Related works to filterbank learning 虽然DNN-HMM模型的性能比GMM-HMM模型相比,具有很大的优势,但是训练集和测试集之间的失配问题,也使得DNN-HM ...

  9. P3047 [USACO12FEB]附近的牛Nearby Cows

    https://www.luogu.org/problemnew/show/P304 1 #include <bits/stdc++.h> 2 #define up(i,l,r) for( ...

  10. 《Linux就该这么学》第十九天课程

    今天对“Linux就该这么学”课程做个收尾 最后一张使用LNMP架构部署动态网站环境 第1步:下载及解压源码包文件.为了方便在网络中传输,源码包文件通常会在归档后使用gzip或bzip2等格式进行压缩 ...