Go will run the given function in a worker of the pool. If all workers are busy, Go() will spawn a new goroutine to run the workload.
(f func())
| 29 | // Go will run the given function in a worker of the pool. |
| 30 | // If all workers are busy, Go() will spawn a new goroutine to run the workload. |
| 31 | func (p *ReusableGoroutinesPool) Go(f func()) { |
| 32 | select { |
| 33 | case p.jobs <- f: |
| 34 | default: |
| 35 | if pending := p.pending.Dec(); pending >= 0 { |
| 36 | p.newWorker(f) |
| 37 | return |
| 38 | } else if pending < math.MinInt64/2 { |
| 39 | // Wow, that's a lot of goroutines created, make sure we don't overflow. |
| 40 | p.pending.Store(0) |
| 41 | } |
| 42 | go f() |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | func (p *ReusableGoroutinesPool) newWorker(f func()) { |
| 47 | go func() { |