Skip to content

Commit 3841a13

Browse files
Remove backoff dependency and deprecate RetryOptions (#337)
* Remove backoff dependency and deprecate RetryOptions * Update CHANGELOG, example * Changes after review * Remove consts * Remove Do routine, update changelogs * Update CHANGELOG * Add release date * Remove const
1 parent 09e5e36 commit 3841a13

17 files changed

+109
-333
lines changed

CHANGELOG.md

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,41 @@
1-
## Release (YYYY-MM-DD)
1+
## Release (2024-02-27)
22

3-
- `core`: [v0.y.z]
3+
- `core`: [v0.10.0](core/CHANGELOG.md#v0100-2024-02-27)
44
- **Feature:** Add package `runtime`, which implements methods to be used when performing API requests.
55
- **Feature:** Add method `WithCaptureHTTPResponse` to package `runtime`, which does the same as `config.WithCaptureHTTPResponse`. Method was moved to avoid confusion due to it not being a configuration option, and will be removed in a later release.
66
- **Feature:** Add configuration option that, for the key flow, enables a goroutine to be spawned that will refresh the access token when it's close to expiring
77
- **Deprecation:** Mark method `config.WithCaptureHTTPResponse` as deprecated, to avoid confusion due to it not being a configuration option. Use `runtime.WithCaptureHTTPResponse` instead.
88
- **Deprecation:** Mark method `config.WithJWKSEndpoint` and field `config.Configuration.JWKSCustomUrl` as deprecated. Validation using JWKS was removed, for being redundant with token validation done in the APIs. These have no effect.
9-
- **Breaking Change:** Remove method `KeyFlow.Clone`, that was no longer being used.
9+
- **Deprecation:**
10+
- Methods:
11+
- `config.WithMaxRetries`
12+
- `config.WithWaitBetweenCalls`
13+
- `config.WithRetryTimeout`
14+
- `clients.NewRetryConfig`
15+
- Fields:
16+
- `clients.KeyFlowConfig.ClientRetry`
17+
- `clients.TokenFlowConfig.ClientRetry`
18+
- `clients.NoAuthFlowConfig.ClientRetry`
19+
- `clients.RetryConfig`
20+
- Retry options removed to reduce complexity of the clients. If this functionality is needed, you can provide your own custom HTTP client.
21+
- **Breaking Change:** Change signature of `auth.NoAuth`, which no longer takes `clients.RetryConfig` as argument.
22+
- **Breaking Change:**
23+
- Methods:
24+
- `clients.KeyFlow.Clone`
25+
- `clients.TokenFlow.Clone`
26+
- `clients.NoAuthFlow.Clone`
27+
- `clients.Do`
28+
- Fields:
29+
- `clients.DefaultRetryMaxRetries`
30+
- `clients.DefaultRetryWaitBetweenCalls`
31+
- `clients.DefaultRetryTimeout`
32+
- Constants:
33+
- `clients.ClientTimeoutErr`
34+
- `clients.ClientContextDeadlineErr`
35+
- `clients.ClientConnectionRefusedErr`
36+
- `clients.ClientEOFError`
37+
- `clients.Environment`
38+
- Removed to reduce complexity of the clients, they were no longer being used.
1039

1140
## Release (2024-02-07)
1241

core/CHANGELOG.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,36 @@
1-
## v0.10.0 (YYYY-MM-DD)
1+
## v0.10.0 (2024-02-27)
22

33
- **Feature:** Add configuration option that, for the key flow, enables a goroutine to be spawned that will refresh the access token when it's close to expiring
4+
- **Deprecation:**
5+
- Methods:
6+
- `config.WithMaxRetries`
7+
- `config.WithWaitBetweenCalls`
8+
- `config.WithRetryTimeout`
9+
- `clients.NewRetryConfig`
10+
- Fields:
11+
- `clients.KeyFlowConfig.ClientRetry`
12+
- `clients.TokenFlowConfig.ClientRetry`
13+
- `clients.NoAuthFlowConfig.ClientRetry`
14+
- `clients.RetryConfig`
15+
- Retry options were removed to reduce complexity of the clients. If this functionality is needed, you can provide your own custom HTTP client.
16+
- **Breaking Change:** Change signature of `auth.NoAuth`, which no longer takes `clients.RetryConfig` as argument.
17+
- **Breaking Change:**
18+
- Methods:
19+
- `clients.KeyFlow.Clone`
20+
- `clients.TokenFlow.Clone`
21+
- `clients.NoAuthFlow.Clone`
22+
- `clients.Do`
23+
- Fields:
24+
- `clients.DefaultRetryMaxRetries`
25+
- `clients.DefaultRetryWaitBetweenCalls`
26+
- `clients.DefaultRetryTimeout`
27+
- Constants:
28+
- `clients.ClientTimeoutErr`
29+
- `clients.ClientContextDeadlineErr`
30+
- `clients.ClientConnectionRefusedErr`
31+
- `clients.ClientEOFError`
32+
- `clients.Environment`
33+
- Removed to reduce complexity of the clients, they were no longer being used.
434

535
## v0.9.0 (2024-02-19)
636

core/auth/auth.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func SetupAuth(cfg *config.Configuration) (rt http.RoundTripper, err error) {
3939
if cfg.CustomAuth != nil {
4040
return cfg.CustomAuth, nil
4141
} else if cfg.NoAuth {
42-
noAuthRoundTripper, err := NoAuth(cfg.RetryOptions)
42+
noAuthRoundTripper, err := NoAuth()
4343
if err != nil {
4444
return nil, fmt.Errorf("configuring no auth client: %w", err)
4545
}
@@ -93,10 +93,8 @@ func DefaultAuth(cfg *config.Configuration) (rt http.RoundTripper, err error) {
9393

9494
// NoAuth configures a flow without authentication and returns an http.RoundTripper
9595
// that can be used to make unauthenticated requests
96-
func NoAuth(retryOptions *clients.RetryConfig) (rt http.RoundTripper, err error) {
97-
noAuthConfig := clients.NoAuthFlowConfig{
98-
ClientRetry: retryOptions,
99-
}
96+
func NoAuth() (rt http.RoundTripper, err error) {
97+
noAuthConfig := clients.NoAuthFlowConfig{}
10098
noAuthRoundTripper := &clients.NoAuthFlow{}
10199
if err := noAuthRoundTripper.Init(noAuthConfig); err != nil {
102100
return nil, fmt.Errorf("initializing client: %w", err)
@@ -125,7 +123,6 @@ func TokenAuth(cfg *config.Configuration) (http.RoundTripper, error) {
125123

126124
tokenCfg := clients.TokenFlowConfig{
127125
ServiceAccountToken: cfg.Token,
128-
ClientRetry: cfg.RetryOptions,
129126
}
130127

131128
client := &clients.TokenFlow{}
@@ -181,7 +178,6 @@ func KeyAuth(cfg *config.Configuration) (http.RoundTripper, error) {
181178
keyCfg := clients.KeyFlowConfig{
182179
ServiceAccountKey: serviceAccountKey,
183180
PrivateKey: cfg.PrivateKey,
184-
ClientRetry: cfg.RetryOptions,
185181
TokenUrl: cfg.TokenCustomUrl,
186182
BackgroundTokenRefreshContext: cfg.BackgroundTokenRefreshContext,
187183
}

core/auth/auth_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ func TestNoAuth(t *testing.T) {
558558
} {
559559
t.Run(test.desc, func(t *testing.T) {
560560
// Get the default authentication client and ensure that it's not nil
561-
authClient, err := NoAuth(nil)
561+
authClient, err := NoAuth()
562562
if err != nil {
563563
t.Fatalf("Test returned error on valid test case: %v", err)
564564
}

core/clients/clients.go

Lines changed: 4 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,22 @@
11
package clients
22

33
import (
4-
"context"
5-
"fmt"
6-
"net/http"
7-
"strings"
84
"time"
9-
10-
"github.com/cenkalti/backoff/v4"
11-
)
12-
13-
const (
14-
// API configuration options:
15-
Environment = "STACKIT_ENV"
16-
)
17-
18-
const (
19-
// Known error messages
20-
ClientTimeoutErr = "Client.Timeout exceeded while awaiting headers"
21-
ClientContextDeadlineErr = "context deadline exceeded"
22-
ClientConnectionRefusedErr = "connection refused"
23-
ClientEOFError = "unexpected EOF"
245
)
256

267
const (
27-
DefaultClientTimeout = time.Minute
28-
DefaultRetryMaxRetries = 3
29-
DefaultRetryWaitBetweenCalls = 30 * time.Second
30-
DefaultRetryTimeout = 2 * time.Minute
8+
DefaultClientTimeout = time.Minute
319
)
3210

11+
// Deprecated: retry options were removed to reduce complexity of the client. If this functionality is needed, you can provide your own custom HTTP client.
3312
type RetryConfig struct {
3413
MaxRetries int // Max retries
3514
WaitBetweenCalls time.Duration // Time to wait between requests
3615
RetryTimeout time.Duration // Max time to re-try
3716
ClientTimeout time.Duration // HTTP Client timeout
3817
}
3918

19+
// Deprecated: retry options were removed to reduce complexity of the client. If this functionality is needed, you can provide your own custom HTTP client.
4020
func NewRetryConfig() *RetryConfig {
41-
return &RetryConfig{
42-
MaxRetries: DefaultRetryMaxRetries,
43-
WaitBetweenCalls: DefaultRetryWaitBetweenCalls,
44-
RetryTimeout: DefaultRetryTimeout,
45-
ClientTimeout: DefaultClientTimeout,
46-
}
47-
}
48-
49-
// Do performs the request, retrying according to the configurations provided
50-
func Do(client *http.Client, req *http.Request, cfg *RetryConfig) (resp *http.Response, err error) {
51-
if cfg == nil {
52-
cfg = NewRetryConfig()
53-
}
54-
if client == nil {
55-
client = &http.Client{}
56-
}
57-
client.Timeout = cfg.ClientTimeout
58-
59-
maxRetries := cfg.MaxRetries
60-
ctx, cancel := context.WithTimeout(req.Context(), 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
71-
maxRetries--
72-
return err
73-
}
74-
}
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("request returned a gateway error")
82-
}
83-
}
84-
return nil
85-
}, b)
86-
if err != nil {
87-
return resp, fmt.Errorf("url: %s\nmethod: %s\n err: %w", req.URL.String(), req.Method, err)
88-
}
89-
90-
return resp, err
91-
}
92-
93-
// ErrorIsOneOf checks if a given error message
94-
// has one of the provided sub strings
95-
func errorIsOneOf(err error, msgs ...string) bool {
96-
if err == nil {
97-
return false
98-
}
99-
for _, m := range msgs {
100-
if strings.Contains(err.Error(), m) {
101-
return true
102-
}
103-
}
104-
return false
21+
return &RetryConfig{}
10522
}

core/clients/clients_test.go

Lines changed: 0 additions & 123 deletions
This file was deleted.

0 commit comments

Comments
 (0)