Skip to content

Commit 6205076

Browse files
authored
feat: add jitter in the client default retry exponential backoff (#492)
Enable jitter in the client default retry exponential backoff function.
1 parent cf9fdaf commit 6205076

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

hcloud/client.go

+13-6
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,19 @@ func WithInstrumentation(registry prometheus.Registerer) ClientOption {
219219
// NewClient creates a new client.
220220
func NewClient(options ...ClientOption) *Client {
221221
client := &Client{
222-
endpoint: Endpoint,
223-
tokenValid: true,
224-
httpClient: &http.Client{},
225-
retryBackoffFunc: ExponentialBackoff(2, time.Second),
226-
retryMaxRetries: 5,
227-
pollBackoffFunc: ConstantBackoff(500 * time.Millisecond),
222+
endpoint: Endpoint,
223+
tokenValid: true,
224+
httpClient: &http.Client{},
225+
226+
retryBackoffFunc: ExponentialBackoffWithOpts(ExponentialBackoffOpts{
227+
Base: time.Second,
228+
Multiplier: 2,
229+
Cap: time.Minute,
230+
Jitter: true,
231+
}),
232+
retryMaxRetries: 5,
233+
234+
pollBackoffFunc: ConstantBackoff(500 * time.Millisecond),
228235
}
229236

230237
for _, option := range options {

hcloud/hcloud.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ The Hetzner Cloud API reference is available at https://docs.hetzner.cloud.
55
66
# Retry mechanism
77
8-
The [Client.Do] method will retry failed requests that match certain criteria. The default
9-
retry interval is defined by an exponential backoff algorithm truncated to 60s. The
10-
default maximal number of retries is 5.
8+
The [Client.Do] method will retry failed requests that match certain criteria. The
9+
default retry interval is defined by an exponential backoff algorithm truncated to 60s
10+
with jitter. The default maximal number of retries is 5.
1111
1212
The following rules defines when a request can be retried:
1313

0 commit comments

Comments
 (0)