From 2db95753e2c826aeafa3bd9b864e95efd89ace7f Mon Sep 17 00:00:00 2001 From: "Jonas L." Date: Mon, 22 Jul 2024 15:33:49 +0200 Subject: [PATCH] feat: allow configuring retry options (#488) This lets the user configure the retry options, with a custom back off function and the max number of retries. --- hcloud/client.go | 17 +++++++++++++++++ hcloud/client_test.go | 5 ++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/hcloud/client.go b/hcloud/client.go index 12e4f064..ae9fcb34 100644 --- a/hcloud/client.go +++ b/hcloud/client.go @@ -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. diff --git a/hcloud/client_test.go b/hcloud/client_test.go index 5229290c..f5dbd867 100644 --- a/hcloud/client_test.go +++ b/hcloud/client_test.go @@ -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{