Skip to content

Commit

Permalink
Merge pull request #572 from ably/fallback-on-cloudfront-error
Browse files Browse the repository at this point in the history
feat: retry on cloudfront http errors
  • Loading branch information
QuintinWillison committed Dec 16, 2022
2 parents 25877a6 + e4ddcb9 commit 9ae4809
Showing 1 changed file with 5 additions and 5 deletions.
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
}

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

0 comments on commit 9ae4809

Please sign in to comment.