-
Notifications
You must be signed in to change notification settings - Fork 40
/
pool.go
43 lines (37 loc) · 889 Bytes
/
pool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package utils
import (
"math"
"sync"
)
// WaitGroupPool pool of WaitGroup
type WaitGroupPool struct {
pool chan struct{}
wg *sync.WaitGroup
}
// NewWaitGroupPool creates a sized pool for WaitGroup
func NewWaitGroupPool(size int) *WaitGroupPool {
if size <= 0 {
size = math.MaxInt32
}
return &WaitGroupPool{
pool: make(chan struct{}, size),
wg: &sync.WaitGroup{},
}
}
// Add increments the WaitGroup counter by one.
// See sync.WaitGroup documentation for more information.
func (p *WaitGroupPool) Add() {
p.pool <- struct{}{}
p.wg.Add(1)
}
// Done decrements the WaitGroup counter by one.
// See sync.WaitGroup documentation for more information.
func (p *WaitGroupPool) Done() {
<-p.pool
p.wg.Done()
}
// Wait blocks until the WaitGroup counter is zero.
// See sync.WaitGroup documentation for more information.
func (p *WaitGroupPool) Wait() {
p.wg.Wait()
}