当前位置: 首页 > news >正文

errgroup 原理简析

golang.org/x/sync/errgroup

errgroup提供了一组并行任务中错误采集的方案。

先看注释

Package errgroup provides synchronization, error propagation, and Context cancelation for groups of goroutines working on subtasks of a common task.

Group 结构体

// A Group is a collection of goroutines working on subtasks that are part of the same overall task.// A zero Group is valid and does not cancel on error.
type Group struct {cancel func()  // 内部使用的结束方法wg sync.WaitGroup // 内嵌sync.WaitGroup,用来阻塞errOnce sync.Once // 只采集一次errorerr     error     // 采集到的error
}

WithContext 返回 *Group 与 context.Context

context.Context 会在子任务发生错误,或Wait方法结束阻塞时被取消

// WithContext returns a new Group and an associated Context derived from ctx.
//
// The derived Context is canceled the first time a function passed to Go
// returns a non-nil error or the first time Wait returns, whichever occurs
// first.
func WithContext(ctx context.Context) (*Group, context.Context) {ctx, cancel := context.WithCancel(ctx) // 创建可以取消的contextreturn &Group{cancel: cancel}, ctx     // 创建并返回句柄
}

Wait 作为阻塞屏障,与WaitGroup的Wai方法作用一样

// Wait blocks until all function calls from the Go method have returned, then
// returns the first non-nil error (if any) from them.
func (g *Group) Wait() error {g.wg.Wait()     // 阻塞if g.cancel != nil { // 阻塞结束后,cancel掉整个子Context链,Wait结束阻塞g.cancel()}return g.err // 返回收集的error
}

Go 创建新的协程去执行子任务

// Go calls the given function in a new goroutine.
//
// The first call to return a non-nil error cancels the group; its error will be
// returned by Wait.
func (g *Group) Go(f func() error) {g.wg.Add(1) // 令牌+1go func() {defer g.wg.Done() // 方法执行结束后令牌-1, 令牌为0的时候WaitGroup的Wait方法结束阻塞if err := f(); err != nil { //执行传入的f()方法,并检测errg.errOnce.Do(func() {  // 如果err不为空,则将err赋值给g.err,并且只赋值一次g.err = errif g.cancel != nil { //如果cancel非空,则执行该方法,通知Context链,做取消操作。g.cancel()}})}}()
}

原理简析

	eg, ctx := errgroup.WithContext(context.Background()) // ctx还可以作为父ctx传递给其他函数调用eg.Go(func() error {return // ...})eg.Go(func() error {return // ...})if err := eg.Wait(); err != nil {// ...}

errgroup是通过封装WaitGroup,Context,sync.Once来实现的。它利用了WaitGroup的Add,Done,Wait方法实现阻塞屏障。使用context.WithCancel来实现取消策略,取消策略针对WithContext传递出的ctx。使用sync.Once实现只保存一组任务中第一次出现的error。

errgroup.Wait结束阻塞的时机:wg sync.WaitGroup的令牌归零。

ctx 传播信号的几个时机:

  1. 一组任务中任何一个子任务产生error,执行了cancel。
    在这里插入图片描述
  2. 一组任务顺利执行结束,Wait中执行了cancel。
    在这里插入图片描述
  3. 外部传入的context为可取消的context。外层调用了cancel方法。
	c, cancel := context.WithCancel(context.Background())eg, ctx := errgroup.WithContext(c)go func() {select {case <-ctx.Done():fmt.Println("结束")return}}()eg.Go(func() error {time.Sleep(time.Second * 10)return nil})eg.Go(func() error {time.Sleep(time.Second * 10)return nil})cancel()if err := eg.Wait(); err != nil {fmt.Println(err)}
http://www.lryc.cn/news/8924.html

相关文章:

  • Centos7.6 下 Docker 安装
  • C++11--lambda表达式
  • 四【Spring框架】
  • 树与二叉树 总复习
  • window10安装MySQL数据库
  • 羊了个羊游戏开发教程3:卡牌拾取和消除
  • SHA1详解
  • Go并发介绍及其使用
  • 小米手机屏幕解锁技巧精选
  • 「SDOI2009」HH去散步
  • 用上Visual Studio后,我的世界游戏的构建时间减少了一半
  • 34、基于51单片机锂电池电压电流容量检测仪表LCD液晶显示 原理图PCB程序设计
  • 【Java基础】泛型(一)-基础使用
  • 学Python不会不知道NumPy计算包吧,带你五分钟看懂NumPy计算包
  • 积水内涝监测——埋入式积水终端设备介绍
  • Kafka的日志同步
  • 【Mybatis源码解析】mapper实例化及执行流程源码分析
  • 分布式文件管理系统(MinIO)
  • Springcloud-配置中心config
  • [项目篇] 音乐播放器开发报告
  • Spring Cloud Alibaba--gateway微服务详解之网关(二)
  • Zynq非VDMA方案实现视频3帧缓存输出,无需SDK配置,提供工程源码和技术支持
  • 血液透析过滤芯气密性检测装置中的高精度多段压力控制解决方案
  • PDF加密如何批量解除?快来了解下这个方法
  • C++——哈希4|布隆过滤器
  • python冒号的用法总结
  • 面试题整理
  • C语言深度解剖-关键字(7)
  • 利用JavaScript编写Python内置函数查询工具
  • 【MySQL进阶】SQL优化