Skip to content

Commit a016af6

Browse files
Replace k8s.io/apimachinery with cenkalti/backoff (#205)
Co-authored-by: Marcel Boehm <marcel.boehm@inovex.de>
1 parent 8263f00 commit a016af6

File tree

3 files changed

+28
-35
lines changed

3 files changed

+28
-35
lines changed

core/clients/clients.go

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"strings"
88
"time"
99

10-
"k8s.io/apimachinery/pkg/util/wait"
10+
"github.com/cenkalti/backoff/v4"
1111
)
1212

1313
const (
@@ -55,31 +55,34 @@ func Do(client *http.Client, req *http.Request, cfg *RetryConfig) (resp *http.Re
5555
client = &http.Client{}
5656
}
5757
client.Timeout = cfg.ClientTimeout
58+
5859
maxRetries := cfg.MaxRetries
59-
err = wait.PollUntilContextTimeout(context.Background(), cfg.WaitBetweenCalls, cfg.RetryTimeout, true, wait.ConditionFunc(
60-
func() (bool, error) {
61-
resp, err = client.Do(req) //nolint:bodyclose // body is closed by the caller functions
62-
if err != nil {
63-
if maxRetries > 0 {
64-
if errorIsOneOf(err, ClientTimeoutErr, ClientContextDeadlineErr, ClientConnectionRefusedErr) ||
65-
(req.Method == http.MethodGet && strings.Contains(err.Error(), ClientEOFError)) {
66-
// reduce retries counter and retry
67-
maxRetries--
68-
return false, nil
69-
}
70-
}
71-
return false, err
72-
}
73-
if maxRetries > 0 && resp != nil {
74-
if resp.StatusCode == http.StatusBadGateway ||
75-
resp.StatusCode == http.StatusGatewayTimeout {
60+
ctx, cancel := context.WithTimeout(context.Background(), cfg.RetryTimeout)
61+
defer cancel()
62+
b := backoff.WithContext(backoff.NewConstantBackOff(cfg.WaitBetweenCalls), ctx)
63+
64+
err = backoff.Retry(func() error {
65+
resp, err = client.Do(req) //nolint:bodyclose // body is closed by the caller functions
66+
if err != nil {
67+
if maxRetries > 0 {
68+
if errorIsOneOf(err, ClientTimeoutErr, ClientContextDeadlineErr, ClientConnectionRefusedErr) ||
69+
(req.Method == http.MethodGet && strings.Contains(err.Error(), ClientEOFError)) {
70+
// reduce retries counter and retry
7671
maxRetries--
77-
return false, nil
72+
return err
7873
}
7974
}
80-
return true, nil
81-
}).WithContext(),
82-
)
75+
return backoff.Permanent(err)
76+
}
77+
if maxRetries > 0 && resp != nil {
78+
if resp.StatusCode == http.StatusBadGateway ||
79+
resp.StatusCode == http.StatusGatewayTimeout {
80+
maxRetries--
81+
return fmt.Errorf("requested returned a gateway error")
82+
}
83+
}
84+
return nil
85+
}, b)
8386
if err != nil {
8487
return resp, fmt.Errorf("url: %s\nmethod: %s\n err: %w", req.URL.String(), req.Method, err)
8588
}

core/go.mod

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,16 @@ go 1.18
44

55
require (
66
github.com/MicahParks/keyfunc/v2 v2.1.0
7+
github.com/cenkalti/backoff/v4 v4.2.1
78
github.com/google/go-cmp v0.6.0
89
github.com/google/uuid v1.4.0
910
golang.org/x/oauth2 v0.14.0
10-
k8s.io/apimachinery v0.28.4
1111
)
1212

1313
require (
14-
github.com/go-logr/logr v1.2.4 // indirect
1514
github.com/golang-jwt/jwt/v5 v5.1.0
1615
github.com/golang/protobuf v1.5.3 // indirect
1716
golang.org/x/net v0.18.0 // indirect
1817
google.golang.org/appengine v1.6.7 // indirect
1918
google.golang.org/protobuf v1.31.0 // indirect
20-
k8s.io/klog/v2 v2.100.1 // indirect
21-
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
2219
)

core/go.sum

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
github.com/MicahParks/keyfunc/v2 v2.1.0 h1:6ZXKb9Rp6qp1bDbJefnG7cTH8yMN1IC/4nf+GVjO99k=
22
github.com/MicahParks/keyfunc/v2 v2.1.0/go.mod h1:rW42fi+xgLJ2FRRXAfNx9ZA8WpD4OeE/yHVMteCkw9k=
3-
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
4-
github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
5-
github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
3+
github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM=
4+
github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
65
github.com/golang-jwt/jwt/v5 v5.1.0 h1:UGKbA/IPjtS6zLcdB7i5TyACMgSbOTiR8qzXgw8HWQU=
76
github.com/golang-jwt/jwt/v5 v5.1.0/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
87
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
@@ -31,9 +30,3 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
3130
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
3231
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
3332
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
34-
k8s.io/apimachinery v0.28.4 h1:zOSJe1mc+GxuMnFzD4Z/U1wst50X28ZNsn5bhgIIao8=
35-
k8s.io/apimachinery v0.28.4/go.mod h1:wI37ncBvfAoswfq626yPTe6Bz1c22L7uaJ8dho83mgg=
36-
k8s.io/klog/v2 v2.100.1 h1:7WCHKK6K8fNhTqfBhISHQ97KrnJNFZMcQvKp7gP/tmg=
37-
k8s.io/klog/v2 v2.100.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
38-
k8s.io/utils v0.0.0-20230726121419-3b25d923346b h1:sgn3ZU783SCgtaSJjpcVVlRqd6GSnlTLKgpAAttJvpI=
39-
k8s.io/utils v0.0.0-20230726121419-3b25d923346b/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=

0 commit comments

Comments
 (0)