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

Add thread-safe multi error implementation #2

Open
wants to merge 4 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
15 changes: 7 additions & 8 deletions merrors/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@
//
// Example 3:
//
// func CloseAll(closers []io.Closer) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this right indent?

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

// errs := merrors.New()
// for _ , c := range closers {
// errs.Add(c.Close())
// }
// return errs.Err()
// }
//
// func CloseAll(closers []io.Closer) error {
// errs := merrors.New()
// for _ , c := range closers {
// errs.Add(c.Close())
// }
// return errs.Err()
// }
package merrors
38 changes: 35 additions & 3 deletions merrors/merrors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
stderrors "errors"
"fmt"
"io"
"sync"
)

// NilOrMultiError type allows combining multiple errors into one.
Expand Down Expand Up @@ -38,11 +39,42 @@ func (e *NilOrMultiError) Add(errs ...error) {
}

// Err returns the error list as an Error (also implements error) or nil if it is empty.
func (e NilOrMultiError) Err() Error {
if len(e.errs) == 0 {
func (e *NilOrMultiError) Err() Error {
if e == nil || len(e.errs) == 0 {
return nil
}
return multiError(e)

return multiError(*e)
}

// NilOrMultiSyncError is a thread-safe implementation of NilOrMultiError.
// It allows combining multiple errors into one.
type NilOrMultiSyncError struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we don't want to make the normal implementation concurrency safe 🤔 I don't think it introduces worth noting overhead... The only risk is when somebody would like to have different synchronization techniques that works for them, then they would need to reimplement all.. but sounds like rare use case.

Otherwise another option is to have different package..

I think for me it would make more sense to have ONLY gorotoutine safe implementation here.

Copy link
Author

Choose a reason for hiding this comment

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

I think it depends on what use cases there are out in the wild, but I'd agree having one implementation and documenting that it's concurrency safe so it's clear for the users is the way to go. I'll prepare the changes.

mtx sync.Mutex
multiErr NilOrMultiError
}

// NewSync returns NilOrMultiSyncError with provided errors added if not nil.
func NewSync(errs ...error) *NilOrMultiSyncError {
sm := &NilOrMultiSyncError{}
sm.Add(errs...)
return sm
}

// Add adds single or many errors to the error list. It has same behavior as NilOrMultiError.
func (e *NilOrMultiSyncError) Add(errs ...error) {
e.mtx.Lock()
defer e.mtx.Unlock()

e.multiErr.Add(errs...)
}

// Err returns the error list as an Error (also implements error) or nil if it is empty.
func (e *NilOrMultiSyncError) Err() Error {
e.mtx.Lock()
defer e.mtx.Unlock()

return e.multiErr.Err()
}

// Error is extended error interface that allows to use returned read-only multi error in more advanced ways.
Expand Down
20 changes: 20 additions & 0 deletions merrors/merrors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,26 @@ func TestMultiError(t *testing.T) {
}())
}

func TestMultiSyncError(t *testing.T) {
err := errors.New("test1")

// Run tests with race detector to detect data races in NilOrMultiSyncError.
e := merrors.NewSync(err)
for i := 0; i < 10; i++ {
go func() {
e.Add(err)
}()
}

testutil.NotOk(t, func() error {
return e.Err()
}())
Comment on lines +77 to +79

Choose a reason for hiding this comment

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

Hm, can't this be

Suggested change
testutil.NotOk(t, func() error {
return e.Err()
}())
testutil.NotOk(t, e.Err())


testutil.Ok(t, func() error {
return merrors.NewSync(nil, nil, nil).Err()
}())
}

func TestMultiError_Error(t *testing.T) {
err := errors.New("test1")

Expand Down