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

Only retry on request errors with 5XX status code and use exponential backoff #1146

Merged
merged 3 commits into from
Aug 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions plotly/api/v1/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import requests
from requests.exceptions import RequestException
from retrying import retry

from plotly import config, exceptions
from plotly.api.utils import basic_auth
from plotly.api.v2.utils import should_retry


def validate_response(response):
Expand Down Expand Up @@ -59,6 +61,8 @@ def get_headers():
return headers


@retry(wait_exponential_multiplier=1000, wait_exponential_max=16000,
stop_max_delay=180000, retry_on_exception=should_retry)
def request(method, url, **kwargs):
"""
Central place to make any v1 api request.
Expand Down
14 changes: 12 additions & 2 deletions plotly/api/v2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,18 @@ def get_headers():
return headers


@retry(wait_random_min=100, wait_random_max=1000, wait_exponential_max=10000,
stop_max_delay=120000)
def should_retry(exception):
if isinstance(exception, exceptions.PlotlyRequestError):
if (isinstance(exception.status_code, int) and
500 <= exception.status_code < 600):
# Retry on 5XX errors.
return True

return False


@retry(wait_exponential_multiplier=1000, wait_exponential_max=16000,
stop_max_delay=180000, retry_on_exception=should_retry)
def request(method, url, **kwargs):
"""
Central place to make any api v2 api request.
Expand Down
4 changes: 2 additions & 2 deletions plotly/plotly/plotly.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ def get_streaming_specs(self):

return streaming_specs

def heartbeat(self, reconnect_on=(200, '', 408)):
def heartbeat(self, reconnect_on=(200, '', 408, 502)):
"""
Keep stream alive. Streams will close after ~1 min of inactivity.

Expand Down Expand Up @@ -616,7 +616,7 @@ def open(self):
self._stream = chunked_requests.Stream(**streaming_specs)

def write(self, trace, layout=None,
reconnect_on=(200, '', 408)):
reconnect_on=(200, '', 408, 502)):
"""
Write to an open stream.

Expand Down