mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-23 09:00:20 +08:00
chore: add lock for batcherror (#3950)
This commit is contained in:
parent
ec41880476
commit
03b6e377d7
@ -8,8 +8,8 @@ import (
|
||||
type (
|
||||
// A BatchError is an error that can hold multiple errors.
|
||||
BatchError struct {
|
||||
mu sync.Mutex
|
||||
errs errorArray
|
||||
lock sync.Mutex
|
||||
}
|
||||
|
||||
errorArray []error
|
||||
@ -17,8 +17,8 @@ type (
|
||||
|
||||
// Add adds errs to be, nil errors are ignored.
|
||||
func (be *BatchError) Add(errs ...error) {
|
||||
be.mu.Lock()
|
||||
defer be.mu.Unlock()
|
||||
be.lock.Lock()
|
||||
defer be.lock.Unlock()
|
||||
|
||||
for _, err := range errs {
|
||||
if err != nil {
|
||||
@ -29,6 +29,9 @@ func (be *BatchError) Add(errs ...error) {
|
||||
|
||||
// Err returns an error that represents all errors.
|
||||
func (be *BatchError) Err() error {
|
||||
be.lock.Lock()
|
||||
defer be.lock.Unlock()
|
||||
|
||||
switch len(be.errs) {
|
||||
case 0:
|
||||
return nil
|
||||
@ -41,6 +44,9 @@ func (be *BatchError) Err() error {
|
||||
|
||||
// NotNil checks if any error inside.
|
||||
func (be *BatchError) NotNil() bool {
|
||||
be.lock.Lock()
|
||||
defer be.lock.Unlock()
|
||||
|
||||
return len(be.errs) > 0
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user