-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(exp): add retry package #479
Draft
jooola
wants to merge
1
commit into
hetznercloud:main
Choose a base branch
from
jooola:retryutils
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,42 @@ | ||
package retryutils | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/hetznercloud/hcloud-go/v2/hcloud" | ||
) | ||
|
||
// TODO: generate the opts from the [hcloud.Client]. | ||
type Opts struct { | ||
Backoff hcloud.BackoffFunc | ||
MaxRetries int | ||
Policy func(err error) bool | ||
} | ||
|
||
func Retry[T any](opts Opts, request func() (T, *hcloud.Response, error)) (T, *hcloud.Response, error) { | ||
retries := 0 | ||
for { | ||
result, resp, err := request() | ||
if err != nil { | ||
if opts.Policy(err) && retries < opts.MaxRetries { | ||
select { | ||
case <-resp.Request.Context().Done(): | ||
break | ||
case <-time.After(opts.Backoff(retries)): | ||
retries++ | ||
continue | ||
} | ||
} | ||
} | ||
return result, resp, err | ||
} | ||
} | ||
|
||
func RetryNoResult(opts Opts, request func() (*hcloud.Response, error)) (*hcloud.Response, error) { | ||
_, resp, err := Retry(opts, func() (any, *hcloud.Response, error) { | ||
resp, err := request() | ||
return nil, resp, err | ||
}) | ||
|
||
return resp, 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,143 @@ | ||
package retryutils | ||
|
||
import ( | ||
"context" | ||
"net/http/httptest" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/hetznercloud/hcloud-go/v2/hcloud" | ||
"github.com/hetznercloud/hcloud-go/v2/hcloud/exp/mockutils" | ||
Check failure on line 14 in hcloud/exp/retryutils/retry_test.go GitHub Actions / test (1.21)
|
||
) | ||
|
||
func FakeBackoff(counter *int64) hcloud.BackoffFunc { | ||
return func(_ int) time.Duration { | ||
atomic.AddInt64(counter, 1) | ||
return 0 | ||
} | ||
} | ||
|
||
func TestRetry(t *testing.T) { | ||
sshKey := &hcloud.SSHKey{ID: 123} | ||
|
||
updateResponse := mockutils.Request{ | ||
Method: "PUT", Path: "/ssh_keys/123", | ||
Status: 200, | ||
JSONRaw: `{ "ssh_key": { "id": 123 }}`, | ||
} | ||
updateLockedResponse := mockutils.Request{ | ||
Method: "PUT", Path: "/ssh_keys/123", | ||
Status: 423, | ||
JSONRaw: `{ "error": { "code": "locked", "message": "Resource is locked" }}`, | ||
} | ||
deleteResponse := mockutils.Request{ | ||
Method: "DELETE", Path: "/ssh_keys/123", | ||
Status: 204, | ||
} | ||
deleteLockedResponse := mockutils.Request{ | ||
Method: "DELETE", Path: "/ssh_keys/123", | ||
Status: 423, | ||
JSONRaw: `{ "error": { "code": "locked", "message": "Resource is locked" }}`, | ||
} | ||
|
||
testCases := []struct { | ||
name string | ||
requests []mockutils.Request | ||
run func(t *testing.T, client *hcloud.Client) | ||
}{ | ||
{ | ||
name: "happy with 3 return values", | ||
requests: []mockutils.Request{ | ||
updateLockedResponse, | ||
updateLockedResponse, | ||
updateResponse, | ||
}, | ||
run: func(t *testing.T, client *hcloud.Client) { | ||
var retryCount int64 | ||
retryOpts := Opts{ | ||
Backoff: FakeBackoff(&retryCount), | ||
MaxRetries: 3, | ||
Policy: func(err error) bool { | ||
return hcloud.IsError(err, hcloud.ErrorCodeLocked) | ||
}, | ||
} | ||
|
||
result, resp, err := Retry(retryOpts, func() (*hcloud.SSHKey, *hcloud.Response, error) { | ||
return client.SSHKey.Update(context.Background(), sshKey, hcloud.SSHKeyUpdateOpts{}) | ||
}) | ||
require.NoError(t, err) | ||
assert.NotNil(t, resp) | ||
assert.Equal(t, int64(123), result.ID) | ||
|
||
assert.Equal(t, int64(2), retryCount) | ||
}, | ||
}, | ||
{ | ||
name: "happy with 2 return values", | ||
requests: []mockutils.Request{ | ||
deleteLockedResponse, | ||
deleteLockedResponse, | ||
deleteResponse, | ||
}, | ||
run: func(t *testing.T, client *hcloud.Client) { | ||
var retryCount int64 | ||
retryOpts := Opts{ | ||
Backoff: FakeBackoff(&retryCount), | ||
MaxRetries: 3, | ||
Policy: func(err error) bool { | ||
return hcloud.IsError(err, hcloud.ErrorCodeLocked) | ||
}, | ||
} | ||
|
||
resp, err := RetryNoResult(retryOpts, func() (*hcloud.Response, error) { | ||
return client.SSHKey.Delete(context.Background(), sshKey) | ||
}) | ||
require.NoError(t, err) | ||
assert.NotNil(t, resp) | ||
|
||
assert.Equal(t, int64(2), retryCount) | ||
}, | ||
}, | ||
{ | ||
name: "fail with locked error", | ||
requests: []mockutils.Request{ | ||
deleteLockedResponse, | ||
deleteLockedResponse, | ||
deleteLockedResponse, | ||
deleteLockedResponse, | ||
}, | ||
run: func(t *testing.T, client *hcloud.Client) { | ||
var retryCount int64 | ||
retryOpts := Opts{ | ||
Backoff: FakeBackoff(&retryCount), | ||
MaxRetries: 3, | ||
Policy: func(err error) bool { | ||
return hcloud.IsError(err, hcloud.ErrorCodeLocked) | ||
}, | ||
} | ||
|
||
resp, err := RetryNoResult(retryOpts, func() (*hcloud.Response, error) { | ||
return client.SSHKey.Delete(context.Background(), sshKey) | ||
}) | ||
require.EqualError(t, err, "Resource is locked (locked)") | ||
assert.NotNil(t, resp) | ||
|
||
assert.Equal(t, int64(3), retryCount) | ||
}, | ||
}, | ||
} | ||
for _, testCase := range testCases { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
server := httptest.NewServer(mockutils.Handler(t, testCase.requests)) | ||
defer server.Close() | ||
|
||
client := hcloud.NewClient(hcloud.WithEndpoint(server.URL)) | ||
|
||
testCase.run(t, client) | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add examples for these to showcase how one can use them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haha, I would love to, but:
Let's wait until we find a use case that we can implement and test against.