crashbytes-resilience 1.0.0__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.
- crashbytes_resilience-1.0.0/.github/CODEOWNERS +1 -0
- crashbytes_resilience-1.0.0/.github/workflows/ci.yml +23 -0
- crashbytes_resilience-1.0.0/.github/workflows/publish.yml +19 -0
- crashbytes_resilience-1.0.0/.gitignore +11 -0
- crashbytes_resilience-1.0.0/LICENSE +21 -0
- crashbytes_resilience-1.0.0/PKG-INFO +83 -0
- crashbytes_resilience-1.0.0/README.md +54 -0
- crashbytes_resilience-1.0.0/pyproject.toml +54 -0
- crashbytes_resilience-1.0.0/src/crashbytes_resilience/__init__.py +35 -0
- crashbytes_resilience-1.0.0/src/crashbytes_resilience/_core.py +381 -0
- crashbytes_resilience-1.0.0/src/crashbytes_resilience/py.typed +0 -0
- crashbytes_resilience-1.0.0/tests/test_resilience.py +390 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
* @CrashBytes
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [main]
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
test:
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
strategy:
|
|
13
|
+
matrix:
|
|
14
|
+
python-version: ["3.10", "3.11", "3.12", "3.13"]
|
|
15
|
+
steps:
|
|
16
|
+
- uses: actions/checkout@v4
|
|
17
|
+
- uses: actions/setup-python@v5
|
|
18
|
+
with:
|
|
19
|
+
python-version: ${{ matrix.python-version }}
|
|
20
|
+
- run: pip install -e ".[dev]"
|
|
21
|
+
- run: ruff check src/ tests/
|
|
22
|
+
- run: mypy --strict src/
|
|
23
|
+
- run: pytest --cov=crashbytes_resilience --cov-branch --cov-fail-under=90
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
name: Publish to PyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags: ["*"]
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
publish:
|
|
9
|
+
runs-on: ubuntu-latest
|
|
10
|
+
permissions:
|
|
11
|
+
id-token: write
|
|
12
|
+
steps:
|
|
13
|
+
- uses: actions/checkout@v4
|
|
14
|
+
- uses: actions/setup-python@v5
|
|
15
|
+
with:
|
|
16
|
+
python-version: "3.12"
|
|
17
|
+
- run: pip install build
|
|
18
|
+
- run: python -m build
|
|
19
|
+
- uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 CrashBytes
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: crashbytes-resilience
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Unified resilience patterns — retry, circuit breaker, rate limiter, timeout, bulkhead, fallback.
|
|
5
|
+
Project-URL: Homepage, https://github.com/CrashBytes/crashbytes-resilience
|
|
6
|
+
Project-URL: Repository, https://github.com/CrashBytes/crashbytes-resilience
|
|
7
|
+
Project-URL: Issues, https://github.com/CrashBytes/crashbytes-resilience/issues
|
|
8
|
+
Author-email: CrashBytes <crashbytes@users.noreply.github.com>
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: bulkhead,circuit-breaker,rate-limiter,resilience,retry,timeout
|
|
12
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Typing :: Typed
|
|
21
|
+
Requires-Python: >=3.10
|
|
22
|
+
Provides-Extra: dev
|
|
23
|
+
Requires-Dist: mypy; extra == 'dev'
|
|
24
|
+
Requires-Dist: pytest; extra == 'dev'
|
|
25
|
+
Requires-Dist: pytest-asyncio; extra == 'dev'
|
|
26
|
+
Requires-Dist: pytest-cov; extra == 'dev'
|
|
27
|
+
Requires-Dist: ruff; extra == 'dev'
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
|
|
30
|
+
# crashbytes-resilience
|
|
31
|
+
|
|
32
|
+
Unified resilience patterns for Python — retry, circuit breaker, rate limiter, timeout, bulkhead, fallback. Zero dependencies. Sync + async. Thread-safe.
|
|
33
|
+
|
|
34
|
+
## Install
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
pip install crashbytes-resilience
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Usage
|
|
41
|
+
|
|
42
|
+
```python
|
|
43
|
+
from crashbytes_resilience import retry, circuit_breaker, timeout, fallback, pipeline
|
|
44
|
+
|
|
45
|
+
@retry(max_attempts=3, delay=0.5, backoff=2.0)
|
|
46
|
+
def fetch_data():
|
|
47
|
+
...
|
|
48
|
+
|
|
49
|
+
@circuit_breaker(failure_threshold=5, recovery_timeout=30)
|
|
50
|
+
def call_service():
|
|
51
|
+
...
|
|
52
|
+
|
|
53
|
+
@timeout(5.0)
|
|
54
|
+
async def slow_operation():
|
|
55
|
+
...
|
|
56
|
+
|
|
57
|
+
@fallback(lambda: {"cached": True})
|
|
58
|
+
def get_config():
|
|
59
|
+
...
|
|
60
|
+
|
|
61
|
+
# Compose patterns
|
|
62
|
+
@pipeline(retry(max_attempts=3, delay=0.1), timeout(5.0), fallback(lambda: None))
|
|
63
|
+
def resilient_call():
|
|
64
|
+
...
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Patterns
|
|
68
|
+
|
|
69
|
+
| Decorator | Description |
|
|
70
|
+
|-----------|-------------|
|
|
71
|
+
| `@retry()` | Retry with exponential backoff |
|
|
72
|
+
| `@circuit_breaker()` | Stop calling failing services |
|
|
73
|
+
| `@rate_limiter()` | Token-bucket rate limiting |
|
|
74
|
+
| `@timeout()` | Time-limit operations |
|
|
75
|
+
| `@bulkhead()` | Limit concurrent executions |
|
|
76
|
+
| `@fallback()` | Provide fallback on failure |
|
|
77
|
+
| `@pipeline()` | Compose multiple patterns |
|
|
78
|
+
|
|
79
|
+
All patterns work with both sync and async functions.
|
|
80
|
+
|
|
81
|
+
## License
|
|
82
|
+
|
|
83
|
+
MIT
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# crashbytes-resilience
|
|
2
|
+
|
|
3
|
+
Unified resilience patterns for Python — retry, circuit breaker, rate limiter, timeout, bulkhead, fallback. Zero dependencies. Sync + async. Thread-safe.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install crashbytes-resilience
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```python
|
|
14
|
+
from crashbytes_resilience import retry, circuit_breaker, timeout, fallback, pipeline
|
|
15
|
+
|
|
16
|
+
@retry(max_attempts=3, delay=0.5, backoff=2.0)
|
|
17
|
+
def fetch_data():
|
|
18
|
+
...
|
|
19
|
+
|
|
20
|
+
@circuit_breaker(failure_threshold=5, recovery_timeout=30)
|
|
21
|
+
def call_service():
|
|
22
|
+
...
|
|
23
|
+
|
|
24
|
+
@timeout(5.0)
|
|
25
|
+
async def slow_operation():
|
|
26
|
+
...
|
|
27
|
+
|
|
28
|
+
@fallback(lambda: {"cached": True})
|
|
29
|
+
def get_config():
|
|
30
|
+
...
|
|
31
|
+
|
|
32
|
+
# Compose patterns
|
|
33
|
+
@pipeline(retry(max_attempts=3, delay=0.1), timeout(5.0), fallback(lambda: None))
|
|
34
|
+
def resilient_call():
|
|
35
|
+
...
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Patterns
|
|
39
|
+
|
|
40
|
+
| Decorator | Description |
|
|
41
|
+
|-----------|-------------|
|
|
42
|
+
| `@retry()` | Retry with exponential backoff |
|
|
43
|
+
| `@circuit_breaker()` | Stop calling failing services |
|
|
44
|
+
| `@rate_limiter()` | Token-bucket rate limiting |
|
|
45
|
+
| `@timeout()` | Time-limit operations |
|
|
46
|
+
| `@bulkhead()` | Limit concurrent executions |
|
|
47
|
+
| `@fallback()` | Provide fallback on failure |
|
|
48
|
+
| `@pipeline()` | Compose multiple patterns |
|
|
49
|
+
|
|
50
|
+
All patterns work with both sync and async functions.
|
|
51
|
+
|
|
52
|
+
## License
|
|
53
|
+
|
|
54
|
+
MIT
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "crashbytes-resilience"
|
|
7
|
+
version = "1.0.0"
|
|
8
|
+
description = "Unified resilience patterns — retry, circuit breaker, rate limiter, timeout, bulkhead, fallback."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
|
+
authors = [{ name = "CrashBytes", email = "crashbytes@users.noreply.github.com" }]
|
|
13
|
+
keywords = ["resilience", "retry", "circuit-breaker", "rate-limiter", "timeout", "bulkhead"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 5 - Production/Stable",
|
|
16
|
+
"Intended Audience :: Developers",
|
|
17
|
+
"License :: OSI Approved :: MIT License",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Programming Language :: Python :: 3.10",
|
|
20
|
+
"Programming Language :: Python :: 3.11",
|
|
21
|
+
"Programming Language :: Python :: 3.12",
|
|
22
|
+
"Programming Language :: Python :: 3.13",
|
|
23
|
+
"Typing :: Typed",
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
[project.optional-dependencies]
|
|
27
|
+
dev = ["pytest", "pytest-cov", "pytest-asyncio", "mypy", "ruff"]
|
|
28
|
+
|
|
29
|
+
[project.urls]
|
|
30
|
+
Homepage = "https://github.com/CrashBytes/crashbytes-resilience"
|
|
31
|
+
Repository = "https://github.com/CrashBytes/crashbytes-resilience"
|
|
32
|
+
Issues = "https://github.com/CrashBytes/crashbytes-resilience/issues"
|
|
33
|
+
|
|
34
|
+
[tool.ruff]
|
|
35
|
+
target-version = "py310"
|
|
36
|
+
line-length = 99
|
|
37
|
+
|
|
38
|
+
[tool.ruff.lint]
|
|
39
|
+
select = ["E", "F", "I", "N", "UP", "B", "SIM", "TCH"]
|
|
40
|
+
|
|
41
|
+
[tool.mypy]
|
|
42
|
+
strict = true
|
|
43
|
+
python_version = "3.10"
|
|
44
|
+
|
|
45
|
+
[tool.pytest.ini_options]
|
|
46
|
+
testpaths = ["tests"]
|
|
47
|
+
asyncio_mode = "auto"
|
|
48
|
+
|
|
49
|
+
[tool.coverage.run]
|
|
50
|
+
branch = true
|
|
51
|
+
source = ["crashbytes_resilience"]
|
|
52
|
+
|
|
53
|
+
[tool.coverage.report]
|
|
54
|
+
fail_under = 90
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"""crashbytes-resilience — Unified resilience patterns for Python."""
|
|
2
|
+
|
|
3
|
+
from crashbytes_resilience._core import (
|
|
4
|
+
BulkheadFullError,
|
|
5
|
+
CircuitBreaker,
|
|
6
|
+
CircuitOpenError,
|
|
7
|
+
RateLimiter,
|
|
8
|
+
ResilienceError,
|
|
9
|
+
RetriesExhaustedError,
|
|
10
|
+
TimeoutExceededError,
|
|
11
|
+
bulkhead,
|
|
12
|
+
circuit_breaker,
|
|
13
|
+
fallback,
|
|
14
|
+
pipeline,
|
|
15
|
+
rate_limiter,
|
|
16
|
+
retry,
|
|
17
|
+
timeout,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
__all__ = [
|
|
21
|
+
"BulkheadFullError",
|
|
22
|
+
"CircuitBreaker",
|
|
23
|
+
"CircuitOpenError",
|
|
24
|
+
"RateLimiter",
|
|
25
|
+
"ResilienceError",
|
|
26
|
+
"RetriesExhaustedError",
|
|
27
|
+
"TimeoutExceededError",
|
|
28
|
+
"bulkhead",
|
|
29
|
+
"circuit_breaker",
|
|
30
|
+
"fallback",
|
|
31
|
+
"pipeline",
|
|
32
|
+
"rate_limiter",
|
|
33
|
+
"retry",
|
|
34
|
+
"timeout",
|
|
35
|
+
]
|
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
"""Unified resilience patterns for Python."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import asyncio
|
|
6
|
+
import functools
|
|
7
|
+
import inspect
|
|
8
|
+
import threading
|
|
9
|
+
import time
|
|
10
|
+
from typing import Any, TypeVar
|
|
11
|
+
|
|
12
|
+
T = TypeVar("T")
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
# ── Exceptions ──────────────────────────────────────────────────────
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class ResilienceError(Exception):
|
|
19
|
+
"""Base for all resilience errors."""
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class CircuitOpenError(ResilienceError):
|
|
23
|
+
"""Raised when the circuit breaker is open."""
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class BulkheadFullError(ResilienceError):
|
|
27
|
+
"""Raised when the bulkhead is at capacity."""
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class TimeoutExceededError(ResilienceError):
|
|
31
|
+
"""Raised when an operation exceeds its timeout."""
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class RetriesExhaustedError(ResilienceError):
|
|
35
|
+
"""Raised when all retry attempts fail."""
|
|
36
|
+
|
|
37
|
+
def __init__(self, last_exception: Exception) -> None:
|
|
38
|
+
self.last_exception = last_exception
|
|
39
|
+
super().__init__(f"All retries exhausted. Last error: {last_exception}")
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
# ── Retry ───────────────────────────────────────────────────────────
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def retry(
|
|
46
|
+
max_attempts: int = 3,
|
|
47
|
+
delay: float = 0.1,
|
|
48
|
+
backoff: float = 2.0,
|
|
49
|
+
exceptions: tuple[type[Exception], ...] = (Exception,),
|
|
50
|
+
) -> Any:
|
|
51
|
+
"""Decorator: retry on failure with exponential backoff.
|
|
52
|
+
|
|
53
|
+
Works with both sync and async functions.
|
|
54
|
+
"""
|
|
55
|
+
|
|
56
|
+
def decorator(fn: Any) -> Any:
|
|
57
|
+
if inspect.iscoroutinefunction(fn):
|
|
58
|
+
|
|
59
|
+
@functools.wraps(fn)
|
|
60
|
+
async def async_wrapper(*args: Any, **kwargs: Any) -> Any:
|
|
61
|
+
current_delay = delay
|
|
62
|
+
last_exc: Exception | None = None
|
|
63
|
+
for attempt in range(max_attempts):
|
|
64
|
+
try:
|
|
65
|
+
return await fn(*args, **kwargs)
|
|
66
|
+
except exceptions as exc:
|
|
67
|
+
last_exc = exc
|
|
68
|
+
if attempt < max_attempts - 1:
|
|
69
|
+
await asyncio.sleep(current_delay)
|
|
70
|
+
current_delay *= backoff
|
|
71
|
+
assert last_exc is not None
|
|
72
|
+
raise RetriesExhaustedError(last_exc)
|
|
73
|
+
|
|
74
|
+
return async_wrapper
|
|
75
|
+
|
|
76
|
+
@functools.wraps(fn)
|
|
77
|
+
def sync_wrapper(*args: Any, **kwargs: Any) -> Any:
|
|
78
|
+
current_delay = delay
|
|
79
|
+
last_exc: Exception | None = None
|
|
80
|
+
for attempt in range(max_attempts):
|
|
81
|
+
try:
|
|
82
|
+
return fn(*args, **kwargs)
|
|
83
|
+
except exceptions as exc:
|
|
84
|
+
last_exc = exc
|
|
85
|
+
if attempt < max_attempts - 1:
|
|
86
|
+
time.sleep(current_delay)
|
|
87
|
+
current_delay *= backoff
|
|
88
|
+
assert last_exc is not None
|
|
89
|
+
raise RetriesExhaustedError(last_exc)
|
|
90
|
+
|
|
91
|
+
return sync_wrapper
|
|
92
|
+
|
|
93
|
+
return decorator
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
# ── Circuit Breaker ─────────────────────────────────────────────────
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
class CircuitBreaker:
|
|
100
|
+
"""Thread-safe circuit breaker.
|
|
101
|
+
|
|
102
|
+
States: CLOSED -> OPEN -> HALF_OPEN -> CLOSED (or back to OPEN).
|
|
103
|
+
"""
|
|
104
|
+
|
|
105
|
+
CLOSED = "closed"
|
|
106
|
+
OPEN = "open"
|
|
107
|
+
HALF_OPEN = "half_open"
|
|
108
|
+
|
|
109
|
+
def __init__(
|
|
110
|
+
self,
|
|
111
|
+
failure_threshold: int = 5,
|
|
112
|
+
recovery_timeout: float = 30.0,
|
|
113
|
+
exceptions: tuple[type[Exception], ...] = (Exception,),
|
|
114
|
+
) -> None:
|
|
115
|
+
self._failure_threshold = failure_threshold
|
|
116
|
+
self._recovery_timeout = recovery_timeout
|
|
117
|
+
self._exceptions = exceptions
|
|
118
|
+
self._state = self.CLOSED
|
|
119
|
+
self._failure_count = 0
|
|
120
|
+
self._last_failure_time = 0.0
|
|
121
|
+
self._lock = threading.Lock()
|
|
122
|
+
|
|
123
|
+
@property
|
|
124
|
+
def state(self) -> str:
|
|
125
|
+
with self._lock:
|
|
126
|
+
if (
|
|
127
|
+
self._state == self.OPEN
|
|
128
|
+
and time.monotonic() - self._last_failure_time >= self._recovery_timeout
|
|
129
|
+
):
|
|
130
|
+
self._state = self.HALF_OPEN
|
|
131
|
+
return self._state
|
|
132
|
+
|
|
133
|
+
def _record_success(self) -> None:
|
|
134
|
+
with self._lock:
|
|
135
|
+
self._failure_count = 0
|
|
136
|
+
self._state = self.CLOSED
|
|
137
|
+
|
|
138
|
+
def _record_failure(self) -> None:
|
|
139
|
+
with self._lock:
|
|
140
|
+
self._failure_count += 1
|
|
141
|
+
self._last_failure_time = time.monotonic()
|
|
142
|
+
if self._failure_count >= self._failure_threshold:
|
|
143
|
+
self._state = self.OPEN
|
|
144
|
+
|
|
145
|
+
def __call__(self, fn: Any) -> Any:
|
|
146
|
+
if inspect.iscoroutinefunction(fn):
|
|
147
|
+
|
|
148
|
+
@functools.wraps(fn)
|
|
149
|
+
async def async_wrapper(*args: Any, **kwargs: Any) -> Any:
|
|
150
|
+
if self.state == self.OPEN:
|
|
151
|
+
raise CircuitOpenError("Circuit breaker is open")
|
|
152
|
+
try:
|
|
153
|
+
result = await fn(*args, **kwargs)
|
|
154
|
+
except self._exceptions:
|
|
155
|
+
self._record_failure()
|
|
156
|
+
raise
|
|
157
|
+
self._record_success()
|
|
158
|
+
return result
|
|
159
|
+
|
|
160
|
+
return async_wrapper
|
|
161
|
+
|
|
162
|
+
@functools.wraps(fn)
|
|
163
|
+
def sync_wrapper(*args: Any, **kwargs: Any) -> Any:
|
|
164
|
+
if self.state == self.OPEN:
|
|
165
|
+
raise CircuitOpenError("Circuit breaker is open")
|
|
166
|
+
try:
|
|
167
|
+
result = fn(*args, **kwargs)
|
|
168
|
+
except self._exceptions:
|
|
169
|
+
self._record_failure()
|
|
170
|
+
raise
|
|
171
|
+
self._record_success()
|
|
172
|
+
return result
|
|
173
|
+
|
|
174
|
+
return sync_wrapper
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def circuit_breaker(
|
|
178
|
+
failure_threshold: int = 5,
|
|
179
|
+
recovery_timeout: float = 30.0,
|
|
180
|
+
exceptions: tuple[type[Exception], ...] = (Exception,),
|
|
181
|
+
) -> CircuitBreaker:
|
|
182
|
+
"""Create a circuit breaker decorator."""
|
|
183
|
+
return CircuitBreaker(failure_threshold, recovery_timeout, exceptions)
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
# ── Rate Limiter ────────────────────────────────────────────────────
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
class RateLimiter:
|
|
190
|
+
"""Thread-safe token-bucket rate limiter."""
|
|
191
|
+
|
|
192
|
+
def __init__(self, max_calls: int, period: float = 1.0) -> None:
|
|
193
|
+
self._max_calls = max_calls
|
|
194
|
+
self._period = period
|
|
195
|
+
self._tokens = float(max_calls)
|
|
196
|
+
self._last_refill = time.monotonic()
|
|
197
|
+
self._lock = threading.Lock()
|
|
198
|
+
|
|
199
|
+
def _refill(self) -> None:
|
|
200
|
+
now = time.monotonic()
|
|
201
|
+
elapsed = now - self._last_refill
|
|
202
|
+
refill = self._tokens + elapsed * self._max_calls / self._period
|
|
203
|
+
self._tokens = min(self._max_calls, refill)
|
|
204
|
+
self._last_refill = now
|
|
205
|
+
|
|
206
|
+
def acquire(self) -> bool:
|
|
207
|
+
"""Try to acquire a token. Returns True if allowed."""
|
|
208
|
+
with self._lock:
|
|
209
|
+
self._refill()
|
|
210
|
+
if self._tokens >= 1.0:
|
|
211
|
+
self._tokens -= 1.0
|
|
212
|
+
return True
|
|
213
|
+
return False
|
|
214
|
+
|
|
215
|
+
def __call__(self, fn: Any) -> Any:
|
|
216
|
+
if inspect.iscoroutinefunction(fn):
|
|
217
|
+
|
|
218
|
+
@functools.wraps(fn)
|
|
219
|
+
async def async_wrapper(*args: Any, **kwargs: Any) -> Any:
|
|
220
|
+
if not self.acquire():
|
|
221
|
+
raise ResilienceError("Rate limit exceeded")
|
|
222
|
+
return await fn(*args, **kwargs)
|
|
223
|
+
|
|
224
|
+
return async_wrapper
|
|
225
|
+
|
|
226
|
+
@functools.wraps(fn)
|
|
227
|
+
def sync_wrapper(*args: Any, **kwargs: Any) -> Any:
|
|
228
|
+
if not self.acquire():
|
|
229
|
+
raise ResilienceError("Rate limit exceeded")
|
|
230
|
+
return fn(*args, **kwargs)
|
|
231
|
+
|
|
232
|
+
return sync_wrapper
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
def rate_limiter(max_calls: int, period: float = 1.0) -> RateLimiter:
|
|
236
|
+
"""Create a rate limiter decorator."""
|
|
237
|
+
return RateLimiter(max_calls, period)
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
# ── Timeout ─────────────────────────────────────────────────────────
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
def timeout(seconds: float) -> Any:
|
|
244
|
+
"""Decorator: raise TimeoutError_ if the function exceeds *seconds*.
|
|
245
|
+
|
|
246
|
+
For async functions, uses asyncio.wait_for.
|
|
247
|
+
For sync functions, uses threading.
|
|
248
|
+
"""
|
|
249
|
+
|
|
250
|
+
def decorator(fn: Any) -> Any:
|
|
251
|
+
if inspect.iscoroutinefunction(fn):
|
|
252
|
+
|
|
253
|
+
@functools.wraps(fn)
|
|
254
|
+
async def async_wrapper(*args: Any, **kwargs: Any) -> Any:
|
|
255
|
+
try:
|
|
256
|
+
return await asyncio.wait_for(fn(*args, **kwargs), timeout=seconds)
|
|
257
|
+
except asyncio.TimeoutError:
|
|
258
|
+
raise TimeoutExceededError(f"Operation timed out after {seconds}s") from None
|
|
259
|
+
|
|
260
|
+
return async_wrapper
|
|
261
|
+
|
|
262
|
+
@functools.wraps(fn)
|
|
263
|
+
def sync_wrapper(*args: Any, **kwargs: Any) -> Any:
|
|
264
|
+
result_container: list[Any] = []
|
|
265
|
+
exc_container: list[Exception] = []
|
|
266
|
+
|
|
267
|
+
def target() -> None:
|
|
268
|
+
try:
|
|
269
|
+
result_container.append(fn(*args, **kwargs))
|
|
270
|
+
except Exception as e:
|
|
271
|
+
exc_container.append(e)
|
|
272
|
+
|
|
273
|
+
thread = threading.Thread(target=target)
|
|
274
|
+
thread.start()
|
|
275
|
+
thread.join(timeout=seconds)
|
|
276
|
+
if thread.is_alive():
|
|
277
|
+
raise TimeoutExceededError(f"Operation timed out after {seconds}s")
|
|
278
|
+
if exc_container:
|
|
279
|
+
raise exc_container[0]
|
|
280
|
+
return result_container[0]
|
|
281
|
+
|
|
282
|
+
return sync_wrapper
|
|
283
|
+
|
|
284
|
+
return decorator
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
# ── Bulkhead ────────────────────────────────────────────────────────
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
def bulkhead(max_concurrent: int) -> Any:
|
|
291
|
+
"""Decorator: limit concurrent executions.
|
|
292
|
+
|
|
293
|
+
Uses threading.Semaphore for sync, asyncio.Semaphore for async.
|
|
294
|
+
"""
|
|
295
|
+
sync_sem = threading.Semaphore(max_concurrent)
|
|
296
|
+
async_sem: asyncio.Semaphore | None = None
|
|
297
|
+
|
|
298
|
+
def decorator(fn: Any) -> Any:
|
|
299
|
+
if inspect.iscoroutinefunction(fn):
|
|
300
|
+
|
|
301
|
+
@functools.wraps(fn)
|
|
302
|
+
async def async_wrapper(*args: Any, **kwargs: Any) -> Any:
|
|
303
|
+
nonlocal async_sem
|
|
304
|
+
if async_sem is None:
|
|
305
|
+
async_sem = asyncio.Semaphore(max_concurrent)
|
|
306
|
+
acquired = async_sem._value > 0 # noqa: SLF001
|
|
307
|
+
if not acquired:
|
|
308
|
+
raise BulkheadFullError(
|
|
309
|
+
f"Bulkhead full (max {max_concurrent} concurrent)"
|
|
310
|
+
)
|
|
311
|
+
async with async_sem:
|
|
312
|
+
return await fn(*args, **kwargs)
|
|
313
|
+
|
|
314
|
+
return async_wrapper
|
|
315
|
+
|
|
316
|
+
@functools.wraps(fn)
|
|
317
|
+
def sync_wrapper(*args: Any, **kwargs: Any) -> Any:
|
|
318
|
+
acquired = sync_sem.acquire(blocking=False)
|
|
319
|
+
if not acquired:
|
|
320
|
+
raise BulkheadFullError(
|
|
321
|
+
f"Bulkhead full (max {max_concurrent} concurrent)"
|
|
322
|
+
)
|
|
323
|
+
try:
|
|
324
|
+
return fn(*args, **kwargs)
|
|
325
|
+
finally:
|
|
326
|
+
sync_sem.release()
|
|
327
|
+
|
|
328
|
+
return sync_wrapper
|
|
329
|
+
|
|
330
|
+
return decorator
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
# ── Fallback ────────────────────────────────────────────────────────
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
def fallback(fallback_fn: Any) -> Any:
|
|
337
|
+
"""Decorator: call *fallback_fn* if the wrapped function raises."""
|
|
338
|
+
|
|
339
|
+
def decorator(fn: Any) -> Any:
|
|
340
|
+
if inspect.iscoroutinefunction(fn):
|
|
341
|
+
|
|
342
|
+
@functools.wraps(fn)
|
|
343
|
+
async def async_wrapper(*args: Any, **kwargs: Any) -> Any:
|
|
344
|
+
try:
|
|
345
|
+
return await fn(*args, **kwargs)
|
|
346
|
+
except Exception:
|
|
347
|
+
if inspect.iscoroutinefunction(fallback_fn):
|
|
348
|
+
return await fallback_fn(*args, **kwargs)
|
|
349
|
+
return fallback_fn(*args, **kwargs)
|
|
350
|
+
|
|
351
|
+
return async_wrapper
|
|
352
|
+
|
|
353
|
+
@functools.wraps(fn)
|
|
354
|
+
def sync_wrapper(*args: Any, **kwargs: Any) -> Any:
|
|
355
|
+
try:
|
|
356
|
+
return fn(*args, **kwargs)
|
|
357
|
+
except Exception:
|
|
358
|
+
return fallback_fn(*args, **kwargs)
|
|
359
|
+
|
|
360
|
+
return sync_wrapper
|
|
361
|
+
|
|
362
|
+
return decorator
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
# ── Pipeline ────────────────────────────────────────────────────────
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
def pipeline(*decorators: Any) -> Any:
|
|
369
|
+
"""Compose multiple resilience decorators into a single decorator.
|
|
370
|
+
|
|
371
|
+
Applied inside-out: ``pipeline(retry(), circuit_breaker())`` means
|
|
372
|
+
the circuit breaker wraps the retry which wraps the function.
|
|
373
|
+
"""
|
|
374
|
+
|
|
375
|
+
def decorator(fn: Any) -> Any:
|
|
376
|
+
result = fn
|
|
377
|
+
for dec in decorators:
|
|
378
|
+
result = dec(result)
|
|
379
|
+
return result
|
|
380
|
+
|
|
381
|
+
return decorator
|
|
File without changes
|
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
"""Tests for crashbytes-resilience."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import asyncio
|
|
6
|
+
import functools
|
|
7
|
+
import threading
|
|
8
|
+
import time
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
import pytest
|
|
12
|
+
|
|
13
|
+
from crashbytes_resilience import (
|
|
14
|
+
BulkheadFullError,
|
|
15
|
+
CircuitBreaker,
|
|
16
|
+
CircuitOpenError,
|
|
17
|
+
RateLimiter,
|
|
18
|
+
ResilienceError,
|
|
19
|
+
RetriesExhaustedError,
|
|
20
|
+
TimeoutExceededError,
|
|
21
|
+
bulkhead,
|
|
22
|
+
circuit_breaker,
|
|
23
|
+
fallback,
|
|
24
|
+
pipeline,
|
|
25
|
+
rate_limiter,
|
|
26
|
+
retry,
|
|
27
|
+
timeout,
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
# ── Retry ───────────────────────────────────────────────────────────
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class TestRetry:
|
|
34
|
+
def test_succeeds_first_try(self) -> None:
|
|
35
|
+
@retry(max_attempts=3, delay=0)
|
|
36
|
+
def succeed() -> str:
|
|
37
|
+
return "ok"
|
|
38
|
+
|
|
39
|
+
assert succeed() == "ok"
|
|
40
|
+
|
|
41
|
+
def test_retries_then_succeeds(self) -> None:
|
|
42
|
+
call_count = 0
|
|
43
|
+
|
|
44
|
+
@retry(max_attempts=3, delay=0)
|
|
45
|
+
def flaky() -> str:
|
|
46
|
+
nonlocal call_count
|
|
47
|
+
call_count += 1
|
|
48
|
+
if call_count < 3:
|
|
49
|
+
raise ValueError("fail")
|
|
50
|
+
return "ok"
|
|
51
|
+
|
|
52
|
+
assert flaky() == "ok"
|
|
53
|
+
assert call_count == 3
|
|
54
|
+
|
|
55
|
+
def test_exhausted(self) -> None:
|
|
56
|
+
@retry(max_attempts=2, delay=0)
|
|
57
|
+
def always_fail() -> None:
|
|
58
|
+
raise ValueError("nope")
|
|
59
|
+
|
|
60
|
+
with pytest.raises(RetriesExhaustedError, match="nope"):
|
|
61
|
+
always_fail()
|
|
62
|
+
|
|
63
|
+
def test_specific_exceptions(self) -> None:
|
|
64
|
+
@retry(max_attempts=3, delay=0, exceptions=(ValueError,))
|
|
65
|
+
def raise_type() -> None:
|
|
66
|
+
raise TypeError("wrong")
|
|
67
|
+
|
|
68
|
+
with pytest.raises(TypeError):
|
|
69
|
+
raise_type()
|
|
70
|
+
|
|
71
|
+
def test_backoff_delay(self) -> None:
|
|
72
|
+
times: list[float] = []
|
|
73
|
+
|
|
74
|
+
@retry(max_attempts=3, delay=0.05, backoff=2.0)
|
|
75
|
+
def track_time() -> None:
|
|
76
|
+
times.append(time.monotonic())
|
|
77
|
+
raise ValueError("fail")
|
|
78
|
+
|
|
79
|
+
with pytest.raises(RetriesExhaustedError):
|
|
80
|
+
track_time()
|
|
81
|
+
|
|
82
|
+
assert len(times) == 3
|
|
83
|
+
# First delay ~0.05s, second ~0.1s
|
|
84
|
+
assert times[1] - times[0] >= 0.04
|
|
85
|
+
assert times[2] - times[1] >= 0.08
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
class TestRetryAsync:
|
|
89
|
+
async def test_async_retry(self) -> None:
|
|
90
|
+
call_count = 0
|
|
91
|
+
|
|
92
|
+
@retry(max_attempts=3, delay=0)
|
|
93
|
+
async def flaky() -> str:
|
|
94
|
+
nonlocal call_count
|
|
95
|
+
call_count += 1
|
|
96
|
+
if call_count < 2:
|
|
97
|
+
raise ValueError("fail")
|
|
98
|
+
return "ok"
|
|
99
|
+
|
|
100
|
+
result = await flaky()
|
|
101
|
+
assert result == "ok"
|
|
102
|
+
assert call_count == 2
|
|
103
|
+
|
|
104
|
+
async def test_async_exhausted(self) -> None:
|
|
105
|
+
@retry(max_attempts=2, delay=0)
|
|
106
|
+
async def always_fail() -> None:
|
|
107
|
+
raise ValueError("nope")
|
|
108
|
+
|
|
109
|
+
with pytest.raises(RetriesExhaustedError):
|
|
110
|
+
await always_fail()
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
# ── Circuit Breaker ─────────────────────────────────────────────────
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
class TestCircuitBreaker:
|
|
117
|
+
def test_closes_on_success(self) -> None:
|
|
118
|
+
cb = circuit_breaker(failure_threshold=3)
|
|
119
|
+
|
|
120
|
+
@cb
|
|
121
|
+
def success() -> str:
|
|
122
|
+
return "ok"
|
|
123
|
+
|
|
124
|
+
assert success() == "ok"
|
|
125
|
+
assert cb.state == CircuitBreaker.CLOSED
|
|
126
|
+
|
|
127
|
+
def test_opens_after_threshold(self) -> None:
|
|
128
|
+
cb = circuit_breaker(failure_threshold=2, recovery_timeout=10)
|
|
129
|
+
|
|
130
|
+
@cb
|
|
131
|
+
def fail() -> None:
|
|
132
|
+
raise ValueError("fail")
|
|
133
|
+
|
|
134
|
+
for _ in range(2):
|
|
135
|
+
with pytest.raises(ValueError):
|
|
136
|
+
fail()
|
|
137
|
+
|
|
138
|
+
assert cb.state == CircuitBreaker.OPEN
|
|
139
|
+
|
|
140
|
+
with pytest.raises(CircuitOpenError):
|
|
141
|
+
fail()
|
|
142
|
+
|
|
143
|
+
def test_half_open_recovery(self) -> None:
|
|
144
|
+
cb = circuit_breaker(failure_threshold=1, recovery_timeout=0.05)
|
|
145
|
+
|
|
146
|
+
call_count = 0
|
|
147
|
+
|
|
148
|
+
@cb
|
|
149
|
+
def flaky() -> str:
|
|
150
|
+
nonlocal call_count
|
|
151
|
+
call_count += 1
|
|
152
|
+
if call_count == 1:
|
|
153
|
+
raise ValueError("fail")
|
|
154
|
+
return "ok"
|
|
155
|
+
|
|
156
|
+
with pytest.raises(ValueError):
|
|
157
|
+
flaky()
|
|
158
|
+
|
|
159
|
+
assert cb.state == CircuitBreaker.OPEN
|
|
160
|
+
time.sleep(0.06)
|
|
161
|
+
assert cb.state == CircuitBreaker.HALF_OPEN
|
|
162
|
+
|
|
163
|
+
assert flaky() == "ok"
|
|
164
|
+
assert cb.state == CircuitBreaker.CLOSED
|
|
165
|
+
|
|
166
|
+
async def test_async_circuit_breaker(self) -> None:
|
|
167
|
+
cb = circuit_breaker(failure_threshold=1)
|
|
168
|
+
|
|
169
|
+
@cb
|
|
170
|
+
async def fail() -> None:
|
|
171
|
+
raise ValueError("fail")
|
|
172
|
+
|
|
173
|
+
with pytest.raises(ValueError):
|
|
174
|
+
await fail()
|
|
175
|
+
|
|
176
|
+
with pytest.raises(CircuitOpenError):
|
|
177
|
+
await fail()
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
# ── Rate Limiter ────────────────────────────────────────────────────
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
class TestRateLimiter:
|
|
184
|
+
def test_allows_within_limit(self) -> None:
|
|
185
|
+
rl = rate_limiter(max_calls=5, period=1.0)
|
|
186
|
+
|
|
187
|
+
@rl
|
|
188
|
+
def call() -> str:
|
|
189
|
+
return "ok"
|
|
190
|
+
|
|
191
|
+
for _ in range(5):
|
|
192
|
+
assert call() == "ok"
|
|
193
|
+
|
|
194
|
+
def test_blocks_over_limit(self) -> None:
|
|
195
|
+
rl = rate_limiter(max_calls=2, period=10.0)
|
|
196
|
+
|
|
197
|
+
@rl
|
|
198
|
+
def call() -> str:
|
|
199
|
+
return "ok"
|
|
200
|
+
|
|
201
|
+
call()
|
|
202
|
+
call()
|
|
203
|
+
with pytest.raises(ResilienceError, match="Rate limit"):
|
|
204
|
+
call()
|
|
205
|
+
|
|
206
|
+
def test_acquire_method(self) -> None:
|
|
207
|
+
rl = RateLimiter(max_calls=1, period=10.0)
|
|
208
|
+
assert rl.acquire() is True
|
|
209
|
+
assert rl.acquire() is False
|
|
210
|
+
|
|
211
|
+
async def test_async_rate_limiter(self) -> None:
|
|
212
|
+
rl = rate_limiter(max_calls=1, period=10.0)
|
|
213
|
+
|
|
214
|
+
@rl
|
|
215
|
+
async def call() -> str:
|
|
216
|
+
return "ok"
|
|
217
|
+
|
|
218
|
+
assert await call() == "ok"
|
|
219
|
+
with pytest.raises(ResilienceError):
|
|
220
|
+
await call()
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
# ── Timeout ─────────────────────────────────────────────────────────
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
class TestTimeout:
|
|
227
|
+
def test_within_timeout(self) -> None:
|
|
228
|
+
@timeout(1.0)
|
|
229
|
+
def fast() -> str:
|
|
230
|
+
return "ok"
|
|
231
|
+
|
|
232
|
+
assert fast() == "ok"
|
|
233
|
+
|
|
234
|
+
def test_exceeds_timeout(self) -> None:
|
|
235
|
+
@timeout(0.05)
|
|
236
|
+
def slow() -> str:
|
|
237
|
+
time.sleep(1.0)
|
|
238
|
+
return "ok"
|
|
239
|
+
|
|
240
|
+
with pytest.raises(TimeoutExceededError):
|
|
241
|
+
slow()
|
|
242
|
+
|
|
243
|
+
def test_propagates_exception(self) -> None:
|
|
244
|
+
@timeout(1.0)
|
|
245
|
+
def fail() -> None:
|
|
246
|
+
raise ValueError("inner")
|
|
247
|
+
|
|
248
|
+
with pytest.raises(ValueError, match="inner"):
|
|
249
|
+
fail()
|
|
250
|
+
|
|
251
|
+
async def test_async_timeout(self) -> None:
|
|
252
|
+
@timeout(1.0)
|
|
253
|
+
async def fast() -> str:
|
|
254
|
+
return "ok"
|
|
255
|
+
|
|
256
|
+
assert await fast() == "ok"
|
|
257
|
+
|
|
258
|
+
async def test_async_exceeds_timeout(self) -> None:
|
|
259
|
+
@timeout(0.05)
|
|
260
|
+
async def slow() -> str:
|
|
261
|
+
await asyncio.sleep(1.0)
|
|
262
|
+
return "ok"
|
|
263
|
+
|
|
264
|
+
with pytest.raises(TimeoutExceededError):
|
|
265
|
+
await slow()
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
# ── Bulkhead ────────────────────────────────────────────────────────
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
class TestBulkhead:
|
|
272
|
+
def test_allows_within_limit(self) -> None:
|
|
273
|
+
@bulkhead(max_concurrent=2)
|
|
274
|
+
def call() -> str:
|
|
275
|
+
return "ok"
|
|
276
|
+
|
|
277
|
+
assert call() == "ok"
|
|
278
|
+
|
|
279
|
+
def test_blocks_over_limit(self) -> None:
|
|
280
|
+
barrier = threading.Barrier(2)
|
|
281
|
+
|
|
282
|
+
@bulkhead(max_concurrent=1)
|
|
283
|
+
def call() -> str:
|
|
284
|
+
barrier.wait(timeout=1)
|
|
285
|
+
return "ok"
|
|
286
|
+
|
|
287
|
+
errors: list[Exception] = []
|
|
288
|
+
results: list[str] = []
|
|
289
|
+
|
|
290
|
+
def run() -> None:
|
|
291
|
+
try:
|
|
292
|
+
results.append(call())
|
|
293
|
+
except BulkheadFullError as e:
|
|
294
|
+
errors.append(e)
|
|
295
|
+
|
|
296
|
+
t1 = threading.Thread(target=run)
|
|
297
|
+
t2 = threading.Thread(target=run)
|
|
298
|
+
t1.start()
|
|
299
|
+
time.sleep(0.02) # Let t1 acquire the semaphore
|
|
300
|
+
t2.start()
|
|
301
|
+
t1.join(timeout=2)
|
|
302
|
+
t2.join(timeout=2)
|
|
303
|
+
|
|
304
|
+
assert len(errors) == 1
|
|
305
|
+
|
|
306
|
+
async def test_async_bulkhead(self) -> None:
|
|
307
|
+
@bulkhead(max_concurrent=2)
|
|
308
|
+
async def call() -> str:
|
|
309
|
+
return "ok"
|
|
310
|
+
|
|
311
|
+
assert await call() == "ok"
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
# ── Fallback ────────────────────────────────────────────────────────
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
class TestFallback:
|
|
318
|
+
def test_no_fallback_on_success(self) -> None:
|
|
319
|
+
@fallback(lambda: "fallback")
|
|
320
|
+
def call() -> str:
|
|
321
|
+
return "ok"
|
|
322
|
+
|
|
323
|
+
assert call() == "ok"
|
|
324
|
+
|
|
325
|
+
def test_fallback_on_failure(self) -> None:
|
|
326
|
+
@fallback(lambda: "fallback")
|
|
327
|
+
def call() -> str:
|
|
328
|
+
raise ValueError("fail")
|
|
329
|
+
|
|
330
|
+
assert call() == "fallback"
|
|
331
|
+
|
|
332
|
+
async def test_async_fallback(self) -> None:
|
|
333
|
+
@fallback(lambda: "fallback")
|
|
334
|
+
async def call() -> str:
|
|
335
|
+
raise ValueError("fail")
|
|
336
|
+
|
|
337
|
+
assert await call() == "fallback"
|
|
338
|
+
|
|
339
|
+
async def test_async_fallback_fn(self) -> None:
|
|
340
|
+
async def fb() -> str:
|
|
341
|
+
return "async fallback"
|
|
342
|
+
|
|
343
|
+
@fallback(fb)
|
|
344
|
+
async def call() -> str:
|
|
345
|
+
raise ValueError("fail")
|
|
346
|
+
|
|
347
|
+
assert await call() == "async fallback"
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
# ── Pipeline ────────────────────────────────────────────────────────
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
class TestPipeline:
|
|
354
|
+
def test_compose_retry_and_fallback(self) -> None:
|
|
355
|
+
call_count = 0
|
|
356
|
+
|
|
357
|
+
@pipeline(retry(max_attempts=2, delay=0), fallback(lambda: "fallback"))
|
|
358
|
+
def call() -> str:
|
|
359
|
+
nonlocal call_count
|
|
360
|
+
call_count += 1
|
|
361
|
+
raise ValueError("fail")
|
|
362
|
+
|
|
363
|
+
result = call()
|
|
364
|
+
assert result == "fallback"
|
|
365
|
+
assert call_count == 2
|
|
366
|
+
|
|
367
|
+
def test_compose_order(self) -> None:
|
|
368
|
+
order: list[str] = []
|
|
369
|
+
|
|
370
|
+
def deco_a(fn: Any) -> Any:
|
|
371
|
+
@functools.wraps(fn)
|
|
372
|
+
def wrapper(*args: Any, **kwargs: Any) -> Any:
|
|
373
|
+
order.append("a")
|
|
374
|
+
return fn(*args, **kwargs)
|
|
375
|
+
return wrapper
|
|
376
|
+
|
|
377
|
+
def deco_b(fn: Any) -> Any:
|
|
378
|
+
@functools.wraps(fn)
|
|
379
|
+
def wrapper(*args: Any, **kwargs: Any) -> Any:
|
|
380
|
+
order.append("b")
|
|
381
|
+
return fn(*args, **kwargs)
|
|
382
|
+
return wrapper
|
|
383
|
+
|
|
384
|
+
@pipeline(deco_a, deco_b)
|
|
385
|
+
def call() -> str:
|
|
386
|
+
return "ok"
|
|
387
|
+
|
|
388
|
+
call()
|
|
389
|
+
# deco_b wraps deco_a wraps fn → b runs first, then a
|
|
390
|
+
assert order == ["b", "a"]
|