From 16037201f7348b88cd7b41f19361faf1194a035c Mon Sep 17 00:00:00 2001 From: Oded Valtzer Date: Fri, 18 Jan 2019 19:14:11 +0200 Subject: [PATCH 1/8] add last error option and implementation including tests --- options.go | 19 ++++++++++++++----- retry.go | 16 ++++++++++++++++ retry_test.go | 15 +++++++++++++++ 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/options.go b/options.go index 1972af7..1617201 100644 --- a/options.go +++ b/options.go @@ -14,16 +14,25 @@ type OnRetryFunc func(n uint, err error) type DelayTypeFunc func(n uint, config *config) time.Duration type config struct { - attempts uint - delay time.Duration - onRetry OnRetryFunc - retryIf RetryIfFunc - delayType DelayTypeFunc + attempts uint + delay time.Duration + onRetry OnRetryFunc + retryIf RetryIfFunc + delayType DelayTypeFunc + lastErrorOnly bool } // Option represents an option for retry. type Option func(*config) +// return the direct last error that came from the retried function +// default is false (return wrapped error with everything +func LastErrorOnly(lastErrorOnly bool) Option { + return func(c *config) { + c.lastErrorOnly = lastErrorOnly + } +} + // Attempts set count of retry // default is 10 func Attempts(attempts uint) Option { diff --git a/retry.go b/retry.go index 7d784ca..454b6fe 100644 --- a/retry.go +++ b/retry.go @@ -83,6 +83,7 @@ func Do(retryableFunc RetryableFunc, opts ...Option) error { onRetry: func(n uint, err error) {}, retryIf: func(err error) bool { return true }, delayType: BackOffDelay, + lastErrorOnly: false, } //apply opts @@ -117,6 +118,9 @@ func Do(retryableFunc RetryableFunc, opts ...Option) error { n++ } + if config.lastErrorOnly { + return errorLog.LastError() + } return errorLog } @@ -153,3 +157,15 @@ func lenWithoutNil(e Error) (count int) { func (e Error) WrappedErrors() []error { return e } + +func (e Error) LastError() error { + var lastErr error + for _, err := range e.WrappedErrors() { + if err != nil{ + lastErr = err + } else { + return lastErr + } + } + return lastErr +} \ No newline at end of file diff --git a/retry_test.go b/retry_test.go index 0a0407e..59dd160 100644 --- a/retry_test.go +++ b/retry_test.go @@ -6,6 +6,7 @@ import ( "time" "github.com/stretchr/testify/assert" + "fmt" ) func TestDoAllFailed(t *testing.T) { @@ -92,3 +93,17 @@ func TestFixedSleep(t *testing.T) { assert.Error(t, err) assert.True(t, dur < 500*time.Millisecond, "3 times default retry is shorter then 500ms") } + +func TestLastErrorOnly(t *testing.T) { + var retrySum uint + err := Do( + func() error { return errors.New(fmt.Sprintf("%d", retrySum)) }, + OnRetry(func(n uint, err error) { retrySum += 1 }), + Delay(time.Nanosecond), + LastErrorOnly(true), + ) + if err.Error() != "9"{ + fmt.Println(err.Error()) + t.Fail() + } +} \ No newline at end of file From 9b6323767ac64a32dc000c4e6a2755be5ebcfe7d Mon Sep 17 00:00:00 2001 From: Oded Valtzer Date: Fri, 18 Jan 2019 19:16:23 +0200 Subject: [PATCH 2/8] new line and comment fix --- options.go | 2 +- retry.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/options.go b/options.go index 1617201..51a56fa 100644 --- a/options.go +++ b/options.go @@ -26,7 +26,7 @@ type config struct { type Option func(*config) // return the direct last error that came from the retried function -// default is false (return wrapped error with everything +// default is false (return wrapped errors with everything) func LastErrorOnly(lastErrorOnly bool) Option { return func(c *config) { c.lastErrorOnly = lastErrorOnly diff --git a/retry.go b/retry.go index 454b6fe..155fe95 100644 --- a/retry.go +++ b/retry.go @@ -168,4 +168,4 @@ func (e Error) LastError() error { } } return lastErr -} \ No newline at end of file +} From 206298c4d2a024fe8a8403a55c5c97d1b6567c97 Mon Sep 17 00:00:00 2001 From: Oded Valtzer Date: Fri, 18 Jan 2019 19:17:33 +0200 Subject: [PATCH 3/8] new line fix --- retry_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/retry_test.go b/retry_test.go index 59dd160..ba714af 100644 --- a/retry_test.go +++ b/retry_test.go @@ -106,4 +106,4 @@ func TestLastErrorOnly(t *testing.T) { fmt.Println(err.Error()) t.Fail() } -} \ No newline at end of file +} From 2de03ab7f57866090e860f174f969c31bbd071fc Mon Sep 17 00:00:00 2001 From: Oded Valtzer Date: Fri, 18 Jan 2019 19:32:04 +0200 Subject: [PATCH 4/8] fix lint --- retry.go | 14 +++++++------- retry_test.go | 5 +++-- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/retry.go b/retry.go index 155fe95..9ad339c 100644 --- a/retry.go +++ b/retry.go @@ -78,11 +78,11 @@ func Do(retryableFunc RetryableFunc, opts ...Option) error { //default config := &config{ - attempts: 10, - delay: 100 * time.Millisecond, - onRetry: func(n uint, err error) {}, - retryIf: func(err error) bool { return true }, - delayType: BackOffDelay, + attempts: 10, + delay: 100 * time.Millisecond, + onRetry: func(n uint, err error) {}, + retryIf: func(err error) bool { return true }, + delayType: BackOffDelay, lastErrorOnly: false, } @@ -119,7 +119,7 @@ func Do(retryableFunc RetryableFunc, opts ...Option) error { } if config.lastErrorOnly { - return errorLog.LastError() + return errorLog.LastError() } return errorLog } @@ -161,7 +161,7 @@ func (e Error) WrappedErrors() []error { func (e Error) LastError() error { var lastErr error for _, err := range e.WrappedErrors() { - if err != nil{ + if err != nil { lastErr = err } else { return lastErr diff --git a/retry_test.go b/retry_test.go index ba714af..5eaecc7 100644 --- a/retry_test.go +++ b/retry_test.go @@ -5,8 +5,9 @@ import ( "testing" "time" - "github.com/stretchr/testify/assert" "fmt" + + "github.com/stretchr/testify/assert" ) func TestDoAllFailed(t *testing.T) { @@ -102,7 +103,7 @@ func TestLastErrorOnly(t *testing.T) { Delay(time.Nanosecond), LastErrorOnly(true), ) - if err.Error() != "9"{ + if err.Error() != "9" { fmt.Println(err.Error()) t.Fail() } From 8617d3928c2d5b7627e43c4a7ade4af822156a0d Mon Sep 17 00:00:00 2001 From: Oded Valtzer Date: Fri, 18 Jan 2019 19:40:34 +0200 Subject: [PATCH 5/8] remove redundant print --- retry_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/retry_test.go b/retry_test.go index 5eaecc7..ee56aa2 100644 --- a/retry_test.go +++ b/retry_test.go @@ -104,7 +104,6 @@ func TestLastErrorOnly(t *testing.T) { LastErrorOnly(true), ) if err.Error() != "9" { - fmt.Println(err.Error()) t.Fail() } } From 798c0f327f3725a9070bfd530504e1bfc25ac6ad Mon Sep 17 00:00:00 2001 From: Oded Valtzer Date: Sat, 19 Jan 2019 11:29:01 +0200 Subject: [PATCH 6/8] use commit counter for last error instead of loop --- retry.go | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/retry.go b/retry.go index 9ad339c..361f265 100644 --- a/retry.go +++ b/retry.go @@ -119,7 +119,7 @@ func Do(retryableFunc RetryableFunc, opts ...Option) error { } if config.lastErrorOnly { - return errorLog.LastError() + return errorLog[n] } return errorLog } @@ -158,14 +158,3 @@ func (e Error) WrappedErrors() []error { return e } -func (e Error) LastError() error { - var lastErr error - for _, err := range e.WrappedErrors() { - if err != nil { - lastErr = err - } else { - return lastErr - } - } - return lastErr -} From 6720c5b41c9b0964f0f8089cfbd38978f13670cf Mon Sep 17 00:00:00 2001 From: Oded Valtzer Date: Sat, 19 Jan 2019 12:32:05 +0200 Subject: [PATCH 7/8] lint --- retry.go | 1 - 1 file changed, 1 deletion(-) diff --git a/retry.go b/retry.go index 361f265..e8d6ba0 100644 --- a/retry.go +++ b/retry.go @@ -157,4 +157,3 @@ func lenWithoutNil(e Error) (count int) { func (e Error) WrappedErrors() []error { return e } - From 13bfda1721cfd17e065ecf8736ae93cf74117a1f Mon Sep 17 00:00:00 2001 From: Oded Valtzer Date: Sat, 19 Jan 2019 16:35:10 +0200 Subject: [PATCH 8/8] assert convention --- retry_test.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/retry_test.go b/retry_test.go index ee56aa2..9408366 100644 --- a/retry_test.go +++ b/retry_test.go @@ -103,7 +103,5 @@ func TestLastErrorOnly(t *testing.T) { Delay(time.Nanosecond), LastErrorOnly(true), ) - if err.Error() != "9" { - t.Fail() - } + assert.Equal(t, "9", err.Error()) }