From 03b6e377d75b423f946f95ee1f20b29fe086e14a Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Sat, 2 Mar 2024 00:59:15 +0800 Subject: [PATCH] chore: add lock for batcherror (#3950) --- core/errorx/batcherror.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/core/errorx/batcherror.go b/core/errorx/batcherror.go index b63867cb..e0fcbb27 100644 --- a/core/errorx/batcherror.go +++ b/core/errorx/batcherror.go @@ -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 }