首页 > 其他分享 >golang改进errGroup

golang改进errGroup

时间:2022-08-18 00:12:17浏览次数:61  
标签:Group errGroup sync golang 改进 cancel sem

需求

在并发控制中,想实现以下功能

1、并发超时控制

2、一个出错,主程序退出

3、兼容errGroup

然后对errGroup进行一次改写

package utils

import (
	"context"
	"errors"
	"fmt"
	"sync"
	"time"
)

type token struct{}

// A Group is a collection of goroutines working on subtasks that are part of
// the same overall task.
//
// A zero Group is valid, has no limit on the number of active goroutines,
// and does not cancel on error.
type Group struct {
	cancel func()

	wg sync.WaitGroup

	sem chan token

	errOnce sync.Once
	err     error
	unsafe  bool
	ctx     context.Context
}

var errTimeout = errors.New("超时了")

func (g *Group) done() {
	if g.sem != nil {
		<-g.sem
	}
	g.wg.Done()
}

// 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)
	return &Group{cancel: cancel, unsafe: true, ctx: ctx}, ctx
}

// GroupTimeoutContext 超时设置
func GroupTimeoutContext(parent context.Context, timeout time.Duration) (*Group, context.Context) {
	ctx, cancel := context.WithTimeout(parent, timeout)
	return &Group{cancel: cancel, unsafe: true, ctx: ctx}, ctx
}

// 超时设置
func GroupTimeout(parent context.Context, timeout time.Duration) *Group {
	ctx, cancel := context.WithTimeout(parent, timeout)
	return &Group{cancel: cancel, unsafe: true, ctx: ctx}
}

// UnsafeGroup 获取不安全的同步锁(出错就退出)
func UnsafeGroup() *Group {
	var g Group
	g.unsafe = true
	g.ctx, g.cancel = context.WithCancel(context.Background())
	return &g
}

// 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 {
	if g.unsafe {
		monitor := make(chan struct{})
		go func() {
			g.wg.Wait()
			close(monitor)
		}()
		select {
		case <-g.ctx.Done():
			g.errOnce.Do(func() {
				if nil == g.err {
					g.err = errTimeout
				}
			})
		case <-monitor:
		}
	} else {
		g.wg.Wait()
	}

	if g.cancel != nil {
		g.cancel()
	}
	return g.err
}

// Go calls the given function in a new goroutine.
// It blocks until the new goroutine can be added without the number of
// active goroutines in the group exceeding the configured limit.
//
// 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() (err error)) {
	if g.sem != nil {
		g.sem <- token{}
	}
	if g.unsafe && g.cancel == nil {
		g.ctx, g.cancel = context.WithCancel(context.Background())
	}
	g.wg.Add(1)

	go func() {
		defer g.done()
		if err := f(); err != nil {
			g.errOnce.Do(func() {
				if nil == g.err {
					g.err = err
				}
				if g.cancel != nil {
					g.cancel()
				}
			})
		}
	}()
}

// TryGo calls the given function in a new goroutine only if the number of
// active goroutines in the group is currently below the configured limit.
//
// The return value reports whether the goroutine was started.
func (g *Group) TryGo(f func() error) bool {
	if g.sem != nil {
		select {
		case g.sem <- token{}:
			// Note: this allows barging iff channels in general allow barging.
		default:
			return false
		}
	}

	if g.unsafe && g.cancel == nil {
		g.ctx, g.cancel = context.WithCancel(context.Background())
	}

	g.wg.Add(1)
	go func() {
		defer g.done()

		if err := f(); err != nil {
			g.errOnce.Do(func() {
				if nil == g.err {
					g.err = err
				}
				if g.cancel != nil {
					g.cancel()
				}
			})
		}
	}()
	return true
}

