Skip to content

Commit 4556ef9

Browse files
authored
Only retry on request errors with 5XX status code and use exponential backoff (#1146)
* Only retry on request errors with 5XX status code Also, switch to exponential backoff to give the servers more breathing room. Tries in seconds: 0, 1, 2, 4, 8, 16, 16, 16... for up to 3 minutes total. * Reconnect stream write/hearbeat on 502 errors * Added retrying logic to v1 api request function
1 parent 5138734 commit 4556ef9

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

Diff for: plotly/api/v1/utils.py

+4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import requests
44
from requests.exceptions import RequestException
5+
from retrying import retry
56

67
from plotly import config, exceptions
78
from plotly.api.utils import basic_auth
9+
from plotly.api.v2.utils import should_retry
810

911

1012
def validate_response(response):
@@ -59,6 +61,8 @@ def get_headers():
5961
return headers
6062

6163

64+
@retry(wait_exponential_multiplier=1000, wait_exponential_max=16000,
65+
stop_max_delay=180000, retry_on_exception=should_retry)
6266
def request(method, url, **kwargs):
6367
"""
6468
Central place to make any v1 api request.

Diff for: plotly/api/v2/utils.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,18 @@ def get_headers():
111111
return headers
112112

113113

114-
@retry(wait_random_min=100, wait_random_max=1000, wait_exponential_max=10000,
115-
stop_max_delay=120000)
114+
def should_retry(exception):
115+
if isinstance(exception, exceptions.PlotlyRequestError):
116+
if (isinstance(exception.status_code, int) and
117+
500 <= exception.status_code < 600):
118+
# Retry on 5XX errors.
119+
return True
120+
121+
return False
122+
123+
124+
@retry(wait_exponential_multiplier=1000, wait_exponential_max=16000,
125+
stop_max_delay=180000, retry_on_exception=should_retry)
116126
def request(method, url, **kwargs):
117127
"""
118128
Central place to make any api v2 api request.

Diff for: plotly/plotly/plotly.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ def get_streaming_specs(self):
578578

579579
return streaming_specs
580580

581-
def heartbeat(self, reconnect_on=(200, '', 408)):
581+
def heartbeat(self, reconnect_on=(200, '', 408, 502)):
582582
"""
583583
Keep stream alive. Streams will close after ~1 min of inactivity.
584584
@@ -616,7 +616,7 @@ def open(self):
616616
self._stream = chunked_requests.Stream(**streaming_specs)
617617

618618
def write(self, trace, layout=None,
619-
reconnect_on=(200, '', 408)):
619+
reconnect_on=(200, '', 408, 502)):
620620
"""
621621
Write to an open stream.
622622

0 commit comments

Comments
 (0)