-
-
Notifications
You must be signed in to change notification settings - Fork 5
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import ( | |
stderrors "errors" | ||
"fmt" | ||
"io" | ||
"sync" | ||
) | ||
|
||
// NilOrMultiError type allows combining multiple errors into one. | ||
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, can't this be
Suggested change
|
||||||||||
|
||||||||||
testutil.Ok(t, func() error { | ||||||||||
return merrors.NewSync(nil, nil, nil).Err() | ||||||||||
}()) | ||||||||||
} | ||||||||||
|
||||||||||
func TestMultiError_Error(t *testing.T) { | ||||||||||
err := errors.New("test1") | ||||||||||
|
||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this right indent?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/efficientgo/core/pull/3/files#r1009394420
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/efficientgo/core/pull/3/files#r1009394420