taipanstack 0.5.2__tar.gz → 0.6.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.
- {taipanstack-0.5.2 → taipanstack-0.6.0}/PKG-INFO +31 -30
- {taipanstack-0.5.2 → taipanstack-0.6.0}/README.md +28 -28
- {taipanstack-0.5.2 → taipanstack-0.6.0}/pyproject.toml +6 -5
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/__init__.py +1 -1
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/bridges/_imports.py +2 -2
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/bridges/db_bridge.py +86 -37
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/bridges/http_bridge.py +63 -33
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/bridges/web_bridge.py +20 -8
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/config/generators.py +18 -3
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/config/models.py +3 -2
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/core/compat.py +2 -5
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/core/optimizations.py +2 -2
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/core/result.py +24 -16
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/resilience/adaptive/adaptive_retry.py +1 -1
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/resilience/adaptive/bulkhead.py +40 -17
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/resilience/adaptive/orchestrator.py +131 -74
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/resilience/circuit_breaker.py +190 -126
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/resilience/resilience.py +35 -15
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/resilience/retry.py +103 -44
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/resilience/watchdogs/config_watcher.py +16 -8
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/resilience/watchdogs/health_pinger.py +16 -2
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/resilience/watchdogs/resource_watcher.py +5 -5
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/security/decorators.py +8 -14
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/security/guards.py +113 -33
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/security/jwt.py +16 -11
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/security/models.py +19 -17
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/security/password.py +2 -2
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/security/sanitizers.py +87 -13
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/security/validators.py +28 -9
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/utils/cache.py +53 -41
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/utils/concurrency.py +73 -30
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/utils/filesystem.py +42 -26
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/utils/logging.py +30 -16
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/utils/rate_limit.py +23 -12
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/utils/subprocess.py +6 -4
- {taipanstack-0.5.2 → taipanstack-0.6.0}/LICENSE +0 -0
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/bridges/__init__.py +0 -0
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/config/__init__.py +0 -0
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/config/version_config.py +0 -0
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/core/__init__.py +0 -0
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/resilience/__init__.py +0 -0
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/resilience/adaptive/__init__.py +0 -0
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/resilience/adaptive/adaptive_breaker.py +0 -0
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/resilience/watchdogs/__init__.py +0 -0
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/resilience/watchdogs/_base.py +0 -0
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/security/__init__.py +0 -0
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/security/types.py +0 -0
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/utils/__init__.py +0 -0
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/utils/circuit_breaker.py +0 -0
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/utils/context.py +0 -0
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/utils/resilience.py +0 -0
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/utils/retry.py +0 -0
- {taipanstack-0.5.2 → taipanstack-0.6.0}/src/taipanstack/utils/serialization.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: taipanstack
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.6.0
|
|
4
4
|
Summary: TaipanStack - Modular, secure, and scalable Python stack for robust development
|
|
5
5
|
License-Expression: MIT
|
|
6
6
|
License-File: LICENSE
|
|
@@ -32,12 +32,13 @@ Requires-Dist: httpx (>=0.24.0) ; extra == "all"
|
|
|
32
32
|
Requires-Dist: httpx (>=0.24.0) ; extra == "bridges-http"
|
|
33
33
|
Requires-Dist: litestar (>=2.0.0) ; extra == "all"
|
|
34
34
|
Requires-Dist: litestar (>=2.0.0) ; extra == "bridges-web"
|
|
35
|
+
Requires-Dist: msgpack (>=1.2.1,<2.0.0)
|
|
35
36
|
Requires-Dist: orjson (>=3.9.0)
|
|
36
37
|
Requires-Dist: pip (>=26.1,<27.0)
|
|
37
38
|
Requires-Dist: psutil (>=5.9.0) ; extra == "all"
|
|
38
39
|
Requires-Dist: psutil (>=5.9.0) ; extra == "resilience"
|
|
39
40
|
Requires-Dist: pydantic (>=2.0)
|
|
40
|
-
Requires-Dist: pydantic-settings (>=2.0.0)
|
|
41
|
+
Requires-Dist: pydantic-settings (>=2.14.2,<3.0.0)
|
|
41
42
|
Requires-Dist: pyjwt (>=2.11.0,<3.0.0)
|
|
42
43
|
Requires-Dist: redis (>=5.0.0) ; extra == "all"
|
|
43
44
|
Requires-Dist: redis (>=5.0.0) ; extra == "bridges-db"
|
|
@@ -58,6 +59,7 @@ Description-Content-Type: text/markdown
|
|
|
58
59
|
<div align="center">
|
|
59
60
|
|
|
60
61
|
# 🐍 Taipan Stack
|
|
62
|
+
[](https://github.com/gabrielima7/TaipanStack/actions/workflows/ci-push-validate.yml)
|
|
61
63
|
|
|
62
64
|
### **The Modern Python Foundation**
|
|
63
65
|
|
|
@@ -87,12 +89,11 @@ Description-Content-Type: text/markdown
|
|
|
87
89
|
|
|
88
90
|
Taipan Stack is a battle-tested foundation for production-grade Python projects that combines **security**, **performance**, and **developer experience** into a single, cohesive toolkit.
|
|
89
91
|
|
|
90
|
-
### ✨ What's New in v0.
|
|
91
|
-
- **
|
|
92
|
-
- **Resilience
|
|
93
|
-
- **
|
|
94
|
-
- **QA
|
|
95
|
-
- **Bridges API Reference**: Added new API documentation reference for the Bridges module (PR #822).
|
|
92
|
+
### ✨ What's New in v0.6.0
|
|
93
|
+
- **Security & Hardening**: Hardened path sanitization, file extension checking, SSRF protections, and URL smugglers with bounds checks and strict type validation (PR #871, #900, #903, #908, #924, #951, #955, #957, #959).
|
|
94
|
+
- **Resilience and Concurrency**: Fixed concurrency permit leaks, potential infinite loops in health watchdogs, and stabilized timeout structures under cancellation and arithmetic overflow scenarios (PR #932, #937, #940, #948).
|
|
95
|
+
- **Typing & Clean Code**: Refactored hot paths to drastically reduce cyclomatic and cognitive complexity, and enhanced static typing with explicit `TypeAlias`, `Literal`, and `TypeGuard` mapping (PR #887, #901, #918, #964).
|
|
96
|
+
- **QA and Standardized Testing**: Consolidated and fully standardized the naming conventions across the entire testing suite, verifying **1,430 passing tests** with 100% genuine branch coverage and no bypasses (PR #882, #886, #891, #894, #909, #912, #926, #930, #944, #954).
|
|
96
97
|
|
|
97
98
|
<table>
|
|
98
99
|
<tr>
|
|
@@ -129,7 +130,7 @@ Taipan Stack is a battle-tested foundation for production-grade Python projects
|
|
|
129
130
|
|
|
130
131
|
### 🔧 Developer Experience
|
|
131
132
|
- Pre-configured quality tools
|
|
132
|
-
- **100% code coverage** (1,
|
|
133
|
+
- **100% code coverage** (1,430 tests)
|
|
133
134
|
- Architecture enforcement
|
|
134
135
|
- Hardened Docker template
|
|
135
136
|
|
|
@@ -171,7 +172,7 @@ make all
|
|
|
171
172
|
### Verify Installation
|
|
172
173
|
|
|
173
174
|
```bash
|
|
174
|
-
# Run tests with 100% coverage (1,
|
|
175
|
+
# Run tests with 100% coverage (1,430 tests)
|
|
175
176
|
make test
|
|
176
177
|
|
|
177
178
|
# Check architecture contracts
|
|
@@ -192,25 +193,25 @@ make property-test
|
|
|
192
193
|
Taipan Stack follows a clean, layered architecture with strict dependency rules enforced by **Import Linter**.
|
|
193
194
|
|
|
194
195
|
```
|
|
195
|
-
|
|
196
|
-
│
|
|
197
|
-
│
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
▼
|
|
202
|
-
|
|
203
|
-
│ Security │
|
|
204
|
-
│ guards, saniti-
|
|
205
|
-
│ zers, validators│
|
|
206
|
-
|
|
207
|
-
│
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
196
|
+
┌────────────────────────────────────────────────────────┐
|
|
197
|
+
│ Application │
|
|
198
|
+
│ (src/app/main.py) │
|
|
199
|
+
└───────────────────────────┬────────────────────────────┘
|
|
200
|
+
│
|
|
201
|
+
┌───────────────┬─────────────────────┼───────────────┬──────────────┐
|
|
202
|
+
▼ ▼ ▼ ▼ ▼
|
|
203
|
+
┌─────────────────┐┌──────────────┐ ┌──────────────┐┌──────────────┐┌──────────────┐
|
|
204
|
+
│ Security ││ Config │ │ Bridges ││ Resilience ││ Utils │
|
|
205
|
+
│ guards, saniti- ││ models, gens │ │ http, db ││ retry, break ││ logging, fs │
|
|
206
|
+
│ zers, validators││ │ │ ││ watchdogs ││ context │
|
|
207
|
+
└────────┬────────┘└──────┬───────┘ └───────┬──────┘└───────┬──────┘└──────┬───────┘
|
|
208
|
+
│ │ │ │ │
|
|
209
|
+
└────────────────┴─────────────────────┼───────────────┴──────────────┘
|
|
210
|
+
▼
|
|
211
|
+
┌────────────────────────────────┐
|
|
212
|
+
│ Core │
|
|
213
|
+
│ Result types, optimizations │
|
|
214
|
+
└────────────────────────────────┘
|
|
214
215
|
```
|
|
215
216
|
|
|
216
217
|
### Project Structure
|
|
@@ -224,7 +225,7 @@ TaipanStack/
|
|
|
224
225
|
│ ├── config/ # ⚙️ Configuration models & generators
|
|
225
226
|
│ ├── security/ # 🛡️ Guards, sanitizers, validators
|
|
226
227
|
│ └── utils/ # 🔧 Logging, metrics, retry, filesystem
|
|
227
|
-
├── tests/ # ✅ 1,
|
|
228
|
+
├── tests/ # ✅ 1,430 tests, 100% coverage
|
|
228
229
|
├── .semgrep/ # 🔍 Custom SAST rules
|
|
229
230
|
├── .github/ # 🔄 CI/CD + SBOM/SLSA workflows
|
|
230
231
|
├── Dockerfile # 🐳 Hardened multi-stage container
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
<div align="center">
|
|
2
2
|
|
|
3
3
|
# 🐍 Taipan Stack
|
|
4
|
+
[](https://github.com/gabrielima7/TaipanStack/actions/workflows/ci-push-validate.yml)
|
|
4
5
|
|
|
5
6
|
### **The Modern Python Foundation**
|
|
6
7
|
|
|
@@ -30,12 +31,11 @@
|
|
|
30
31
|
|
|
31
32
|
Taipan Stack is a battle-tested foundation for production-grade Python projects that combines **security**, **performance**, and **developer experience** into a single, cohesive toolkit.
|
|
32
33
|
|
|
33
|
-
### ✨ What's New in v0.
|
|
34
|
-
- **
|
|
35
|
-
- **Resilience
|
|
36
|
-
- **
|
|
37
|
-
- **QA
|
|
38
|
-
- **Bridges API Reference**: Added new API documentation reference for the Bridges module (PR #822).
|
|
34
|
+
### ✨ What's New in v0.6.0
|
|
35
|
+
- **Security & Hardening**: Hardened path sanitization, file extension checking, SSRF protections, and URL smugglers with bounds checks and strict type validation (PR #871, #900, #903, #908, #924, #951, #955, #957, #959).
|
|
36
|
+
- **Resilience and Concurrency**: Fixed concurrency permit leaks, potential infinite loops in health watchdogs, and stabilized timeout structures under cancellation and arithmetic overflow scenarios (PR #932, #937, #940, #948).
|
|
37
|
+
- **Typing & Clean Code**: Refactored hot paths to drastically reduce cyclomatic and cognitive complexity, and enhanced static typing with explicit `TypeAlias`, `Literal`, and `TypeGuard` mapping (PR #887, #901, #918, #964).
|
|
38
|
+
- **QA and Standardized Testing**: Consolidated and fully standardized the naming conventions across the entire testing suite, verifying **1,430 passing tests** with 100% genuine branch coverage and no bypasses (PR #882, #886, #891, #894, #909, #912, #926, #930, #944, #954).
|
|
39
39
|
|
|
40
40
|
<table>
|
|
41
41
|
<tr>
|
|
@@ -72,7 +72,7 @@ Taipan Stack is a battle-tested foundation for production-grade Python projects
|
|
|
72
72
|
|
|
73
73
|
### 🔧 Developer Experience
|
|
74
74
|
- Pre-configured quality tools
|
|
75
|
-
- **100% code coverage** (1,
|
|
75
|
+
- **100% code coverage** (1,430 tests)
|
|
76
76
|
- Architecture enforcement
|
|
77
77
|
- Hardened Docker template
|
|
78
78
|
|
|
@@ -114,7 +114,7 @@ make all
|
|
|
114
114
|
### Verify Installation
|
|
115
115
|
|
|
116
116
|
```bash
|
|
117
|
-
# Run tests with 100% coverage (1,
|
|
117
|
+
# Run tests with 100% coverage (1,430 tests)
|
|
118
118
|
make test
|
|
119
119
|
|
|
120
120
|
# Check architecture contracts
|
|
@@ -135,25 +135,25 @@ make property-test
|
|
|
135
135
|
Taipan Stack follows a clean, layered architecture with strict dependency rules enforced by **Import Linter**.
|
|
136
136
|
|
|
137
137
|
```
|
|
138
|
-
|
|
139
|
-
│
|
|
140
|
-
│
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
▼
|
|
145
|
-
|
|
146
|
-
│ Security │
|
|
147
|
-
│ guards, saniti-
|
|
148
|
-
│ zers, validators│
|
|
149
|
-
|
|
150
|
-
│
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
138
|
+
┌────────────────────────────────────────────────────────┐
|
|
139
|
+
│ Application │
|
|
140
|
+
│ (src/app/main.py) │
|
|
141
|
+
└───────────────────────────┬────────────────────────────┘
|
|
142
|
+
│
|
|
143
|
+
┌───────────────┬─────────────────────┼───────────────┬──────────────┐
|
|
144
|
+
▼ ▼ ▼ ▼ ▼
|
|
145
|
+
┌─────────────────┐┌──────────────┐ ┌──────────────┐┌──────────────┐┌──────────────┐
|
|
146
|
+
│ Security ││ Config │ │ Bridges ││ Resilience ││ Utils │
|
|
147
|
+
│ guards, saniti- ││ models, gens │ │ http, db ││ retry, break ││ logging, fs │
|
|
148
|
+
│ zers, validators││ │ │ ││ watchdogs ││ context │
|
|
149
|
+
└────────┬────────┘└──────┬───────┘ └───────┬──────┘└───────┬──────┘└──────┬───────┘
|
|
150
|
+
│ │ │ │ │
|
|
151
|
+
└────────────────┴─────────────────────┼───────────────┴──────────────┘
|
|
152
|
+
▼
|
|
153
|
+
┌────────────────────────────────┐
|
|
154
|
+
│ Core │
|
|
155
|
+
│ Result types, optimizations │
|
|
156
|
+
└────────────────────────────────┘
|
|
157
157
|
```
|
|
158
158
|
|
|
159
159
|
### Project Structure
|
|
@@ -167,7 +167,7 @@ TaipanStack/
|
|
|
167
167
|
│ ├── config/ # ⚙️ Configuration models & generators
|
|
168
168
|
│ ├── security/ # 🛡️ Guards, sanitizers, validators
|
|
169
169
|
│ └── utils/ # 🔧 Logging, metrics, retry, filesystem
|
|
170
|
-
├── tests/ # ✅ 1,
|
|
170
|
+
├── tests/ # ✅ 1,430 tests, 100% coverage
|
|
171
171
|
├── .semgrep/ # 🔍 Custom SAST rules
|
|
172
172
|
├── .github/ # 🔄 CI/CD + SBOM/SLSA workflows
|
|
173
173
|
├── Dockerfile # 🐳 Hardened multi-stage container
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "taipanstack"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.6.0"
|
|
4
4
|
description = "TaipanStack - Modular, secure, and scalable Python stack for robust development"
|
|
5
5
|
authors = [
|
|
6
6
|
{name = "gabrielima7", email = "gabrielima.alu.lmb@gmail.com"}
|
|
@@ -23,13 +23,14 @@ classifiers = [
|
|
|
23
23
|
]
|
|
24
24
|
dependencies = [
|
|
25
25
|
"pydantic (>=2.0)",
|
|
26
|
-
"pydantic-settings (>=2.0.0)",
|
|
26
|
+
"pydantic-settings (>=2.14.2,<3.0.0)",
|
|
27
27
|
"orjson (>=3.9.0)",
|
|
28
28
|
"email-validator (>=2.0.0)",
|
|
29
29
|
"result (>=0.17.0)",
|
|
30
30
|
"pyjwt (>=2.11.0,<3.0.0)",
|
|
31
31
|
"argon2-cffi (>=25.1.0,<26.0.0)",
|
|
32
32
|
"pip (>=26.1,<27.0)",
|
|
33
|
+
"msgpack (>=1.2.1,<2.0.0)",
|
|
33
34
|
]
|
|
34
35
|
|
|
35
36
|
[project.urls]
|
|
@@ -102,7 +103,7 @@ dev = [
|
|
|
102
103
|
"pygments (>=2.19.2)",
|
|
103
104
|
"hypothesis (>=6.151.12,<7.0.0)",
|
|
104
105
|
"authlib (>=1.6.11,<2.0.0)",
|
|
105
|
-
"cryptography (>=46.0.7,<
|
|
106
|
+
"cryptography (>=46.0.7,<50.0.0)",
|
|
106
107
|
"mutmut (>=3.0.0,<4.0.0)",
|
|
107
108
|
"mkdocs-material (>=9.7.6,<10.0.0)",
|
|
108
109
|
]
|
|
@@ -184,6 +185,7 @@ mypy_path = "src"
|
|
|
184
185
|
explicit_package_bases = true
|
|
185
186
|
warn_return_any = true
|
|
186
187
|
warn_unused_configs = true
|
|
188
|
+
warn_unreachable = true
|
|
187
189
|
disallow_untyped_defs = true
|
|
188
190
|
disallow_any_unimported = false
|
|
189
191
|
no_implicit_optional = true
|
|
@@ -210,7 +212,7 @@ warn_required_dynamic_aliases = true
|
|
|
210
212
|
|
|
211
213
|
[tool.pytest.ini_options]
|
|
212
214
|
testpaths = ["tests"]
|
|
213
|
-
addopts = "-v --strict-markers --timeout=
|
|
215
|
+
addopts = "-v --strict-markers --timeout=60 --cov=src --cov-report=html --cov-report=term-missing"
|
|
214
216
|
asyncio_mode = "auto"
|
|
215
217
|
asyncio_default_fixture_loop_scope = "function"
|
|
216
218
|
markers = [
|
|
@@ -248,4 +250,3 @@ paths_to_mutate = ["src/taipanstack/security/sanitizers.py", "src/taipanstack/se
|
|
|
248
250
|
tests_dir = ["tests/test_property_sanitizers_operations.py", "tests/test_security_validators_operations.py", "tests/test_security_guards_operations.py", "tests/test_security_ssrf_operations.py", "tests/test_utils_retry_operations.py", "tests/test_utils_circuit_breaker_operations.py"]
|
|
249
251
|
also_copy = ["src/"]
|
|
250
252
|
runner = "python -m pytest -x -q --tb=no --timeout=10 --no-cov"
|
|
251
|
-
|
|
@@ -5,7 +5,7 @@ A modular, secure, and scalable Python stack for robust development.
|
|
|
5
5
|
Supports Python 3.11+ with version-aware optimizations for 3.13/3.14.
|
|
6
6
|
"""
|
|
7
7
|
|
|
8
|
-
__version__ = "0.
|
|
8
|
+
__version__ = "0.6.0"
|
|
9
9
|
|
|
10
10
|
from taipanstack.config.version_config import (
|
|
11
11
|
VersionRecommendations,
|
|
@@ -13,7 +13,9 @@ from typing import TYPE_CHECKING
|
|
|
13
13
|
|
|
14
14
|
if TYPE_CHECKING:
|
|
15
15
|
import redis.asyncio as aioredis
|
|
16
|
+
from sqlalchemy.engine import Result as SQLAlchemyResult
|
|
16
17
|
from sqlalchemy.ext.asyncio import AsyncEngine
|
|
18
|
+
from sqlalchemy.sql.executable import Executable
|
|
17
19
|
|
|
18
20
|
from taipanstack.core.result import Err, Ok, Result
|
|
19
21
|
from taipanstack.resilience.circuit_breaker import (
|
|
@@ -95,6 +97,23 @@ class ResilientDatabase:
|
|
|
95
97
|
self._circuit_breaker = circuit_breaker
|
|
96
98
|
self._retry_config = retry_config
|
|
97
99
|
|
|
100
|
+
def _check_db_dependencies(self) -> Result[None, Exception]:
|
|
101
|
+
if not _HAS_SQLALCHEMY:
|
|
102
|
+
return Err(
|
|
103
|
+
ImportError(
|
|
104
|
+
"sqlalchemy is required for ResilientDatabase. "
|
|
105
|
+
"Install with: pip install taipanstack[bridges-db]",
|
|
106
|
+
),
|
|
107
|
+
)
|
|
108
|
+
return Ok(None)
|
|
109
|
+
|
|
110
|
+
def _check_breaker_gate(self) -> Result[None, Exception]:
|
|
111
|
+
if self._circuit_breaker is not None:
|
|
112
|
+
cb_err = _breaker_is_open(self._circuit_breaker)
|
|
113
|
+
if cb_err is not None:
|
|
114
|
+
return Err(cb_err)
|
|
115
|
+
return Ok(None)
|
|
116
|
+
|
|
98
117
|
async def _handle_attempt_failure(
|
|
99
118
|
self,
|
|
100
119
|
exc: Exception,
|
|
@@ -125,33 +144,62 @@ class ResilientDatabase:
|
|
|
125
144
|
return True
|
|
126
145
|
return False
|
|
127
146
|
|
|
147
|
+
async def _execute_single_attempt(
|
|
148
|
+
self,
|
|
149
|
+
statement: Executable,
|
|
150
|
+
**kwargs: object,
|
|
151
|
+
) -> Result[SQLAlchemyResult[object], Exception]:
|
|
152
|
+
try:
|
|
153
|
+
async with AsyncSession(self._engine) as session:
|
|
154
|
+
result = await session.execute(statement, **kwargs)
|
|
155
|
+
return Ok(result)
|
|
156
|
+
except Exception as exc:
|
|
157
|
+
return Err(exc)
|
|
158
|
+
|
|
159
|
+
async def _process_attempt(
|
|
160
|
+
self,
|
|
161
|
+
statement: Executable,
|
|
162
|
+
attempt: int,
|
|
163
|
+
max_attempts: int,
|
|
164
|
+
**kwargs: object,
|
|
165
|
+
) -> Result[SQLAlchemyResult[object], Exception] | tuple[bool, Exception]:
|
|
166
|
+
result = await self._execute_single_attempt(statement, **kwargs)
|
|
167
|
+
if isinstance(result, Ok):
|
|
168
|
+
return result
|
|
169
|
+
|
|
170
|
+
last_error = result.err_value
|
|
171
|
+
should_continue = await self._handle_attempt_failure(
|
|
172
|
+
last_error, attempt, max_attempts
|
|
173
|
+
)
|
|
174
|
+
return should_continue, last_error
|
|
175
|
+
|
|
128
176
|
async def _execute_loop(
|
|
129
177
|
self,
|
|
130
|
-
statement:
|
|
178
|
+
statement: Executable,
|
|
131
179
|
max_attempts: int,
|
|
132
180
|
**kwargs: object,
|
|
133
|
-
) -> Result[object, Exception]:
|
|
181
|
+
) -> Result[SQLAlchemyResult[object], Exception]:
|
|
134
182
|
"""Execute the retry loop for database operations."""
|
|
135
|
-
last_error: Exception
|
|
183
|
+
last_error: Exception = RuntimeError("Database execute failed")
|
|
136
184
|
|
|
137
185
|
for attempt in range(1, max_attempts + 1):
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
186
|
+
outcome = await self._process_attempt(
|
|
187
|
+
statement, attempt, max_attempts, **kwargs
|
|
188
|
+
)
|
|
189
|
+
if not isinstance(outcome, tuple):
|
|
190
|
+
return outcome
|
|
191
|
+
|
|
192
|
+
should_continue, last_error = outcome
|
|
193
|
+
if not should_continue:
|
|
146
194
|
break
|
|
147
195
|
|
|
148
|
-
return Err(last_error
|
|
196
|
+
return Err(last_error)
|
|
149
197
|
|
|
150
198
|
async def execute(
|
|
151
199
|
self,
|
|
152
|
-
statement:
|
|
200
|
+
statement: Executable,
|
|
153
201
|
**kwargs: object,
|
|
154
|
-
) -> Result[object, Exception]:
|
|
202
|
+
) -> Result[SQLAlchemyResult[object], Exception]:
|
|
155
203
|
"""Execute a SQL statement with resilience.
|
|
156
204
|
|
|
157
205
|
Args:
|
|
@@ -162,19 +210,13 @@ class ResilientDatabase:
|
|
|
162
210
|
``Ok(result)`` on success, ``Err`` on failure.
|
|
163
211
|
|
|
164
212
|
"""
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
"sqlalchemy is required for ResilientDatabase. "
|
|
169
|
-
"Install with: pip install taipanstack[bridges-db]"
|
|
170
|
-
)
|
|
171
|
-
)
|
|
213
|
+
dep_result = self._check_db_dependencies()
|
|
214
|
+
if isinstance(dep_result, Err):
|
|
215
|
+
return dep_result
|
|
172
216
|
|
|
173
|
-
|
|
174
|
-
if
|
|
175
|
-
|
|
176
|
-
if cb_err is not None:
|
|
177
|
-
return Err(cb_err)
|
|
217
|
+
cb_result = self._check_breaker_gate()
|
|
218
|
+
if isinstance(cb_result, Err):
|
|
219
|
+
return cb_result
|
|
178
220
|
|
|
179
221
|
max_attempts = 1
|
|
180
222
|
if self._retry_config is not None:
|
|
@@ -236,8 +278,8 @@ class ResilientRedis:
|
|
|
236
278
|
return Err(
|
|
237
279
|
ImportError(
|
|
238
280
|
"redis is required for ResilientRedis. "
|
|
239
|
-
"Install with: pip install taipanstack[bridges-db]"
|
|
240
|
-
)
|
|
281
|
+
"Install with: pip install taipanstack[bridges-db]",
|
|
282
|
+
),
|
|
241
283
|
)
|
|
242
284
|
return Ok(None)
|
|
243
285
|
|
|
@@ -248,6 +290,21 @@ class ResilientRedis:
|
|
|
248
290
|
return Err(cb_err)
|
|
249
291
|
return Ok(None)
|
|
250
292
|
|
|
293
|
+
async def _execute_command(
|
|
294
|
+
self,
|
|
295
|
+
command: str,
|
|
296
|
+
*args: object,
|
|
297
|
+
) -> Result[object, Exception]:
|
|
298
|
+
try:
|
|
299
|
+
fn = getattr(self._client, command.lower())
|
|
300
|
+
result = await fn(*args)
|
|
301
|
+
return Ok(result)
|
|
302
|
+
except Exception as exc:
|
|
303
|
+
logger.warning("Redis command '%s' failed: %s", command, exc)
|
|
304
|
+
if self._circuit_breaker is not None:
|
|
305
|
+
self._circuit_breaker._record_failure(exc)
|
|
306
|
+
return Err(exc)
|
|
307
|
+
|
|
251
308
|
async def execute(
|
|
252
309
|
self,
|
|
253
310
|
command: str,
|
|
@@ -271,15 +328,7 @@ class ResilientRedis:
|
|
|
271
328
|
if isinstance(cb_result, Err):
|
|
272
329
|
return cb_result
|
|
273
330
|
|
|
274
|
-
|
|
275
|
-
fn = getattr(self._client, command.lower())
|
|
276
|
-
result = await fn(*args)
|
|
277
|
-
return Ok(result)
|
|
278
|
-
except Exception as exc:
|
|
279
|
-
logger.warning("Redis command '%s' failed: %s", command, exc)
|
|
280
|
-
if self._circuit_breaker is not None:
|
|
281
|
-
self._circuit_breaker._record_failure(exc)
|
|
282
|
-
return Err(exc)
|
|
331
|
+
return await self._execute_command(command, *args)
|
|
283
332
|
|
|
284
333
|
async def health_check(self) -> Result[bool, Exception]:
|
|
285
334
|
"""Check Redis connectivity via PING.
|