Spaces:
Running
Running
File size: 5,356 Bytes
2978bba | 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 | # MorphGuard Resilient API System
The MorphGuard Resilient API System provides a comprehensive solution for making API calls with intelligent fallbacks, context-aware routing, and adaptive behavior. This system is designed to enhance application reliability and user experience by gracefully handling API failures and degraded conditions.
## Key Features
### 1. Intelligent Fallbacks
- **Tiered Fallback Selection**: Prioritize fallbacks by importance
- **Context-Aware Fallbacks**: Select fallbacks based on request context
- **Dynamic Fallback Generation**: Generate fallback data at runtime
- **Fallback Registry**: Centralized management of fallbacks
### 2. Context-Aware Request Handling
- **Request Context**: Route requests based on context information
- **Endpoint Configuration**: Fine-tune behavior for specific endpoints
- **Adaptive Timeouts**: Adjust timeouts based on historical performance
- **Priority-Based Handling**: Prioritize critical requests
### 3. Circuit Breaker Pattern
- **Automatic Circuit Breaking**: Prevent cascading failures
- **Health-Aware Recovery**: Intelligently attempt recovery
- **Per-Endpoint Configuration**: Configure circuit breakers for each endpoint
- **Manual Reset**: Reset circuit breakers when needed
### 4. Resource-Aware Request Throttling
- **Resource Monitoring**: Track CPU and memory usage
- **Automatic Throttling**: Reduce load during high resource utilization
- **Priority-Based Dropping**: Drop low-priority requests first
- **Adaptive Behavior**: Adjust behavior based on system conditions
### 5. Degraded Mode Operations
- **Degraded Mode Detection**: Automatically detect system degradation
- **Fallback to Cached Data**: Use cached data during degradation
- **Reduced Functionality**: Provide essential functionality during degradation
- **Graceful Recovery**: Smoothly transition back to normal operation
### 6. Comprehensive Metrics and Telemetry
- **Request Statistics**: Track success rates, timing, and more
- **Circuit Breaker Status**: Monitor circuit breaker states
- **Resource Usage**: Track resource utilization
- **Fallback Usage**: Monitor fallback utilization
## Architecture
The system is built on a modular architecture with the following components:
1. **API Fallback Registry**: Manages fallbacks for API endpoints
2. **Context-Aware API Adapter**: Enhances requests with context awareness
3. **API Resilience Client**: Integrates all components for a unified interface
4. **API Utilities**: Core request handling and circuit breaking
5. **Telemetry Integration**: Comprehensive logging and metrics
## Usage
### Basic Usage
```python
from src.api_resilience import get_resilient_api
# Get the resilient API client
api = get_resilient_api()
# Make a request
try:
response = api.get("/users")
print(f"Response: {response}")
except Exception as e:
print(f"Error: {e}")
```
### Context-Aware Requests
```python
# Make a request with context
response = api.get(
"/users/profile",
context={"role": "admin", "user_id": "123"}
)
```
### Registering Fallbacks
```python
from src.api_resilience import get_resilient_api, FallbackTier
# Register a fallback
api.register_fallback(
endpoint="/users/profile",
key="default_profile",
fallback={"id": None, "name": "Guest", "email": None, "role": "guest"},
tier=FallbackTier.PRIMARY,
tags={"type": "static", "category": "user"}
)
```
### Configuring Endpoints
```python
from src.api_resilience import get_resilient_api
from src.context_aware_api import EndpointPriority, ResourceConsumption
# Configure an endpoint
api.configure_endpoint(
"/users",
priority=EndpointPriority.HIGH,
resource_consumption=ResourceConsumption.MODERATE,
timeout=5.0,
retries=2,
circuit_breaker_enabled=True,
cache_enabled=True,
tags={"category": "user", "importance": "high"}
)
```
### Managing Circuit Breakers
```python
# Reset a circuit breaker
api.reset_circuit_breaker("/users")
# Reset all circuit breakers
api.reset_all_circuit_breakers()
```
### Monitoring
```python
# Get request statistics
stats = api.get_request_stats("/users")
# Check if system is in degraded mode
is_degraded = api.is_in_degraded_mode()
# Get fallbacks for an endpoint
fallbacks = api.get_fallbacks("/users")
```
## Configuration
The resilient API system can be configured with various options:
```python
options = {
"base_url": "http://localhost:5000/api",
"cache_dir": ".mg_api_cache",
"fallbacks_dir": ".mg_fallbacks",
"timeout": 30.0,
"retries": 3,
"enable_telemetry": True,
"enable_fallbacks": True,
"enable_circuit_breaker": True,
"enable_context_aware": True,
"enable_resource_monitoring": True,
"register_default_fallbacks": True
}
api = get_resilient_api(options)
```
## Running the Demo
A demo script is provided to showcase the features of the resilient API system:
```bash
python demo_resilient_api.py
```
For verbose logging:
```bash
python demo_resilient_api.py --verbose
```
## Integration with MorphGuard
The resilient API system is fully integrated with the MorphGuard telemetry and error handling systems, providing comprehensive monitoring and diagnostics. It uses the existing caching and circuit breaker implementations while adding new capabilities for context-aware routing and intelligent fallbacks. |