Skip to content

Commit

Permalink
Merge ec6ca5b into 3d9c0dc
Browse files Browse the repository at this point in the history
  • Loading branch information
xavdid-stripe committed Jun 12, 2024
2 parents 3d9c0dc + ec6ca5b commit e287857
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 34 deletions.
2 changes: 1 addition & 1 deletion stripe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
default_http_client: Optional["HTTPClient"] = None
app_info: Optional[AppInfo] = None
enable_telemetry: bool = True
max_network_retries: int = 0
max_network_retries: int = 2
ca_bundle_path: str = os.path.join(
os.path.dirname(__file__), "data", "ca-certificates.crt"
)
Expand Down
8 changes: 4 additions & 4 deletions stripe/_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,9 @@ def _should_retry(
num_retries: int,
max_network_retries: Optional[int],
):
max_network_retries = (
max_network_retries if max_network_retries is not None else 0
)
if max_network_retries is None:
max_network_retries = stripe.max_network_retries

if num_retries >= max_network_retries:
return False

Expand All @@ -211,7 +211,7 @@ def _should_retry(
return True

# Retry on conflict errors.
if status_code == 409:
if status_code == 409 or status_code == 429:
return True

# Retry on 500, 503, and other internal errors.
Expand Down
43 changes: 14 additions & 29 deletions tests/test_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,43 +179,28 @@ def test_jitter_has_randomness_but_within_range(self):

class TestRetryConditionsDefaultHttpClient(StripeClientTestCase):
def test_should_retry_on_codes(self):
one_xx = list(range(100, 104))
two_xx = list(range(200, 209))
three_xx = list(range(300, 308))
four_xx = list(range(400, 431))

client = _http_client.new_default_http_client()
codes = one_xx + two_xx + three_xx + four_xx
codes.remove(409)

no_retry_codes = {
*range(100, 104),
*range(200, 209),
*range(300, 308),
*range(400, 431),
} - {409, 429}

# These status codes should not be retried by default.
for code in codes:
assert (
client._should_retry(
(None, code, None), None, 0, max_network_retries=1
)
is False
for code in no_retry_codes:
assert not client._should_retry(
(None, code, None), None, 0, max_network_retries=1
)

retry_codes = [409, 429, 500, 503]

# These status codes should be retried by default.
assert (
client._should_retry(
for code in retry_codes:
assert client._should_retry(
(None, 409, None), None, 0, max_network_retries=1
)
is True
)
assert (
client._should_retry(
(None, 500, None), None, 0, max_network_retries=1
)
is True
)
assert (
client._should_retry(
(None, 503, None), None, 0, max_network_retries=1
)
is True
)

def test_should_retry_on_error(self, mocker):
client = _http_client.new_default_http_client()
Expand Down

0 comments on commit e287857

Please sign in to comment.