Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On error callback #89

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pool/context_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ func (p *ContextPool) WithMaxGoroutines(n int) *ContextPool {
return p
}

// WithErrorCallback configures the pool to call f everytime a task returns error.
func (p *ContextPool) WithErrorCallback(f func(err error)) *ContextPool {
Comment on lines +78 to +79
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we expose an equivalent on ResultErrorPool and ResultContextPool?

p.panicIfInitialized()
p.errorPool.WithErrorCallback(f)
return p
}

func (p *ContextPool) panicIfInitialized() {
p.errorPool.panicIfInitialized()
}
13 changes: 13 additions & 0 deletions pool/error_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type ErrorPool struct {

mu sync.Mutex
errs error

errorCallBack func(err error)
}

// Go submits a task to the pool. If all goroutines in the pool
Expand Down Expand Up @@ -68,6 +70,13 @@ func (p *ErrorPool) WithMaxGoroutines(n int) *ErrorPool {
return p
}

// WithErrorCallback configures the pool to call f everytime a task returns error.
func (p *ErrorPool) WithErrorCallback(f func(err error)) *ErrorPool {
p.panicIfInitialized()
p.errorCallBack = f
return p
}

// deref is a helper that creates a shallow copy of the pool with the same
// settings. We don't want to just dereference the pointer because that makes
// the copylock lint angry.
Expand All @@ -93,5 +102,9 @@ func (p *ErrorPool) addErr(err error) {
p.errs = errors.Append(p.errs, err)
}
p.mu.Unlock()

if p.errorCallBack != nil {
p.errorCallBack(err)
}
Comment on lines +106 to +108
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be behind the mutex? It seems useful to be able to guarantee that the callback will never be called concurrently.

Copy link
Author

@samix73 samix73 Feb 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have also thought about that but if the callback is doing something time intensive it would block the whole pool.
I don't really have a strong opinion about this, it's a matter communication to the user so they know how it behaves either way.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, makes sense. I also don't feel very strongly. I think it's fine the way it is, but we should document that the callback must be safe to call from multiple goroutines.

}
}
36 changes: 36 additions & 0 deletions pool/error_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,40 @@ func TestErrorPool(t *testing.T) {
})
}
})

t.Run("on-error callback", func(t *testing.T) {
t.Parallel()

err1 := errors.New("err1")
err2 := errors.New("err2")

calledForErr1 := false
calledForErr2 := false

g := New().WithErrors().WithErrorCallback(func(err error) {
if errors.Is(err, err1) {
calledForErr1 = true
} else if errors.Is(err, err2) {
calledForErr2 = true
} else {
t.Fatal("callback called with invalid error")
}
})
g.Go(func() error {
return err1
})
g.Go(func() error {
return err2
})

_ = g.Wait()

if !calledForErr1 {
t.Fatal("call back is not called for err1")
}

if !calledForErr2 {
t.Fatal("call back is not called for err2")
}
})
}