Skip to content

Commit 53a706b

Browse files
Refactor retry logic method naming for clarity
1 parent 8fd6a5d commit 53a706b

File tree

3 files changed

+17
-29
lines changed

3 files changed

+17
-29
lines changed

workos/audit_logs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,13 @@ def create_event(
9090

9191
headers["idempotency-key"] = idempotency_key
9292

93-
# Enable retries for audit log event creation with default settings
93+
# Enable retries for audit log event creation with default retryConfig
9494
self._http_client.request(
9595
EVENTS_PATH,
9696
method=REQUEST_METHOD_POST,
9797
json=json,
9898
headers=headers,
99-
retry_config=RetryConfig(), # Uses default values: 3 retries, exponential backoff
99+
retry_config=RetryConfig(),
100100
)
101101

102102
def create_export(

workos/utils/_base_http_client.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -218,23 +218,22 @@ def _is_retryable_error(self, response: httpx.Response) -> bool:
218218
"""Determine if an error should be retried."""
219219
return response.status_code in RETRY_STATUS_CODES
220220

221-
def _get_retry_delay(
222-
self, attempt: int, response: httpx.Response, retry_config: RetryConfig
223-
) -> float:
224-
"""Calculate delay with exponential backoff and jitter."""
225-
return self._calculate_backoff_delay(attempt, retry_config)
226-
227-
def _calculate_backoff_delay(
228-
self, attempt: int, retry_config: RetryConfig
229-
) -> float:
221+
def _is_retryable_exception(self, exc: Exception) -> bool:
222+
"""Determine if an exception should trigger a retry."""
223+
# Retry on network [connection, timeout] exceptions
224+
if isinstance(exc, (httpx.ConnectError, httpx.TimeoutException)):
225+
return True
226+
return False
227+
228+
def _get_backoff_delay(self, attempt: int, retry_config: RetryConfig) -> float:
230229
"""Calculate delay with exponential backoff and jitter.
231230
232231
Args:
233232
attempt: The current retry attempt number (0-indexed)
234233
retry_config: The retry configuration
235234
236235
Returns:
237-
The delay in seconds to wait before the next retry
236+
The delay, in seconds, to wait before the next retry
238237
"""
239238
# Exponential backoff: base_delay * 2^attempt
240239
delay: float = retry_config.base_delay * (2**attempt)
@@ -246,13 +245,6 @@ def _calculate_backoff_delay(
246245
jitter_amount: float = delay * retry_config.jitter * random.random()
247246
return delay + jitter_amount
248247

249-
def _should_retry_exception(self, exc: Exception) -> bool:
250-
"""Determine if an exception should trigger a retry."""
251-
# Retry on network errors (connection, timeout)
252-
if isinstance(exc, (httpx.ConnectError, httpx.TimeoutException)):
253-
return True
254-
return False
255-
256248
def build_request_url(
257249
self,
258250
url: str,

workos/utils/http_client.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def request(
133133
if attempt < retry_config.max_retries and self._is_retryable_error(
134134
response
135135
):
136-
delay = self._get_retry_delay(attempt, response, retry_config)
136+
delay = self._get_backoff_delay(attempt, retry_config)
137137
time.sleep(delay)
138138
continue
139139

@@ -142,10 +142,8 @@ def request(
142142

143143
except Exception as exc:
144144
last_exception = exc
145-
if attempt < retry_config.max_retries and self._should_retry_exception(
146-
exc
147-
):
148-
delay = self._calculate_backoff_delay(attempt, retry_config)
145+
if attempt < retry_config.max_retries and self._is_retryable_exception(exc):
146+
delay = self._get_backoff_delay(attempt, retry_config)
149147
time.sleep(delay)
150148
continue
151149
raise
@@ -270,7 +268,7 @@ async def request(
270268
if attempt < retry_config.max_retries and self._is_retryable_error(
271269
response
272270
):
273-
delay = self._get_retry_delay(attempt, response, retry_config)
271+
delay = self._get_backoff_delay(attempt, retry_config)
274272
await asyncio.sleep(delay)
275273
continue
276274

@@ -279,10 +277,8 @@ async def request(
279277

280278
except Exception as exc:
281279
last_exception = exc
282-
if attempt < retry_config.max_retries and self._should_retry_exception(
283-
exc
284-
):
285-
delay = self._calculate_backoff_delay(attempt, retry_config)
280+
if attempt < retry_config.max_retries and self._is_retryable_exception(exc):
281+
delay = self._get_backoff_delay(attempt, retry_config)
286282
await asyncio.sleep(delay)
287283
continue
288284
raise

0 commit comments

Comments
 (0)