Skip to content

Commit 0d5879f

Browse files
[CLIENT] move BackOff retry parameters to provider client level (#578)
[CLIENT] move BackOff retry parameters to provider client level Move BackOff parameters to client level to make it more configurable for terraform provider. === RUN TestThrottlingSgs --- PASS: TestThrottlingSgs (122.57s) PASS === RUN TestTooManyRequestsRetry --- PASS: TestTooManyRequestsRetry (20.01s) === RUN TestTooManyRequestsRetry/429 --- PASS: TestTooManyRequestsRetry/429 (20.01s) PASS Process finished with the exit code 0 Reviewed-by: Aloento Reviewed-by: Artem Lifshits
1 parent 8bfef1b commit 0d5879f

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

acceptance/openstack/client_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,18 +137,21 @@ func TestGatewayRetry(t *testing.T) {
137137
}
138138

139139
func TestTooManyRequestsRetry(t *testing.T) {
140-
t.Skip("please run only manually, long test")
141140
th.SetupHTTP()
142141
defer th.TeardownHTTP()
143142

144143
code := http.StatusTooManyRequests
145-
146-
failHandler := &failHandler{ExpectedFailures: 5, ErrorCode: code}
144+
retries := 2
145+
timeout := 20 * time.Second
146+
failHandler := &failHandler{ExpectedFailures: 1, ErrorCode: code}
147147
th.Mux.Handle(fmt.Sprintf("/%d", code), failHandler)
148148

149149
codeURL := fmt.Sprintf("%d", code)
150150
t.Run(codeURL, func(sub *testing.T) {
151151
client := fake.ServiceClient()
152+
client.MaxBackoffRetries = &retries
153+
client.BackoffRetryTimeout = &timeout
154+
152155
_, err := client.Delete(client.ServiceURL(codeURL), &golangsdk.RequestOpts{
153156
OkCodes: []int{200},
154157
})

acceptance/openstack/networking/v2/sgs/sg_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,10 @@ func TestThrottlingSgs(t *testing.T) {
128128
sg, err := secgroups.Create(clientCompute, createSGOpts).Extract()
129129
th.AssertNoErr(t, err)
130130

131-
size := 15
131+
size := 20
132132
q := make(chan []string, size)
133133
for i := 0; i < size; i++ {
134-
go CreateMultipleSgsRules(clientNetworking, sg.ID, 47, i, q) // nolint
134+
go CreateMultipleSgsRules(clientNetworking, sg.ID, 70, i, q) // nolint
135135
}
136136
for i := 0; i < size; i++ {
137137
sgs := <-q

provider_client.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"fmt"
66
"io"
7-
"io/ioutil"
87
"net/http"
98
"strings"
109
"sync"
@@ -81,6 +80,10 @@ type ProviderClient struct {
8180
// UserAgent represents the User-Agent header in the HTTP request.
8281
UserAgent UserAgent
8382

83+
// MaxBackoffRetries set the maximum number of backoffs. When not set, defaults to defaultMaxBackoffRetryLimit
84+
MaxBackoffRetries *int
85+
// BackoffRetryTimeout specifies time before next retry on 429. When not set, defaults to defaultBackoffTimeout
86+
BackoffRetryTimeout *time.Duration
8487
// ReauthFunc is the function used to re-authenticate the user if the request
8588
// fails with a 401 HTTP response code. This a needed because there may be multiple
8689
// authentication functions for different Identity service versions.
@@ -173,10 +176,6 @@ type RequestOpts struct {
173176
RetryCount *int
174177
// RetryTimeout specifies time before next retry
175178
RetryTimeout *time.Duration
176-
// MaxBackoffRetries set the maximum number of backoffs. When not set, defaults to defaultMaxBackoffRetryLimit
177-
MaxBackoffRetries *int
178-
// BackoffRetryTimeout specifies time before next retry on 429. When not set, defaults to defaultBackoffTimeout
179-
BackoffRetryTimeout *time.Duration
180179
}
181180

182181
var applicationJSON = "application/json"
@@ -280,14 +279,14 @@ func (client *ProviderClient) Request(method, url string, options *RequestOpts)
280279
options.RetryTimeout = &defaultRetryTimeout
281280
}
282281

283-
if options.MaxBackoffRetries == nil {
284-
defaultMaxBackoffRetryLimit := 5
285-
options.MaxBackoffRetries = &defaultMaxBackoffRetryLimit
282+
if client.MaxBackoffRetries == nil {
283+
defaultMaxBackoffRetryLimit := 20
284+
client.MaxBackoffRetries = &defaultMaxBackoffRetryLimit
286285
}
287286

288-
if options.BackoffRetryTimeout == nil {
289-
defaultBackoffTimeout := 120 * time.Second
290-
options.BackoffRetryTimeout = &defaultBackoffTimeout
287+
if client.BackoffRetryTimeout == nil {
288+
defaultBackoffTimeout := 60 * time.Second
289+
client.BackoffRetryTimeout = &defaultBackoffTimeout
291290
}
292291

293292
// Validate the HTTP response status.
@@ -300,7 +299,7 @@ func (client *ProviderClient) Request(method, url string, options *RequestOpts)
300299
}
301300

302301
if !ok {
303-
body, _ := ioutil.ReadAll(resp.Body)
302+
body, _ := io.ReadAll(resp.Body)
304303
_ = resp.Body.Close()
305304
respErr := ErrUnexpectedResponseCode{
306305
URL: url,
@@ -389,9 +388,9 @@ func (client *ProviderClient) Request(method, url string, options *RequestOpts)
389388
if error429er, ok := errType.(Err429er); ok {
390389
err = error429er.Error429(respErr)
391390
}
392-
if *options.MaxBackoffRetries > 0 {
393-
*options.MaxBackoffRetries -= 1
394-
time.Sleep(*options.BackoffRetryTimeout)
391+
if *client.MaxBackoffRetries > 0 {
392+
*client.MaxBackoffRetries -= 1
393+
time.Sleep(*client.BackoffRetryTimeout)
395394
return client.Request(method, url, options)
396395
}
397396
case http.StatusInternalServerError:

0 commit comments

Comments
 (0)