smoothapi-py 1.5.0__tar.gz → 1.5.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (25) hide show
  1. {smoothapi_py-1.5.0 → smoothapi_py-1.5.1}/.gitignore +2 -0
  2. {smoothapi_py-1.5.0 → smoothapi_py-1.5.1}/PKG-INFO +1 -1
  3. {smoothapi_py-1.5.0 → smoothapi_py-1.5.1}/pyproject.toml +1 -1
  4. {smoothapi_py-1.5.0 → smoothapi_py-1.5.1}/smooth_api/__init__.py +12 -10
  5. {smoothapi_py-1.5.0 → smoothapi_py-1.5.1}/smooth_api/config.py +4 -0
  6. {smoothapi_py-1.5.0 → smoothapi_py-1.5.1}/smooth_api/state.py +11 -0
  7. smoothapi_py-1.5.1/smooth_api/utils.py +50 -0
  8. {smoothapi_py-1.5.0 → smoothapi_py-1.5.1}/tests/test_resilience.py +2 -2
  9. smoothapi_py-1.5.0/smooth_api/utils.py +0 -13
  10. {smoothapi_py-1.5.0 → smoothapi_py-1.5.1}/CHANGELOG.md +0 -0
  11. {smoothapi_py-1.5.0 → smoothapi_py-1.5.1}/LICENSE +0 -0
  12. {smoothapi_py-1.5.0 → smoothapi_py-1.5.1}/README.md +0 -0
  13. {smoothapi_py-1.5.0 → smoothapi_py-1.5.1}/smooth_api/dedup.py +0 -0
  14. {smoothapi_py-1.5.0 → smoothapi_py-1.5.1}/tests/conftest.py +0 -0
  15. {smoothapi_py-1.5.0 → smoothapi_py-1.5.1}/tests/test_backoff.py +0 -0
  16. {smoothapi_py-1.5.0 → smoothapi_py-1.5.1}/tests/test_concurrency.py +0 -0
  17. {smoothapi_py-1.5.0 → smoothapi_py-1.5.1}/tests/test_deduplication.py +0 -0
  18. {smoothapi_py-1.5.0 → smoothapi_py-1.5.1}/tests/test_deprecation.py +0 -0
  19. {smoothapi_py-1.5.0 → smoothapi_py-1.5.1}/tests/test_edge_cases.py +0 -0
  20. {smoothapi_py-1.5.0 → smoothapi_py-1.5.1}/tests/test_enhanced_circuit_breaker.py +0 -0
  21. {smoothapi_py-1.5.0 → smoothapi_py-1.5.1}/tests/test_exceptions.py +0 -0
  22. {smoothapi_py-1.5.0 → smoothapi_py-1.5.1}/tests/test_hooks.py +0 -0
  23. {smoothapi_py-1.5.0 → smoothapi_py-1.5.1}/tests/test_isolation.py +0 -0
  24. {smoothapi_py-1.5.0 → smoothapi_py-1.5.1}/tests/test_mock_response.py +0 -0
  25. {smoothapi_py-1.5.0 → smoothapi_py-1.5.1}/tests/test_timeout.py +0 -0
@@ -41,6 +41,8 @@ Thumbs.db
41
41
  *.swp
42
42
  *.swo
43
43
  .agent
44
+ *.sh
45
+ *.ps1
44
46
 
45
47
  # ─── Rust ──────────────────────────────────────────────────
46
48
  **/target/
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: smoothapi-py
3
- Version: 1.5.0
3
+ Version: 1.5.1
4
4
  Summary: API protection library — exponential backoff and circuit breaker
5
5
  Project-URL: Homepage, https://smoothapi.org
6
6
  Project-URL: Issues, https://github.com/AryanSharma48/smoothAPI/issues
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "smoothapi-py"
7
- version = "1.5.0"
7
+ version = "1.5.1"
8
8
  description = "API protection library — exponential backoff and circuit breaker"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
@@ -108,12 +108,11 @@ def _get_retry_after_delay(err: Exception) -> float | None:
108
108
  if hasattr(err.response, "headers"):
109
109
  retry_after = err.response.headers.get("Retry-After")
110
110
  if retry_after is not None:
111
- try:
112
- delay = float(retry_after)
113
- if delay > 0:
114
- return delay
115
- except ValueError:
116
- pass
111
+ trimmed = str(retry_after).strip()
112
+ if trimmed.isascii() and trimmed.isdigit():
113
+ delay = int(trimmed)
114
+ if delay >= 0:
115
+ return float(delay)
117
116
  return None
118
117
 
119
118
 
@@ -182,6 +181,7 @@ def smooth_api(config: SmoothConfig):
182
181
  # or attach to an already-running Future.
183
182
  async def _execute():
184
183
  last_err: Exception | None = None
184
+ prev_delay: float | None = None
185
185
  attempt_count = 0
186
186
 
187
187
  for attempt in range(config.backoff.max_retries + 1):
@@ -281,11 +281,12 @@ def smooth_api(config: SmoothConfig):
281
281
  breaker.record_failure(key)
282
282
  last_err = err
283
283
  if attempt < config.backoff.max_retries:
284
- delay = calculate_backoff(attempt, config.backoff)
284
+ delay = calculate_backoff(attempt, config.backoff, prev_delay)
285
285
  if status == 429:
286
286
  retry_after_delay = _get_retry_after_delay(err)
287
287
  if retry_after_delay is not None:
288
288
  delay = retry_after_delay
289
+ prev_delay = delay
289
290
  if config.on_retry:
