-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #667 from forta-network/caner/forta-625-mention-au…
…to-update-config-in-the Improve auto-updates
- Loading branch information
Showing
18 changed files
with
165 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package nodeutils | ||
|
||
import "sync" | ||
|
||
// ErrorCounter checks incoming errors and tells if we are over | ||
// the max amount of consecutive errors. | ||
type ErrorCounter struct { | ||
max uint | ||
isCriticalError func(error) bool | ||
count uint | ||
mu sync.Mutex | ||
} | ||
|
||
// NewErrorCounter creates a new error counter. | ||
func NewErrorCounter(max uint, isCriticalError func(error) bool) *ErrorCounter { | ||
return &ErrorCounter{ | ||
max: max, | ||
isCriticalError: isCriticalError, | ||
} | ||
} | ||
|
||
func (ec *ErrorCounter) TooManyErrs(err error) bool { | ||
ec.mu.Lock() | ||
defer ec.mu.Unlock() | ||
if err == nil || !ec.isCriticalError(err) { | ||
ec.count = 0 // reset if other errors or no errors | ||
return false | ||
} | ||
ec.count++ | ||
return ec.count >= ec.max | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package nodeutils | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestErrorCounter(t *testing.T) { | ||
r := require.New(t) | ||
|
||
// max two consecutive non-nil errors means a red flag | ||
counter := NewErrorCounter(2, func(err error) bool { | ||
return err != nil | ||
}) | ||
|
||
r.False(counter.TooManyErrs(errors.New("error 1"))) | ||
r.False(counter.TooManyErrs(nil)) // no error - back to zero | ||
r.False(counter.TooManyErrs(errors.New("error 1"))) | ||
r.True(counter.TooManyErrs(errors.New("error 2"))) // hit the limit - we're done | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.