Skip to content

Commit 9d2dae2

Browse files
moar cleanup
1 parent e3a12d7 commit 9d2dae2

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

workos/utils/_base_http_client.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,18 @@ def _is_retryable_error(self, response: httpx.Response) -> bool:
217217

218218
def _get_retry_delay(self, attempt: int, response: httpx.Response, retry_config: RetryConfig) -> float:
219219
"""Calculate delay with exponential backoff and jitter."""
220+
return self._calculate_backoff_delay(attempt, retry_config)
221+
222+
def _calculate_backoff_delay(self, attempt: int, retry_config: RetryConfig) -> float:
223+
"""Calculate delay with exponential backoff and jitter.
224+
225+
Args:
226+
attempt: The current retry attempt number (0-indexed)
227+
retry_config: The retry configuration
228+
229+
Returns:
230+
The delay in seconds to wait before the next retry
231+
"""
220232
# Exponential backoff: base_delay * 2^attempt
221233
delay = retry_config.base_delay * (2 ** attempt)
222234

workos/utils/http_client.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import asyncio
2-
import random
32
import time
43
from types import TracebackType
54
from typing import Optional, Type, Union
@@ -142,18 +141,14 @@ def request(
142141
except Exception as exc:
143142
last_exception = exc
144143
if attempt < retry_config.max_retries and self._should_retry_exception(exc):
145-
delay = retry_config.base_delay * (2 ** attempt)
146-
delay = min(delay, retry_config.max_delay)
147-
jitter_amount = delay * retry_config.jitter * random.random()
148-
time.sleep(delay + jitter_amount)
144+
delay = self._calculate_backoff_delay(attempt, retry_config)
145+
time.sleep(delay)
149146
continue
150147
raise
151148

152-
# Should not reach here, but raise last exception if we do
153149
if last_exception is not None:
154150
raise last_exception
155151

156-
# Fallback: this should never happen
157152
raise RuntimeError("Unexpected state in retry logic")
158153

159154

@@ -279,10 +274,8 @@ async def request(
279274
except Exception as exc:
280275
last_exception = exc
281276
if attempt < retry_config.max_retries and self._should_retry_exception(exc):
282-
delay = retry_config.base_delay * (2 ** attempt)
283-
delay = min(delay, retry_config.max_delay)
284-
jitter_amount = delay * retry_config.jitter * random.random()
285-
await asyncio.sleep(delay + jitter_amount)
277+
delay = self._calculate_backoff_delay(attempt, retry_config)
278+
await asyncio.sleep(delay)
286279
continue
287280
raise
288281

0 commit comments

Comments
 (0)