Skip to content

Commit

Permalink
Retry api call on 502 bad gateway error (#206)
Browse files Browse the repository at this point in the history
* Retry api call on 502 bad gateway error
  • Loading branch information
aniezurawski authored Jan 23, 2020
1 parent ca7d24b commit 9afee18
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions neptune/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import requests
from bravado.exception import BravadoConnectionError, BravadoTimeoutError, HTTPForbidden, \
HTTPInternalServerError, HTTPServerError, HTTPUnauthorized, HTTPServiceUnavailable, HTTPRequestTimeout, \
HTTPGatewayTimeout
HTTPGatewayTimeout, HTTPBadGateway

from neptune.api_exceptions import ConnectionLost, Forbidden, ServerError, \
Unauthorized, SSLError
Expand Down Expand Up @@ -201,7 +201,7 @@ def wrapper(*args, **kwargs):
raise SSLError()
except (BravadoConnectionError, BravadoTimeoutError,
requests.exceptions.ConnectionError, requests.exceptions.Timeout,
HTTPRequestTimeout, HTTPServiceUnavailable, HTTPGatewayTimeout):
HTTPRequestTimeout, HTTPServiceUnavailable, HTTPGatewayTimeout, HTTPBadGateway):
if retry >= 6:
_logger.warning('Experiencing connection interruptions. Reestablishing communication with Neptune.')
time.sleep(2 ** retry)
Expand All @@ -216,7 +216,16 @@ def wrapper(*args, **kwargs):
if e.response is None:
raise
status_code = e.response.status_code
if status_code >= HTTPInternalServerError.status_code:
if status_code in (
HTTPBadGateway.status_code,
HTTPServiceUnavailable.status_code,
HTTPGatewayTimeout.status_code):
if retry >= 6:
_logger.warning(
'Experiencing connection interruptions. Reestablishing communication with Neptune.')
time.sleep(2 ** retry)
continue
elif status_code >= HTTPInternalServerError.status_code:
raise ServerError()
elif status_code == HTTPUnauthorized.status_code:
raise Unauthorized()
Expand Down

0 comments on commit 9afee18

Please sign in to comment.