Skip to content

Commit 2c1a2d6

Browse files
authored
feat: add new WithPollOpts client option (#493)
- Improve consistency with the pattern used with `WithRetryOpts`, - Use the `PollOpts` struct, which is more future-proof than function arguments (properties can be added without breaking), - Update docs to use the latest `WithPollOpts` instead of the deprecated alternative. Since we already changed the way to configure the retry options, I think it is a good time to also change the pattern for the poll opts, even if this does not really bring a new feature.
1 parent af59ab8 commit 2c1a2d6

File tree

5 files changed

+34
-14
lines changed

5 files changed

+34
-14
lines changed

hcloud/action_waiter.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type ActionWaiter interface {
1616
var _ ActionWaiter = (*ActionClient)(nil)
1717

1818
// WaitForFunc waits until all actions are completed by polling the API at the interval
19-
// defined by [WithPollBackoffFunc]. An action is considered as complete when its status is
19+
// defined by [WithPollOpts]. An action is considered as complete when its status is
2020
// either [ActionStatusSuccess] or [ActionStatusError].
2121
//
2222
// The handleUpdate callback is called every time an action is updated.
@@ -98,7 +98,7 @@ func (c *ActionClient) WaitForFunc(ctx context.Context, handleUpdate func(update
9898
}
9999

100100
// WaitFor waits until all actions succeed by polling the API at the interval defined by
101-
// [WithPollBackoffFunc]. An action is considered as succeeded when its status is either
101+
// [WithPollOpts]. An action is considered as succeeded when its status is either
102102
// [ActionStatusSuccess].
103103
//
104104
// If a single action fails, the function will stop waiting and the error set in the

hcloud/action_watch.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
// timeout, use the [context.Context]. Once the method has stopped watching,
2222
// both returned channels are closed.
2323
//
24-
// WatchOverallProgress uses the [WithPollBackoffFunc] of the [Client] to wait
24+
// WatchOverallProgress uses the [WithPollOpts] of the [Client] to wait
2525
// until sending the next request.
2626
//
2727
// Deprecated: WatchOverallProgress is deprecated, use [WaitForFunc] instead.
@@ -86,7 +86,7 @@ func (c *ActionClient) WatchOverallProgress(ctx context.Context, actions []*Acti
8686
// timeout, use the [context.Context]. Once the method has stopped watching,
8787
// both returned channels are closed.
8888
//
89-
// WatchProgress uses the [WithPollBackoffFunc] of the [Client] to wait until
89+
// WatchProgress uses the [WithPollOpts] of the [Client] to wait until
9090
// sending the next request.
9191
//
9292
// Deprecated: WatchProgress is deprecated, use [WaitForFunc] instead.

hcloud/client.go

+22-4
Original file line numberDiff line numberDiff line change
@@ -140,22 +140,40 @@ func WithToken(token string) ClientOption {
140140
// polling from the API.
141141
//
142142
// Deprecated: Setting the poll interval is deprecated, you can now configure
143-
// [WithPollBackoffFunc] with a [ConstantBackoff] to get the same results. To
143+
// [WithPollOpts] with a [ConstantBackoff] to get the same results. To
144144
// migrate your code, replace your usage like this:
145145
//
146146
// // before
147147
// hcloud.WithPollInterval(2 * time.Second)
148148
// // now
149-
// hcloud.WithPollBackoffFunc(hcloud.ConstantBackoff(2 * time.Second))
149+
// hcloud.WithPollOpts(hcloud.PollOpts{
150+
// BackoffFunc: hcloud.ConstantBackoff(2 * time.Second),
151+
// })
150152
func WithPollInterval(pollInterval time.Duration) ClientOption {
151-
return WithPollBackoffFunc(ConstantBackoff(pollInterval))
153+
return WithPollOpts(PollOpts{
154+
BackoffFunc: ConstantBackoff(pollInterval),
155+
})
152156
}
153157

154158
// WithPollBackoffFunc configures a Client to use the specified backoff
155159
// function when polling from the API.
160+
//
161+
// Deprecated: WithPollBackoffFunc is deprecated, use [WithPollOpts] instead.
156162
func WithPollBackoffFunc(f BackoffFunc) ClientOption {
163+
return WithPollOpts(PollOpts{
164+
BackoffFunc: f,
165+
})
166+
}
167+
168+
// PollOpts defines the options used by [WithPollOpts].
169+
type PollOpts struct {
170+
BackoffFunc BackoffFunc
171+
}
172+
173+
// WithPollOpts configures a Client to use the specified options when polling from the API.
174+
func WithPollOpts(opts PollOpts) ClientOption {
157175
return func(client *Client) {
158-
client.pollBackoffFunc = f
176+
client.pollBackoffFunc = opts.BackoffFunc
159177
}
160178
}
161179

hcloud/client_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,12 @@ func newTestEnvWithServer(server *httptest.Server, mux *http.ServeMux) testEnv {
4242
WithEndpoint(server.URL),
4343
WithToken("token"),
4444
WithRetryOpts(RetryOpts{
45-
BackoffFunc: func(_ int) time.Duration { return 0 },
45+
BackoffFunc: ConstantBackoff(0),
4646
MaxRetries: 5,
4747
}),
48-
WithPollBackoffFunc(func(r int) time.Duration { return 0 }),
48+
WithPollOpts(PollOpts{
49+
BackoffFunc: ConstantBackoff(0),
50+
}),
4951
)
5052
return testEnv{
5153
Server: server,

hcloud/zz_action_client_iface.go

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)