Skip to content

Commit

Permalink
feat: allow configuring retry options (#488)
Browse files Browse the repository at this point in the history
This lets the user configure the retry options, with a custom back off
function and the max number of retries.
  • Loading branch information
jooola committed Jul 22, 2024
1 parent f4ad6bc commit 2db9575
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
17 changes: 17 additions & 0 deletions hcloud/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,29 @@ func WithPollBackoffFunc(f BackoffFunc) ClientOption {

// WithBackoffFunc configures a Client to use the specified backoff function.
// The backoff function is used for retrying HTTP requests.
//
// Deprecated: WithBackoffFunc is deprecated, use [WithRetryOpts] instead.
func WithBackoffFunc(f BackoffFunc) ClientOption {
return func(client *Client) {
client.retryBackoffFunc = f
}
}

// RetryOpts defines the options used by [WithRetryOpts].
type RetryOpts struct {
BackoffFunc BackoffFunc
MaxRetries int
}

// WithRetryOpts configures a Client to use the specified options when retrying API
// requests.
func WithRetryOpts(opts RetryOpts) ClientOption {
return func(client *Client) {
client.retryBackoffFunc = opts.BackoffFunc
client.retryMaxRetries = opts.MaxRetries
}
}

// WithApplication configures a Client with the given application name and
// application version. The version may be blank. Programs are encouraged
// to at least set an application name.
Expand Down
5 changes: 4 additions & 1 deletion hcloud/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ func newTestEnvWithServer(server *httptest.Server, mux *http.ServeMux) testEnv {
client := NewClient(
WithEndpoint(server.URL),
WithToken("token"),
WithBackoffFunc(func(_ int) time.Duration { return 0 }),
WithRetryOpts(RetryOpts{
BackoffFunc: func(_ int) time.Duration { return 0 },
MaxRetries: 5,
}),
WithPollBackoffFunc(func(r int) time.Duration { return 0 }),
)
return testEnv{
Expand Down

0 comments on commit 2db9575

Please sign in to comment.