-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_wait_group_test.go
52 lines (42 loc) · 1.07 KB
/
util_wait_group_test.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
44
45
46
47
48
49
50
51
52
package ratelimiter
import (
"fmt"
"sync"
)
func newWaitGroup() waitGroup {
return waitGroup{
waitGroups: make(map[string]*sync.WaitGroup),
}
}
type waitGroup struct {
waitGroups map[string]*sync.WaitGroup
}
func (wg *waitGroup) Initialize(key string, initialDelta int) *waitGroup {
if _, exists := wg.waitGroups[key]; exists {
return wg
}
wg.waitGroups[key] = &sync.WaitGroup{}
wg.waitGroups[key].Add(initialDelta)
return wg
}
func (wg *waitGroup) Add(key string, delta int) {
if selectedWaitGroup, exists := wg.waitGroups[key]; exists {
selectedWaitGroup.Add(delta)
} else {
panic(fmt.Sprintf("the wait group %q is not initialized", key))
}
}
func (wg *waitGroup) Wait(key string) {
if selectedWaitGroup, exists := wg.waitGroups[key]; exists {
selectedWaitGroup.Wait()
} else {
panic(fmt.Sprintf("the wait group %q is not initialized", key))
}
}
func (wg *waitGroup) Done(key string) {
if selectedWaitGroup, exists := wg.waitGroups[key]; exists {
selectedWaitGroup.Done()
} else {
panic(fmt.Sprintf("the wait group %q is not initialized", key))
}
}