File tree 1 file changed +19
-0
lines changed
1 file changed +19
-0
lines changed Original file line number Diff line number Diff line change @@ -23,6 +23,7 @@ type Group struct {
23
23
tasks []taskItem
24
24
cleanup func ()
25
25
fastFail bool
26
+ queue chan struct {}
26
27
}
27
28
28
29
func (g * Group ) Append (name string , f func (ctx context.Context ) error ) {
@@ -46,6 +47,13 @@ func (g *Group) FastFail() {
46
47
g .fastFail = true
47
48
}
48
49
50
+ func (g * Group ) Concurrency (n int ) {
51
+ g .queue = make (chan struct {}, n )
52
+ for i := 0 ; i < n ; i ++ {
53
+ g .queue <- struct {}{}
54
+ }
55
+ }
56
+
49
57
func (g * Group ) Run (contextList ... context.Context ) error {
50
58
return g .RunContextList (contextList )
51
59
}
@@ -65,6 +73,14 @@ func (g *Group) RunContextList(contextList []context.Context) error {
65
73
for _ , task := range g .tasks {
66
74
currentTask := task
67
75
go func () {
76
+ if g .queue != nil {
77
+ <- g .queue
78
+ select {
79
+ case <- taskCancelContext .Done ():
80
+ return
81
+ default :
82
+ }
83
+ }
68
84
err := currentTask .Run (taskCancelContext )
69
85
errorAccess .Lock ()
70
86
if err != nil {
@@ -83,6 +99,9 @@ func (g *Group) RunContextList(contextList []context.Context) error {
83
99
taskCancel (errTaskSucceed {})
84
100
taskFinish (errTaskSucceed {})
85
101
}
102
+ if g .queue != nil {
103
+ g .queue <- struct {}{}
104
+ }
86
105
}()
87
106
}
88
107
You can’t perform that action at this time.
0 commit comments