10 examples of 'golang waitgroup' in Go

Every line of 'golang waitgroup' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
16func acquireWaitGroup() *sync.WaitGroup {
17 wgv := waitGroupPool.Get()
18 if wgv == nil {
19 return &sync.WaitGroup{}
20 }
21
22 return wgv.(*sync.WaitGroup)
23}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

99func (cons ConsumerBase) SetWorkerWaitGroup(workers *sync.WaitGroup) {
100 cons.state.SetWorkerWaitGroup(workers)
101}
7func Group(chans ...<-chan struct{}) <-chan struct{} {
8 var wg sync.WaitGroup
9
10 for _, c := range chans {
11 wg.Add(1)
12 go func(c <-chan struct{}) {
13 for range c {
14 }
15 wg.Done()
16 }(c)
17 }
18
19 done := make(chan struct{})
20 go func() {
21 wg.Wait()
22 close(done)
23 }()
24
25 return done
26}
606func (c *Checker) CheckWaitgroupAdd(j *lint.Job) {
607 fn := func(node ast.Node) bool {
608 g, ok := node.(*ast.GoStmt)
609 if !ok {
610 return true
611 }
612 fun, ok := g.Call.Fun.(*ast.FuncLit)
613 if !ok {
614 return true
615 }
616 if len(fun.Body.List) == 0 {
617 return true
618 }
619 stmt, ok := fun.Body.List[0].(*ast.ExprStmt)
620 if !ok {
621 return true
622 }
623 call, ok := stmt.X.(*ast.CallExpr)
624 if !ok {
625 return true
626 }
627 sel, ok := call.Fun.(*ast.SelectorExpr)
628 if !ok {
629 return true
630 }
631 fn, ok := j.Program.Info.ObjectOf(sel.Sel).(*types.Func)
632 if !ok {
633 return true
634 }
635 if fn.FullName() == "(*sync.WaitGroup).Add" {
636 j.Errorf(sel, "should call %s before starting the goroutine to avoid a race",
637 j.Render(stmt))
638 }
639 return true
640 }
641 for _, f := range j.Program.Files {
642 ast.Inspect(f, fn)
643 }
644}
158func (c *mockChain) WaitForSignal(cancel context.CancelFunc, wg *sync.WaitGroup) signalType {
159 for {
160 select {
161
162 // stat consensus spin
163 case <-c.StopLoopChan:
164 // cancel consensus spin and continue waiting for Start or Quit signals
165 cancel()
166 wg.Wait()
167 case <-c.RestartLoopChan:
168 // cancel consensus spin, return start signal
169 cancel()
170 wg.Wait()
171 return start
172 case <-c.QuitChan:
173 cancel()
174 wg.Wait()
175 return quit
176 }
177 }
178}
38func (c *CountdownLatch) Await() {
39 c.Lock()
40 defer c.Unlock()
41 if c.counter == 0 {
42 return
43 }
44 c.cond.Wait()
45}
598func wait(v cmd.Cmd, ch chan os.Signal) {
599 for sig := range ch {
600 if sig == syscall.SIGTERM {
601 if method, found := v.(io.Closer); found {
602 if err := method.Close(); err != nil {
603 fmt.Fprintln(os.Stderr, err)
604 }
605 time.Sleep(500 * time.Millisecond)
606 }
607 os.Stdout.Sync()
608 os.Stderr.Sync()
609 os.Stdout.Close()
610 os.Stderr.Close()
611 os.Exit(0)
612 }
613 break
614 }
615}
111func (g *Group) Wait() error {
112 if g.ch != nil {
113 for _, f := range g.chs {
114 g.ch <- f
115 }
116 }
117 g.wg.Wait()
118 if g.ch != nil {
119 close(g.ch) // let all receiver exit
120 }
121 if g.cancel != nil {
122 g.cancel()
123 }
124 return g.err
125}
92func (g *Group) Wait() error {
93 g.wg.Wait()
94 g.mu.Lock()
95 defer g.mu.Unlock()
96 errors := g.errors
97 l := len(errors)
98 if l == 0 {
99 return nil
100 }
101 if l == 1 {
102 return errors[0]
103 }
104 return errors
105}
12func AwaitSignals(l net.Listener) (err error) {
13 _, err = Wait(l)
14 return
15}

Related snippets