2018-07-10 07:04:25 +02:00
|
|
|
// Package semerrgroup wraps a error group with a semaphore with configurable
|
|
|
|
// size, so you can control the number of tasks being executed simultaneously.
|
2018-07-10 06:38:00 +02:00
|
|
|
package semerrgroup
|
|
|
|
|
2019-10-06 19:58:33 +02:00
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/goreleaser/goreleaser/internal/pipe"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
)
|
2018-07-10 06:38:00 +02:00
|
|
|
|
2020-05-26 05:48:10 +02:00
|
|
|
// Group is the Semphore ErrorGroup itself.
|
2019-08-05 13:36:14 +02:00
|
|
|
type Group interface {
|
|
|
|
Go(func() error)
|
|
|
|
Wait() error
|
2018-07-10 06:38:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new Group of a given size.
|
2019-08-05 13:36:14 +02:00
|
|
|
func New(size int) Group {
|
2022-06-11 21:54:55 +02:00
|
|
|
var g errgroup.Group
|
|
|
|
g.SetLimit(size)
|
|
|
|
return &g
|
2019-08-05 13:36:14 +02:00
|
|
|
}
|
2019-10-06 19:58:33 +02:00
|
|
|
|
|
|
|
var _ Group = &skipAwareGroup{}
|
|
|
|
|
|
|
|
// NewSkipAware returns a new Group of a given size and aware of pipe skips.
|
|
|
|
func NewSkipAware(g Group) Group {
|
|
|
|
return &skipAwareGroup{g: g}
|
|
|
|
}
|
|
|
|
|
|
|
|
type skipAwareGroup struct {
|
|
|
|
g Group
|
|
|
|
skipErr error
|
|
|
|
skipOnce sync.Once
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go execs runs `fn` and saves the result if no error has been encountered.
|
|
|
|
func (s *skipAwareGroup) Go(fn func() error) {
|
|
|
|
s.g.Go(func() error {
|
2021-04-25 19:20:49 +02:00
|
|
|
err := fn()
|
2019-10-06 19:58:33 +02:00
|
|
|
// if the err is a skip, set it for later, but return nil for now so the
|
|
|
|
// the group proceeds.
|
|
|
|
if pipe.IsSkip(err) {
|
|
|
|
s.skipOnce.Do(func() {
|
|
|
|
s.skipErr = err
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait waits for Go to complete and returns the first error encountered.
|
|
|
|
func (s *skipAwareGroup) Wait() error {
|
|
|
|
// if we got a "real error", return it, otherwise return skipErr or nil.
|
|
|
|
if err := s.g.Wait(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return s.skipErr
|
|
|
|
}
|