Skip to content

Commit

Permalink
chore: do not retry on HTTP 429 or HTTP 503
Browse files Browse the repository at this point in the history
Related to #470

We prefer not to retry requests:
- If the ingress return a "bare" 429, as the backend was not able to respond anyway.
- If the ingress return a "bare" 503, to prevent possible self made ddos, and preventing a disaster recovery.
  • Loading branch information
jooola committed Jul 18, 2024
1 parent 756f605 commit daf72a3
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 13 deletions.
5 changes: 1 addition & 4 deletions hcloud/client_handler_retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,8 @@ func retryPolicy(resp *Response, err error) bool {
}
case errors.Is(err, ErrStatusCode):
switch resp.Response.StatusCode {
// 4xx errors
case http.StatusTooManyRequests:
return true
// 5xx errors
case http.StatusBadGateway, http.StatusServiceUnavailable, http.StatusGatewayTimeout:
case http.StatusBadGateway, http.StatusGatewayTimeout:
return true
}
case errors.As(err, &netErr):
Expand Down
14 changes: 7 additions & 7 deletions hcloud/client_handler_retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ func TestRetryHandler(t *testing.T) {
},
},
{
name: "http 503 error recovery",
name: "http 502 error recovery",
wrapped: func(req *http.Request, _ any) (*Response, error) {
resp := fakeResponse(t, 503, "", false)
resp := fakeResponse(t, 502, "", false)
resp.Response.Request = req
return resp, fmt.Errorf("%w %d", ErrStatusCode, 503)
return resp, fmt.Errorf("%w %d", ErrStatusCode, 502)
},
recover: true,
want: func(t *testing.T, err error, retryCount int) {
Expand All @@ -44,14 +44,14 @@ func TestRetryHandler(t *testing.T) {
},
},
{
name: "http 503 error",
name: "http 502 error",
wrapped: func(req *http.Request, _ any) (*Response, error) {
resp := fakeResponse(t, 503, "", false)
resp := fakeResponse(t, 502, "", false)
resp.Response.Request = req
return resp, fmt.Errorf("%w %d", ErrStatusCode, 503)
return resp, fmt.Errorf("%w %d", ErrStatusCode, 502)
},
want: func(t *testing.T, err error, retryCount int) {
assert.EqualError(t, err, "server responded with status code 503")
assert.EqualError(t, err, "server responded with status code 502")
assert.Equal(t, 5, retryCount)
},
},
Expand Down
2 changes: 0 additions & 2 deletions hcloud/hcloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ The following rules defines when a request can be retried:
When the [http.Client] returned a network timeout error.
When the API returned an HTTP error, with the status code:
- [http.StatusTooManyRequests]
- [http.StatusBadGateway]
- [http.StatusServiceUnavailable]
- [http.StatusGatewayTimeout]
When the API returned an application error, with the code:
Expand Down

0 comments on commit daf72a3

Please sign in to comment.