Skip to content

Commit

Permalink
make MErrror thread safe
Browse files Browse the repository at this point in the history
  • Loading branch information
wiggin77 committed Nov 27, 2020
1 parent 83dec6b commit 77a531f
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 4 deletions.
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
module github.com/wiggin77/merror

go 1.15

require github.com/stretchr/testify v1.6.1
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
41 changes: 37 additions & 4 deletions merror.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package merror

import "sync"

// MError represents zero or more errors that can be
// accumulated via the `Append` method.
type MError struct {
cap int
cap int

mux sync.RWMutex
errors []error
overflow int
formatter FormatterFunc
Expand Down Expand Up @@ -34,33 +38,52 @@ func (me *MError) Append(err error) {
if err == nil {
return
}

me.mux.Lock()
defer me.mux.Unlock()

if me.cap > 0 && len(me.errors) >= me.cap {
me.overflow++
} else {
me.errors = append(me.errors, err)
}
}

// Errors returns an array of the `error` instances that have been
// Errors returns a slice of the `error` instances that have been
// appended to this `MError`.
func (me *MError) Errors() []error {
return me.errors
me.mux.RLock()
defer me.mux.RUnlock()

errs := make([]error, len(me.errors))
copy(errs, me.errors)

return errs
}

// Len returns the number of errors that have been appended.
func (me *MError) Len() int {
me.mux.RLock()
defer me.mux.RUnlock()

return len(me.errors)
}

// Overflow returns the number of errors that have been truncated
// because maximum capacity was exceeded.
func (me *MError) Overflow() int {
me.mux.RLock()
defer me.mux.RUnlock()

return me.overflow
}

// SetFormatter sets the `FormatterFunc` to be used when `Error` is
// called. The previous `FormatterFunc` is returned.
func (me *MError) SetFormatter(f FormatterFunc) (old FormatterFunc) {
me.mux.Lock()
defer me.mux.Unlock()

old = me.formatter
me.formatter = f
return
Expand All @@ -69,7 +92,14 @@ func (me *MError) SetFormatter(f FormatterFunc) (old FormatterFunc) {
// ErrorOrNil returns nil if this `MError` contains no errors,
// otherwise this `MError` is returned.
func (me *MError) ErrorOrNil() error {
if me == nil || len(me.errors) == 0 {
if me == nil {
return nil
}

me.mux.RLock()
defer me.mux.RUnlock()

if len(me.errors) == 0 {
return nil
}
return me
Expand All @@ -79,6 +109,9 @@ func (me *MError) ErrorOrNil() error {
// The output format depends on the `Formatter` set for this
// merror instance, or the global formatter if none set.
func (me *MError) Error() string {
me.mux.RLock()
defer me.mux.RUnlock()

f := me.formatter
if f == nil {
f = GlobalFormatter
Expand Down
33 changes: 33 additions & 0 deletions merror_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package merror

import (
"fmt"
"sync"
"testing"

"github.com/stretchr/testify/assert"
)

const (
NumThreads = 25
NumErrors = 5
)

func TestRace(t *testing.T) {
merr := New()
wg := &sync.WaitGroup{}
wg.Add(NumThreads)

for i := 0; i < NumThreads; i++ {
go func(id int) {
defer wg.Done()
for j := 0; j < NumErrors; j++ {
merr.Append(fmt.Errorf("Goroutine: %d ErrorNum: %d", id, j))
}
}(i)
}

wg.Wait()

assert.Equal(t, NumThreads*NumErrors, len(merr.Errors()))
}

0 comments on commit 77a531f

Please sign in to comment.