// SetLimit limits the number of active goroutines in this group to at most n.
// A negative value indicates no limit.
//
// Any subsequent call to the Go method will block until it can add an active
// goroutine without exceeding the configured limit.
//
// The limit must not be modified while any goroutines in the group are active.
func (g *Group) SetLimit(n int) {
	if n < 0 {
		g.sem = nil
		return
	}
	if len(g.sem) != 0 {
		panic(fmt.Errorf("errgroup: modify limit while %v goroutines in the group are still active", len(g.sem)))
	}
	g.sem = make(chan token, n)
}

// IsErr 判断是否执行出错
func (g *Group) IsErr(err error, message ...interface{}) bool {
	// 有错误直接返回
	if g.err != nil {
		return true
	}
	// 无错误直接返回
	if err == nil {
		return false
	}
	switch len(message) {
	case 0:
		g.errOnce.Do(func() {
			if nil == g.err {
				g.err = err
			}
			if g.cancel != nil {
				g.cancel()
			}
		})
	case 1:
		g.errOnce.Do(func() {
			if nil == g.err {
				g.err = fmt.Errorf("%v:%w", message[0], err)
			}
			if g.cancel != nil {
				g.cancel()
			}
		})
	default:
		// 格式化输出
		g.errOnce.Do(func() {
			if nil == g.err {
				s, _ := message[0].(string)
				g.err = fmt.Errorf("%s:%w", fmt.Sprintf(s, message[1:]...), err)
			}
			if g.cancel != nil {
				g.cancel()
			}
		})
	}
	return true
}

func IsTimeout(err error) bool {
	if err == nil {
		return false
	}
	return errors.Is(err, errTimeout)
}

 

标签:Group,errGroup,sync,golang,改进,cancel,sem
From: https://www.cnblogs.com/hardykay/p/16597300.html

相关文章

  • golang gin简介,特性,快速开始,小结
    简介Gin是一个用Go(Golang)编写的Web框架。它具有类似martini的API,性能要好得多,多亏了httprouter,速度提高了40倍。特性快速基于Radix树的路由,小内存占......
  • golang Sync.Mutex互斥锁和Sync.RWMutex读写锁小结
    Sync.Mutex一、结构体typeMutexstruct{stateint32//互斥锁的状态:被g持有,空闲等semauint32//信号量,用于阻塞/唤醒goroutine(协程)}//使用varmtx......
  • golang-rabbitmq 客户端参数
    一、交换器声明方法参数说明   func(ch*Channel)ExchangeDeclare(name,kindstring,durable,autoDelete,internal,noWaitbool,argsTable)error 参数......
  • Golang实现set
    背景Golang语言本身未实现set,但是实现了mapgolang的map是一种无序的键值对的集合,其中键是唯一的而set是键的不重复的集合,因此可以用map来实现setEmpty由于map是key-va......
  • golang之Redis
    Redis是一个基于内存的非关系型数据库,在项目开发中使用非常广泛,Go语言操作Redis需要使用三方包,我们选择支持Redis集群和Redis哨兵的go-redis包来讲述Go语言如......
  • golang之jwt的token登录
    什么是JSONWebToken?JSONWebToken(JWT)是一个开放标准(RFC7519),它定义了一种紧凑且自包含的方式,用于在各方之间以JSON方式安全地传输信息。由于此信息是经过数字签名的......
  • Golang框架之gin
    gin是目前golang的主要web框架之一,之所以选择这个框架是因为其拥有高效的路由性能,并且有人长期维护,目前github上的star数已经破3W。 ......
  • golang 企业转账到零钱
    packagemainimport("bytes""crypto/md5""crypto/tls""encoding/hex""encoding/xml""fmt""io/ioutil""net/http""net/u......
  • golang 实现生产者消费者模式(转)
    方法一:用两个通道+A协程sleep一个通道用来传数据,一个用来传停止信号。packagemainimport( "fmt" "time")//老师视频里的生产者消费者funcmain(){ //......
  • golang超时控制(转)
    Go实现超时退出之前手写rpc框架的时候,吃多了网络超时处理的苦,今天偶然发现了实现超时退出的方法,MARKfuncAsyncCall(){ ctx,cancel:=context.WithTimeout(context.......