-
Notifications
You must be signed in to change notification settings - Fork 0
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 #26 from shogo82148/introduce-do-value
introduce DoValue
- Loading branch information
Showing
6 changed files
with
150 additions
and
5 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 |
---|---|---|
|
@@ -8,6 +8,7 @@ jobs: | |
name: Test | ||
runs-on: ubuntu-latest | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
go: | ||
- "stable" | ||
|
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,45 @@ | ||
package retry | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
) | ||
|
||
// DoValue executes f with retrying policy. | ||
// It is a shorthand of Policy.Start and Retrier.Continue. | ||
// If f returns an error, retry to execute f until f returns nil error. | ||
// If the error implements interface{ Temporary() bool } and Temporary() returns false, | ||
// DoValue doesn't retry and returns the error. | ||
func DoValue[T any](ctx context.Context, policy *Policy, f func() (T, error)) (T, error) { | ||
var zero T | ||
var err error | ||
var target *temporary | ||
|
||
retrier := policy.Start(ctx) | ||
for retrier.Continue() { | ||
var v T | ||
v, err = f() | ||
if err == nil { | ||
return v, nil | ||
} | ||
|
||
// short cut for calling isPermanent and Unwrap | ||
if err, ok := err.(*permanentError); ok { | ||
return zero, err.error | ||
} | ||
|
||
if target == nil { | ||
// lazy allocation of target | ||
target = new(temporary) | ||
} | ||
if errors.As(err, target) { | ||
if !(*target).Temporary() { | ||
return zero, err | ||
} | ||
} | ||
} | ||
if err := retrier.err; err != nil { | ||
return zero, err | ||
} | ||
return zero, err | ||
} |
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,40 @@ | ||
package retry | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestDoValue_Success(t *testing.T) { | ||
policy := &Policy{ | ||
MaxCount: -1, | ||
} | ||
|
||
var count int | ||
v, err := DoValue(context.Background(), policy, func() (int, error) { | ||
count++ | ||
if count < 3 { | ||
return 0, fmt.Errorf("error %d", count) | ||
} | ||
return 42, nil | ||
}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if v != 42 { | ||
t.Errorf("want %d, got %d", 42, v) | ||
} | ||
} | ||
|
||
func TestDoValue_MarkPermanent(t *testing.T) { | ||
permanentErr := errors.New("permanent error") | ||
policy := &Policy{} | ||
_, err := DoValue(context.Background(), policy, func() (int, error) { | ||
return 0, MarkPermanent(permanentErr) | ||
}) | ||
if err != permanentErr { | ||
t.Errorf("want error is %#v, got %#v", err, permanentErr) | ||
} | ||
} |
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