github.com/erda-project/erda-infra@v1.0.10-0.20240327085753-f3a249292aeb/pkg/parallel/group.go (about) 1 // Copyright (c) 2021 Terminus, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package parallel 16 17 import ( 18 "context" 19 "sync" 20 "time" 21 22 "github.com/erda-project/erda-infra/pkg/trace" 23 ) 24 25 // Group . 26 type Group struct { 27 ctx context.Context 28 cancel func() 29 30 wg sync.WaitGroup 31 32 errOnce sync.Once 33 err error 34 } 35 36 // GoGroup . 37 func GoGroup(ctx context.Context, opts ...RunOption) *Group { 38 rOpts := &runOptions{} 39 for _, opt := range opts { 40 opt(rOpts) 41 } 42 g := &Group{} 43 if rOpts.timeout > 0 { 44 g.ctx, g.cancel = context.WithTimeout(ctx, rOpts.timeout) 45 } else { 46 g.ctx, g.cancel = context.WithCancel(ctx) 47 } 48 return g 49 } 50 51 // Wait blocks until all function calls from the Go method have returned, then 52 // returns the first non-nil error (if any) from them. 53 func (g *Group) Wait() error { 54 g.wg.Wait() 55 if g.cancel != nil { 56 g.cancel() 57 } 58 return g.err 59 } 60 61 // Go calls the given function in a new goroutine. 62 // 63 // The first call to return a non-nil error cancels the group; its error will be 64 // returned by Wait. 65 func (g *Group) Go(fn func(ctx context.Context) error) { 66 g.wg.Add(1) 67 trace.GoWithContext(g.ctx, func(ctx context.Context) { 68 defer g.wg.Done() 69 if err := fn(ctx); err != nil { 70 g.errOnce.Do(func() { 71 g.err = err 72 if g.cancel != nil { 73 g.cancel() 74 } 75 }) 76 } 77 }) 78 } 79 80 // RunOption . 81 type RunOption func(opts *runOptions) 82 83 type runOptions struct { 84 timeout time.Duration 85 } 86 87 // WithTimeout . 88 func WithTimeout(timeout time.Duration) RunOption { 89 return func(opts *runOptions) { 90 opts.timeout = timeout 91 } 92 }