-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate retry package for re-use (#641)
- Loading branch information
1 parent
8683246
commit 692855e
Showing
6 changed files
with
99 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Util package for retrying, functions and tools for situations where we are retrying/polling for a change. | ||
|
||
Based around a 'RetryStrategy' interface that allows different approaches such as: | ||
- retry X times | ||
- retry for X seconds | ||
- retry for 2 minutes but backing off | ||
|
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,26 @@ | ||
package retry | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
func Do(fn func() error, retryStrat Strategy) error { | ||
// Reset tells the strategy we are about to start making attempts (it might reset attempts counter/record start time) | ||
retryStrat.Reset() | ||
|
||
for { | ||
// attempt to execute the function | ||
err := fn() | ||
if err == nil { | ||
// success | ||
return nil | ||
} | ||
|
||
if retryStrat.Done() { | ||
return fmt.Errorf("%s - latest error: %w", retryStrat.Summary(), err) | ||
} | ||
|
||
time.Sleep(retryStrat.NextRetryInterval()) | ||
} | ||
} |
8 changes: 4 additions & 4 deletions
8
go/obsclient/clientutil/retry_strategy.go → go/common/retry/retry_strategy.go
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,42 @@ | ||
package retry | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDoWithTimeoutStrategy_SuccessAfterRetries(t *testing.T) { | ||
var count int | ||
testFunc := func() error { | ||
count = count + 1 | ||
fmt.Printf("c: %d\n", count) | ||
if count < 3 { | ||
return fmt.Errorf("attempt number %d", count) | ||
} | ||
return nil | ||
} | ||
err := Do(testFunc, NewTimeoutStrategy(1*time.Second, 100*time.Millisecond)) | ||
if err != nil { | ||
assert.Fail(t, "Expected function to succeed before timeout but failed", err) | ||
} | ||
|
||
assert.Equal(t, 3, count, "expected function to be called 3 times before succeeding") | ||
} | ||
|
||
func TestDoWithTimeoutStrategy_FailAfterTimeout(t *testing.T) { | ||
var count int | ||
testFunc := func() error { | ||
count = count + 1 | ||
fmt.Printf("c: %d\n", count) | ||
return fmt.Errorf("attempt number %d", count) | ||
} | ||
err := Do(testFunc, NewTimeoutStrategy(600*time.Millisecond, 100*time.Millisecond)) | ||
if err == nil { | ||
assert.Fail(t, "expected failure from timeout but no err received") | ||
} | ||
|
||
assert.Greater(t, count, 5, "expected function to be called at least 5 times before timing out") | ||
} |
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