WithContext converts the pool to a ContextPool for tasks that should run under the same context, such that they each respect shared cancellation. For example, WithCancelOnError can be configured on the returned pool to signal that all goroutines should be cancelled upon the first error.
(ctx context.Context)
| 136 | // For example, WithCancelOnError can be configured on the returned pool to |
| 137 | // signal that all goroutines should be cancelled upon the first error. |
| 138 | func (p *Pool) WithContext(ctx context.Context) *ContextPool { |
| 139 | p.panicIfInitialized() |
| 140 | ctx, cancel := context.WithCancel(ctx) |
| 141 | return &ContextPool{ |
| 142 | errorPool: p.WithErrors().deref(), |
| 143 | ctx: ctx, |
| 144 | cancel: cancel, |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | func (p *Pool) worker() { |
| 149 | // The only time this matters is if the task panics. |