Skip to content

Commit

Permalink
pool: update WithFirstError docs for WithCancelOnError
Browse files Browse the repository at this point in the history
  • Loading branch information
bobheadxi committed Jan 8, 2023
1 parent f821312 commit 25effda
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions pool/context_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ type ContextPool struct {
ctx context.Context
cancel context.CancelFunc

failFast bool
cancelOnError bool
}

// Go submits a task. If it returns an error, the error will be
// collected and returned by Wait().
func (p *ContextPool) Go(f func(ctx context.Context) error) {
p.errorPool.Go(func() error {
err := f(p.ctx)
if err != nil && p.failFast {
if err != nil && p.cancelOnError {
// Leaky abstraction warning: We add the error directly because
// otherwise, canceling could cause another goroutine to exit and
// return an error before this error was added, which breaks the
Expand All @@ -41,8 +41,8 @@ func (p *ContextPool) Wait() error {

// WithFirstError configures the pool to only return the first error
// returned by a task. By default, Wait() will return a combined error.
// This is particularly useful for ContextPool where all errors after the
// first are likely to be context.Canceled.
// This is particularly useful for (*ContextPool).WithCancelOnError(),
// where all errors after the first are likely to be context.Canceled.
func (p *ContextPool) WithFirstError() *ContextPool {
p.errorPool.WithFirstError()
return p
Expand All @@ -51,8 +51,13 @@ func (p *ContextPool) WithFirstError() *ContextPool {
// WithCancelOnError configures the pool to cancel its context as soon as
// any task returns an error. By default, the pool's context is not
// canceled until the parent context is canceled.
//
// In this case, all errors returned from the pool after the frst will
// likely be context.Canceled - you may want to also use
// (*ContextPool).WithFirstError() to configure the pool to only return
// the first error.
func (p *ContextPool) WithCancelOnError() *ContextPool {
p.failFast = true
p.cancelOnError = true
return p
}

Expand Down

0 comments on commit 25effda

Please sign in to comment.