-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelite_resilience.py
More file actions
180 lines (154 loc) · 6.56 KB
/
Copy pathelite_resilience.py
File metadata and controls
180 lines (154 loc) · 6.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"""
elite_resilience.py — Provider resilience & circuit breaker patterns
====================================================================
Intelligent fallback chains, retry logic with exponential backoff, provider health monitoring.
"""
import asyncio
import json
import time
from dataclasses import dataclass, field
from datetime import datetime, timedelta
from enum import Enum
from typing import Any, Callable, Generic, TypeVar
T = TypeVar("T")
class CircuitState(Enum):
CLOSED = "closed" # Normal operation
OPEN = "open" # Failing; reject requests
HALF_OPEN = "half_open" # Testing; allow single request
@dataclass
class CircuitBreakerConfig:
failure_threshold: int = 5 # Failures before opening
success_threshold: int = 2 # Successes to close (from half-open)
timeout_seconds: float = 60.0 # Time before half-open retry
@dataclass
class ProviderHealth:
provider: str
state: CircuitState = CircuitState.CLOSED
failure_count: int = 0
success_count: int = 0
last_failure_time: float = 0.0
last_response_time_ms: float = 0.0
total_requests: int = 0
total_errors: int = 0
error_rate: float = 0.0
class CircuitBreaker(Generic[T]):
"""Intelligent circuit breaker for provider resilience."""
def __init__(self, name: str, config: CircuitBreakerConfig = None):
self.name = name
self.config = config or CircuitBreakerConfig()
self.health = ProviderHealth(provider=name)
self._lock = asyncio.Lock()
async def call(self, fn: Callable, *args, **kwargs) -> T:
"""Execute function with circuit breaker protection."""
async with self._lock:
if self.health.state == CircuitState.OPEN:
if time.time() - self.health.last_failure_time > self.config.timeout_seconds:
self.health.state = CircuitState.HALF_OPEN
self.health.success_count = 0
else:
raise Exception(f"[circuit] {self.name} is OPEN (backoff)")
start = time.monotonic()
try:
result = await fn(*args, **kwargs)
await self._record_success(time.monotonic() - start)
return result
except Exception as e:
await self._record_failure()
raise
async def _record_success(self, elapsed: float):
async with self._lock:
self.health.last_response_time_ms = elapsed * 1000
self.health.total_requests += 1
if self.health.state == CircuitState.HALF_OPEN:
self.health.success_count += 1
if self.health.success_count >= self.config.success_threshold:
self.health.state = CircuitState.CLOSED
self.health.failure_count = 0
else:
self.health.failure_count = max(0, self.health.failure_count - 1)
self.health.error_rate = (
self.health.total_errors / self.health.total_requests
if self.health.total_requests > 0 else 0.0
)
async def _record_failure(self):
async with self._lock:
self.health.last_failure_time = time.time()
self.health.failure_count += 1
self.health.total_errors += 1
self.health.total_requests += 1
if self.health.failure_count >= self.config.failure_threshold:
self.health.state = CircuitState.OPEN
self.health.error_rate = (
self.health.total_errors / self.health.total_requests
)
async def health_check(self) -> dict:
"""Return health snapshot for monitoring."""
async with self._lock:
return {
"provider": self.name,
"state": self.health.state.value,
"error_rate": round(self.health.error_rate, 3),
"failure_count": self.health.failure_count,
"total_requests": self.health.total_requests,
"last_response_time_ms": round(self.health.last_response_time_ms, 1),
}
class FallbackChain:
"""Execute multiple strategies with intelligent fallback."""
def __init__(self, name: str, strategies: list[tuple[str, Callable]],
allow_partial: bool = False):
self.name = name
self.strategies = strategies # [(name, fn), ...]
self.allow_partial = allow_partial
self.breakers = {s[0]: CircuitBreaker(s[0]) for s in strategies}
self.attempt_log: list[dict] = []
async def execute(self, *args, **kwargs) -> Any:
"""Try strategies in order until one succeeds."""
self.attempt_log.clear()
for strategy_name, fn in self.strategies:
breaker = self.breakers[strategy_name]
try:
result = await breaker.call(fn, *args, **kwargs)
self.attempt_log.append({
"strategy": strategy_name,
"status": "success",
"timestamp": datetime.now().isoformat(),
})
return result
except Exception as e:
self.attempt_log.append({
"strategy": strategy_name,
"status": "failed",
"error": str(e)[:100],
"timestamp": datetime.now().isoformat(),
})
log_str = json.dumps(self.attempt_log, indent=2)
raise Exception(f"[fallback] {self.name}: all strategies exhausted\n{log_str}")
async def health_report(self) -> dict:
"""Detailed health report for all strategies."""
return {
"chain": self.name,
"strategies": [
await breaker.health_check()
for breaker in self.breakers.values()
],
"recent_attempts": self.attempt_log[-10:], # Last 10
}
async def retry_with_backoff(
fn: Callable,
max_retries: int = 3,
base_delay: float = 0.5,
max_delay: float = 30.0,
jitter: bool = True,
) -> Any:
"""Retry with exponential backoff and optional jitter."""
import random
for attempt in range(max_retries + 1):
try:
return await fn()
except Exception as e:
if attempt >= max_retries:
raise
delay = min(base_delay * (2 ** attempt), max_delay)
if jitter:
delay *= (0.5 + random.random())
await asyncio.sleep(delay)