Skip to content

Commit

Permalink
Fix automatic retries of failed requests (#605)
Browse files Browse the repository at this point in the history
* fix automatic retries of failed requests

* fix `test_retry_codes_until_exceeded`

If the number of retries is set to 3, then the total number of requests should be 4: the initial one + the three retries.

* improve consistency between HTTP clients

This commit modifies `PycurlClient` to set the `should_retry` attribute of `APIConnectionError` exceptions, like `RequestsClient` does.
  • Loading branch information
Changaco authored and ob-stripe committed Aug 20, 2019
1 parent 0163bb7 commit 5bb1865
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
8 changes: 5 additions & 3 deletions stripe/http_client.py
Expand Up @@ -119,7 +119,6 @@ def request_with_retries(self, method, url, headers, post_data=None):
request_start = _now_ms()

try:
num_retries += 1
response = self.request(method, url, headers, post_data)
connection_error = None
except error.APIConnectionError as e:
Expand All @@ -132,7 +131,7 @@ def request_with_retries(self, method, url, headers, post_data=None):
"Encountered a retryable error %s"
% connection_error.user_message
)

num_retries += 1
sleep_time = self._sleep_time_seconds(num_retries)
util.log_info(
(
Expand Down Expand Up @@ -494,21 +493,24 @@ def _handle_request_error(self, e):
"https://twitter.com/stripestatus, or let us know at "
"support@stripe.com."
)
should_retry = True
elif e.args[0] in [pycurl.E_SSL_CACERT, pycurl.E_SSL_PEER_CERTIFICATE]:
msg = (
"Could not verify Stripe's SSL certificate. Please make "
"sure that your network is not intercepting certificates. "
"If this problem persists, let us know at "
"support@stripe.com."
)
should_retry = False
else:
msg = (
"Unexpected error communicating with Stripe. If this "
"problem persists, let us know at support@stripe.com."
)
should_retry = False

msg = textwrap.fill(msg) + "\n\n(Network error: " + e.args[1] + ")"
raise error.APIConnectionError(msg)
raise error.APIConnectionError(msg, should_retry=should_retry)

def _get_proxy(self, url):
if self._proxy:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_http_client.py
Expand Up @@ -436,10 +436,10 @@ def test_retry_codes(self, mock_retry, response, check_call_numbers):
def test_retry_codes_until_exceeded(
self, mock_retry, response, check_call_numbers
):
mock_retry(responses=[response(code=409)] * self.max_retries())
mock_retry(responses=[response(code=409)] * (self.max_retries() + 1))
_, code, _ = self.make_request()
assert code == 409
check_call_numbers(self.max_retries())
check_call_numbers(self.max_retries() + 1)

@pytest.fixture
def connection_error(self, session):
Expand Down

0 comments on commit 5bb1865

Please sign in to comment.