// 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
// context 的 cancel 方法
cancel func()
wg sync.WaitGroup
//传递信号的通道,这里主要是用于控制并发创建 goroutine 的数量
//通过 SetLimit 设置过后,同时创建的goroutine 最大数量为n
sem chan token
// 保证只接受一次错误
errOnce sync.Once
// 最先返回的错误
err error
看结构体中的内容,发现比原生的sync.WaitGroup多了下面的内容:
cancel func()
sem chan token
errOnce sync.Once
err error
2.2 WithContext 方法
// 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 &Groupcancel: cancel, ctx
// 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\'s context, if the
// group was created by calling WithContext. The error will be returned by Wait.
func (g *Group) Go(f func() error)
if g.sem != nil
//往 sem 通道中发送空结构体,控制并发创建 goroutine 的数量
g.sem <- token
g.wg.Add(1)
go func()
// done()函数的逻辑就是当 f 执行完后,从 sem 取一条数据,并且 g.wg.Done()
defer g.done()
if err := f(); err != nil
g.errOnce.Do(func() // 这里就是确保 g.err 只被赋值一次
g.err = err
if g.cancel != nil
g.cancel() //这里就是为啥不用手动执行 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: // 当g.sem的缓冲区满了过后,就会执行default,也代表着未启动成功
// Note: this allows barging iff channels in general allow barging.
default:
return false
//----主要看上面的逻辑,下面的逻辑和Go中的一样-------
g.wg.Add(1)
go func()
defer g.done()
if err := f(); err != nil
g.errOnce.Do(func()
g.err = err
if g.cancel != nil
g.cancel()
)
()
return true
// 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() //通过 g.wg.Wait() 阻塞等待所有的 goroutine 执行完
if g.cancel != nil
//我看这里的时候,有点疑惑,为啥这里会去调用 cancel()方法呢?
//这里是为了代码的健壮性,用 context.WithCancel() 创建得到的 cancel,在代码执行完毕之前取消是一个好习惯
g.cancel()
return g.err
// 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)
elasticsearch@5.6 is keg-only, which means it was not symlinked into /usr/local, because this is an alternate version of another formula.
If you need to have elasticsearch@5.6 first in your PATH run: echo 'export PATH="/usr/local/opt/elasticsearch@5.6/bin:$PATH"' >> ~/.zshrc
To have launchd start elasticsearch@5.6 now and restart at login: brew services start elasticsearch@5.6 Or, if you don't want/need a background service you can just run: /usr/local/opt/elasticsearch@5.6/bin/elasticsearch ==> Summary