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

Fix automatic retries of failed requests #605

Merged
merged 3 commits into from
Aug 20, 2019
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
8 changes: 5 additions & 3 deletions stripe/http_client.py
Original file line number Diff line number Diff line change
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
Changaco marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
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