TryGo calls the given function in a new goroutine only if the number of active goroutines in the group is currently below the configured limit. The return value reports whether the goroutine was started.
(f func() error)
| 74 | // |
| 75 | // The return value reports whether the goroutine was started. |
| 76 | func (g *ErrWaitGroup) TryGo(f func() error) bool { |
| 77 | if g.sem != nil { |
| 78 | select { |
| 79 | case g.sem <- token{}: |
| 80 | // Note: this allows barging iff channels in general allow barging. |
| 81 | default: |
| 82 | return false |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | g.wg.Add(1) |
| 87 | go func() { |
| 88 | defer g.done() |
| 89 | |
| 90 | if err := f(); err != nil { |
| 91 | g.mu.Lock() |
| 92 | err = Join(g.err, err) |
| 93 | g.mu.Unlock() |
| 94 | } |
| 95 | }() |
| 96 | return true |
| 97 | } |
| 98 | |
| 99 | // SetLimit limits the number of active goroutines in this group to at most n. |
| 100 | // A negative value indicates no limit. |