Go submits a task to be run in the pool. If all goroutines in the pool are busy, a call to Go() will block until the task can be started.
(f func())
| 37 | // Go submits a task to be run in the pool. If all goroutines in the pool |
| 38 | // are busy, a call to Go() will block until the task can be started. |
| 39 | func (p *Pool) Go(f func()) { |
| 40 | p.init() |
| 41 | |
| 42 | if p.limiter == nil { |
| 43 | // No limit on the number of goroutines. |
| 44 | select { |
| 45 | case p.tasks <- f: |
| 46 | // A goroutine was available to handle the task. |
| 47 | default: |
| 48 | // No goroutine was available to handle the task. |
| 49 | // Spawn a new one and send it the task. |
| 50 | p.handle.Go(p.worker) |
| 51 | p.tasks <- f |
| 52 | } |
| 53 | } else { |
| 54 | select { |
| 55 | case p.limiter <- struct{}{}: |
| 56 | // If we are below our limit, spawn a new worker rather |
| 57 | // than waiting for one to become available. |
| 58 | p.handle.Go(p.worker) |
| 59 | |
| 60 | // We know there is at least one worker running, so wait |
| 61 | // for it to become available. This ensures we never spawn |
| 62 | // more workers than the number of tasks. |
| 63 | p.tasks <- f |
| 64 | case p.tasks <- f: |
| 65 | // A worker is available and has accepted the task. |
| 66 | return |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | } |
| 71 | |
| 72 | // Wait cleans up spawned goroutines, propagating any panics that were |
| 73 | // raised by a tasks. |