Skip to content

Commit f9d391b

Browse files
authored
refactor(settings): use request retry logic of core instead of custom one (#1894)
The settings client had its own retry implementation, leading to duplicated code and more maintenance if a change is needed on both ends. Now it's using the retry logic of core and not using a custom one anymore.
1 parent 683cca7 commit f9d391b

File tree

3 files changed

+13
-28
lines changed

3 files changed

+13
-28
lines changed

pkg/client/dtclient/retry.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,6 @@ func SendWithRetry(ctx context.Context, sendWithBody SendRequestWithBody, endpoi
8181
return nil, fmt.Errorf("HTTP send request %s failed after %d retries: %w", endpoint, setting.MaxRetries, err)
8282
}
8383

84-
// SendWithRetryWithInitialTry will try to call sendWithBody and if it didn't succeed call [SendWithRetry]
85-
func SendWithRetryWithInitialTry(ctx context.Context, sendWithBody SendRequestWithBody, endpoint string, requestOptions corerest.RequestOptions, body []byte, setting RetrySetting) (*coreapi.Response, error) {
86-
resp, err := coreapi.AsResponseOrError(sendWithBody(ctx, endpoint, bytes.NewReader(body), requestOptions))
87-
if err == nil {
88-
return resp, nil
89-
}
90-
91-
apiError := coreapi.APIError{}
92-
if !errors.As(err, &apiError) || !corerest.ShouldRetry(apiError.StatusCode) {
93-
return nil, err
94-
}
95-
96-
return SendWithRetry(ctx, sendWithBody, endpoint, requestOptions, body, setting)
97-
}
98-
9984
func GetWithRetry(ctx context.Context, c corerest.Client, endpoint string, requestOptions corerest.RequestOptions, settings RetrySetting) (resp *coreapi.Response, err error) {
10085
resp, err = coreapi.AsResponseOrError(c.GET(ctx, endpoint, requestOptions))
10186
if err == nil {

pkg/client/dtclient/retry_test.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,6 @@ func Test_sendWithRetryReturnsIfNotSuccess(t *testing.T) {
115115
assert.Equal(t, 400, apiError.StatusCode)
116116
}
117117

118-
func Test_SendWithRetryWithInitialTry_RetryIgnoredIfForbidden(t *testing.T) {
119-
i := 0
120-
mockCall := SendRequestWithBody(func(ctx context.Context, url string, data io.Reader, options corerest.RequestOptions) (*http.Response, error) {
121-
i++
122-
return nil, coreapi.APIError{StatusCode: http.StatusForbidden}
123-
})
124-
125-
_, err := SendWithRetryWithInitialTry(t.Context(), mockCall, "some/path", corerest.RequestOptions{}, []byte("body"), RetrySetting{MaxRetries: 10})
126-
require.Error(t, err)
127-
assert.Equal(t, 1, i)
128-
}
129-
130118
func Test_GetWithRetry_RetryIgnoredIfForbidden(t *testing.T) {
131119
i := 0
132120
mux := http.NewServeMux()

pkg/client/dtclient/settings_client.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"github.com/dynatrace/dynatrace-configuration-as-code/v2/internal/idutils"
3939
"github.com/dynatrace/dynatrace-configuration-as-code/v2/internal/log"
4040
"github.com/dynatrace/dynatrace-configuration-as-code/v2/internal/log/field"
41+
"github.com/dynatrace/dynatrace-configuration-as-code/v2/internal/pointer"
4142
"github.com/dynatrace/dynatrace-configuration-as-code/v2/internal/version"
4243
dtVersion "github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/client/version"
4344
"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/config"
@@ -580,7 +581,18 @@ func (d *SettingsClient) Upsert(ctx context.Context, obj SettingsObject, upsertO
580581
retrySetting = *upsertOptions.OverrideRetry
581582
}
582583

583-
resp, err := SendWithRetryWithInitialTry(ctx, d.client.POST, d.settingsObjectAPIPath, corerest.RequestOptions{CustomShouldRetryFunc: corerest.RetryIfTooManyRequests}, payload, retrySetting)
584+
httpResp, err := d.client.POST(ctx, d.settingsObjectAPIPath, bytes.NewReader(payload), corerest.RequestOptions{
585+
CustomShouldRetryFunc: func(response *http.Response) bool {
586+
return corerest.ShouldRetry(response.StatusCode)
587+
},
588+
MaxRetries: pointer.Pointer(retrySetting.MaxRetries),
589+
DelayAfterRetry: pointer.Pointer(retrySetting.WaitTime),
590+
})
591+
if err != nil {
592+
d.settingsCache.Delete(obj.SchemaId)
593+
return DynatraceEntity{}, fmt.Errorf("failed to create or update settings object with externalId %s: %w", externalID, err)
594+
}
595+
resp, err := coreapi.NewResponseFromHTTPResponse(httpResp)
584596
if err != nil {
585597
d.settingsCache.Delete(obj.SchemaId)
586598
return DynatraceEntity{}, fmt.Errorf("failed to create or update settings object with externalId %s: %w", externalID, err)

0 commit comments

Comments
 (0)