290
291
  ctx = RetryContext(
291
292
  attempt=attempt + 1,
@@ -368,6 +369,7 @@ def smooth_api(config: SmoothConfig):
368
369
  raise RuntimeError(f'Circuit breaker is OPEN for: {key}')
369
370
 
370
371
  last_err: Exception | None = None
372
+ prev_delay: float | None = None
371
373
  attempt_count = 0
372
374
 
373
375
  try:
@@ -451,11 +453,11 @@ def smooth_api(config: SmoothConfig):
451
453
  breaker.record_failure(key)
452
454
  last_err = err
453
455
  if attempt < config.backoff.max_retries:
454
- delay = calculate_backoff(attempt, config.backoff)
455
- if status == 429:
456
- retry_after_delay = _get_retry_after_delay(err)
456
+ delay = calculate_backoff(attempt, config.backoff, prev_delay)
457
+ retry_after_delay = _get_retry_after_delay(err) if status == 429 else None
457
458
  if status == 429 and retry_after_delay is not None:
458
459
  delay = retry_after_delay
460
+ prev_delay = delay
459
461
  if config.on_retry:
460
462
  ctx = RetryContext(
461
463
  attempt=attempt + 1,
@@ -50,11 +50,15 @@ class RetryContext:
50
50
  return self.delay_ms / 1000.0
51
51
 
52
52
 
53
+ JitterStrategy = Literal['none', 'full', 'equal', 'decorrelated']
54
+
55
+
53
56
  @dataclass
54
57
  class BackoffConfig:
55
58
  base_delay: float = 0.1 # seconds, doubles each attempt before jitter
56
59
  max_delay: float = 30.0 # ceiling on the pre-jitter exponential
57
60
  max_retries: int = 3
61
+ jitter: JitterStrategy = 'equal'
58
62
 
59
63
 
60
64
  @dataclass
@@ -216,6 +216,17 @@ class CircuitBreakerState:
216
216
  if event:
217
217
  self._notify_state_change(event)
218
218
 
219
+ def get_stored_state(self, domain: str) -> CircuitState:
220
+ with self._lock:
221
+ return self._get_or_create(domain).state
222
+
223
+ def get_state_at(self, domain: str, now_ms: float) -> CircuitState:
224
+ with self._lock:
225
+ entry = self._get_or_create(domain)
226
+ if entry.state == 'OPEN' and ((now_ms / 1000.0) - entry.last_failure_time) > self._config.cooldown_ms / 1000:
227
+ return 'HALF_OPEN'
228
+ return entry.state
229
+
219
230
  def get_state(self, domain: str) -> CircuitState:
220
231
  with self._lock:
221
232
  entry = self._get_or_create(domain)
@@ -0,0 +1,50 @@
1
+ import random
2
+ import time
3
+
4
+
5
+ def _compute_capped_delay(base_delay: float, max_delay: float, attempt: int) -> float:
6
+ if base_delay <= 0.0 or max_delay <= 0.0:
7
+ return 0.0
8
+ if base_delay >= max_delay:
9
+ return max_delay
10
+ remaining = max(0, int(attempt))
11
+ current = base_delay
12
+ while remaining > 0 and current < max_delay:
13
+ step = min(remaining, 52)
14
+ current *= 2.0 ** step
15
+ remaining -= step
16
+ return min(max_delay, current)
17
+
18
+
19
+ def calculate_backoff(
20
+ attempt: int,
21
+ config,
22
+ prev_delay: float | None = None,
23
+ random_factor: float | None = None,
24
+ ) -> float:
25
+ base_delay = max(0.0, float(config.base_delay))
26
+ max_delay = max(0.0, float(config.max_delay))
27
+ capped = _compute_capped_delay(base_delay, max_delay, attempt)
28
+ if capped == 0.0:
29
+ return 0.0
30
+
31
+ factor = (
32
+ max(0.0, min(1.0, float(random_factor)))
33
+ if random_factor is not None
34
+ else random.random()
35
+ )
36
+ strategy = getattr(config, "jitter", "equal") or "equal"
37
+ if strategy == "none":
38
+ return capped
39
+ if strategy == "full":
40
+ return capped * factor
41
+ if strategy == "decorrelated":
42
+ base = prev_delay if prev_delay is not None else base_delay
43
+ upper_bound = max(base_delay, base * 3.0)
44
+ return min(max_delay, base_delay + (upper_bound - base_delay) * factor)
45
+ half = capped / 2.0
46
+ return half + half * factor
47
+
48
+
49
+ def sleep_backoff(seconds: float) -> None:
50
+ time.sleep(seconds)
@@ -82,7 +82,7 @@ def test_retry_respects_retry_after_header_sync():
82
82
 
83
83
  class MockResponse:
84
84
  status_code = 429
85
- headers = {"Retry-After": "0.1"}
85
+ headers = {"Retry-After": "1"}
86
86
  reason = "Too Many Requests"
87
87
 
88
88
  class MockResponseSuccess:
@@ -124,7 +124,7 @@ async def test_retry_respects_retry_after_header_async():
124
124
 
125
125
  class MockResponse:
126
126
  status_code = 429
127
- headers = {"Retry-After": "0.1"}
127
+ headers = {"Retry-After": "1"}
128
128
  reason = "Too Many Requests"
129
129
 
130
130
  class MockResponseSuccess:
@@ -1,13 +0,0 @@
1
- import time
2
- import random
3
-
4
- def calculate_backoff(attempt: int, config) -> float:
5
- exponential = config.base_delay * (2 ** attempt)
6
- capped = min(config.max_delay, exponential)
7
- return random.uniform(0, capped)
8
-
9
-
10
- def sleep_backoff(seconds: float) -> None:
11
- time.sleep(seconds)
12
-
13
-
File without changes
File without changes
File without changes