Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: retry on cloudfront http errors #572

Merged
merged 4 commits into from
Dec 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions ably/rest_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ func (c *REST) doWithHandle(ctx context.Context, r *request, handle func(*http.R
if err != nil {
c.log.Error("RestClient: error handling response: ", err)
if e, ok := err.(*ErrorInfo); ok {
if canFallBack(e.StatusCode) {
if canFallBack(e.StatusCode, resp) {
fallbacks, _ := c.opts.getFallbackHosts()
c.log.Infof("RestClient: trying to fallback with hosts=%v", fallbacks)
if len(fallbacks) > 0 {
Expand Down Expand Up @@ -735,7 +735,7 @@ func (c *REST) doWithHandle(ctx context.Context, r *request, handle func(*http.R
return nil, err
}
if ev, ok := err.(*ErrorInfo); ok {
if canFallBack(ev.StatusCode) {
if canFallBack(ev.StatusCode, resp) {
iteration++
continue
}
Expand Down Expand Up @@ -764,9 +764,9 @@ func (c *REST) doWithHandle(ctx context.Context, r *request, handle func(*http.R
return resp, nil
}

func canFallBack(code int) bool {
return http.StatusInternalServerError <= code &&
code <= http.StatusGatewayTimeout
func canFallBack(statusCode int, res *http.Response) bool {
return (statusCode >= http.StatusInternalServerError && statusCode <= http.StatusGatewayTimeout) ||
(res != nil && strings.EqualFold(res.Header.Get("Server"), "CloudFront") && statusCode >= http.StatusBadRequest) // RSC15l4
}
sacOO7 marked this conversation as resolved.
Show resolved Hide resolved

// newHTTPRequest creates a new http.Request that can be sent to ably endpoints.
Expand Down