github.phpd.cn/goreleaser/goreleaser@v0.92.0/internal/semerrgroup/sem.go (about)

     1  // Package semerrgroup wraps a error group with a semaphore with configurable
     2  // size, so you can control the number of tasks being executed simultaneously.
     3  package semerrgroup
     4  
     5  import "golang.org/x/sync/errgroup"
     6  
     7  // Group is the Semphore ErrorGroup itself
     8  type Group struct {
     9  	ch chan bool
    10  	g  errgroup.Group
    11  }
    12  
    13  // New returns a new Group of a given size.
    14  func New(size int) *Group {
    15  	return &Group{
    16  		ch: make(chan bool, size),
    17  		g:  errgroup.Group{},
    18  	}
    19  }
    20  
    21  // Go execs one function respecting the group and semaphore.
    22  func (s *Group) Go(fn func() error) {
    23  	s.g.Go(func() error {
    24  		s.ch <- true
    25  		defer func() {
    26  			<-s.ch
    27  		}()
    28  		return fn()
    29  	})
    30  }
    31  
    32  // Wait waits for the group to complete and return an error if any.
    33  func (s *Group) Wait() error {
    34  	return s.g.Wait()
    35  }