smoothapi-py 1.2.0__tar.gz → 1.2.2__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: smoothapi-py
3
- Version: 1.2.0
3
+ Version: 1.2.2
4
4
  Summary: API protection library — exponential backoff and circuit breaker
5
5
  License: MIT
6
6
  License-File: LICENSE
@@ -202,6 +202,7 @@ config = SmoothConfig(
202
202
  2. **Circuit Check:** Before execution, the breaker checks the state. If it's `OPEN`, the request is blocked instantly (returning your fallback, or raising `RuntimeError`).
203
203
  3. **Execution & Retries:** If an exception is raised, it attempts to extract the HTTP status code (supports `requests` and `httpx`). If the status is in `retry_on`, it's counted as a failure and the thread sleeps with backoff.
204
204
  4. **Recovery:** After `cooldown_ms`, the breaker enters `HALF_OPEN`. The next execution acts as a probe. If it succeeds, the circuit closes. If it fails, it snaps back to `OPEN` immediately.
205
+ 5. **Memory Management:** The circuit breaker cache is capped at 1,000 domains. When exceeded, `CLOSED` circuits with zero failures are swept to prevent memory leaks in dynamic environments.
205
206
 
206
207
  ## License
207
208
 
@@ -187,6 +187,7 @@ config = SmoothConfig(
187
187
  2. **Circuit Check:** Before execution, the breaker checks the state. If it's `OPEN`, the request is blocked instantly (returning your fallback, or raising `RuntimeError`).
188
188
  3. **Execution & Retries:** If an exception is raised, it attempts to extract the HTTP status code (supports `requests` and `httpx`). If the status is in `retry_on`, it's counted as a failure and the thread sleeps with backoff.
189
189
  4. **Recovery:** After `cooldown_ms`, the breaker enters `HALF_OPEN`. The next execution acts as a probe. If it succeeds, the circuit closes. If it fails, it snaps back to `OPEN` immediately.
190
+ 5. **Memory Management:** The circuit breaker cache is capped at 1,000 domains. When exceeded, `CLOSED` circuits with zero failures are swept to prevent memory leaks in dynamic environments.
190
191
 
191
192
  ## License
192
193
 
Binary file
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "smoothapi-py"
7
- version = "1.2.0"
7
+ version = "1.2.2"
8
8
  description = "API protection library — exponential backoff and circuit breaker"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
@@ -139,6 +139,11 @@ def smooth_api(config: SmoothConfig):
139
139
  return wrapper
140
140
 
141
141
  else:
142
+ if deduplicator is not None:
143
+ import warnings
144
+ # Sync deduplication requires a thread-safe lock manager, which is currently unsupported
145
+ warnings.warn("Synchronous deduplication is not supported. Deduplication will be ignored for this function.", UserWarning, stacklevel=2)
146
+
142
147
  @functools.wraps(fn)
143
148
  def wrapper(*args, **kwargs): # type: ignore[misc]
144
149
  fallback = kwargs.pop('fallback', config.fallback)
@@ -25,6 +25,7 @@ class CircuitBreakerState:
25
25
  # No lock needed here
26
26
  def _get_or_create(self, domain: str) -> CircuitEntry:
27
27
  if domain not in self._map:
28
+ self._cleanup()
28
29
  self._map[domain] = CircuitEntry(
29
30
  state='CLOSED',
30
31
  failure_count=0,
@@ -32,6 +33,17 @@ class CircuitBreakerState:
32
33
  )
33
34
  return self._map[domain]
34
35
 
36
+ def _cleanup(self) -> None:
37
+ # Enforce a strict domain limit to bound memory usage
38
+ if len(self._map) <= 1000:
39
+ return
40
+ keys_to_delete = [
41
+ k for k, v in self._map.items()
42
+ if v.state == 'CLOSED' and v.failure_count == 0
43
+ ]
44
+ for k in keys_to_delete:
45
+ del self._map[k]
46
+
35
47
  def can_request(self, domain: str) -> bool:
36
48
  with self._lock:
37
49
  entry = self._get_or_create(domain)
File without changes
File without changes