gitee.com/quant1x/gox@v1.21.2/coroutine/rolling_waitgroup.go (about)

     1  package coroutine
     2  
     3  import "sync"
     4  
     5  // RollingWaitGroup 滑动窗口n的WaitGroup
     6  type RollingWaitGroup struct {
     7  	window    int
     8  	c         chan struct{}
     9  	waitGroup sync.WaitGroup
    10  }
    11  
    12  // NewRollingWaitGroup initialization RollingWaitGroup struct
    13  func NewRollingWaitGroup(n int) *RollingWaitGroup {
    14  	return &RollingWaitGroup{
    15  		window: n,
    16  		c:      make(chan struct{}, n),
    17  	}
    18  }
    19  
    20  func (g *RollingWaitGroup) Add(delta int) {
    21  	g.c <- struct{}{}
    22  	g.waitGroup.Add(delta)
    23  }
    24  
    25  func (g *RollingWaitGroup) Done() {
    26  	g.waitGroup.Done()
    27  	<-g.c
    28  }
    29  
    30  func (g *RollingWaitGroup) Wait() {
    31  	g.waitGroup.Wait()
    32  }