Circuit breaker pattern implementation fastapi
Project description
Circuit Breaker Pattern in Python
Overview
The Circuit Breaker pattern is a design pattern used to improve system resilience by preventing continuous failures in a system. It acts as a safeguard by monitoring requests and stopping repeated failures from affecting performance.
How It Works
- Closed State: The system operates normally and allows requests.
- Open State: If failures exceed a threshold, the circuit "opens," stopping further requests for a cooldown period.
- Half-Open State: After a cooldown, a few test requests are allowed. If they succeed, the circuit closes; otherwise, it stays open.
Why Use Circuit Breaker?
- Prevents cascading failures in distributed systems.
- Improves system reliability and recovery.
- Reduces unnecessary load on failing services.
Example Usage in Python
import uvicorn
from fastapi import FastAPI
from fastapi.exceptions import HTTPException
from circuit_breaker import CircuitBreakerMiddleware, CircuitBreakerInputDto
app = FastAPI()
@app.get("/success-endpoint")
async def success_endpoint():
return "success"
@app.get("/failure-endpoint")
async def faulty_endpoint():
raise HTTPException(status_code=500)
app.add_middleware(
CircuitBreakerMiddleware,
circuit_breaker_input=CircuitBreakerInputDto(
exception_list=(Exception,),
half_open_retry_count=3,
half_open_retry_timeout_seconds=30,
),
)
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file circuit_breaker_fastapi-0.1.0.tar.gz.
File metadata
- Download URL: circuit_breaker_fastapi-0.1.0.tar.gz
- Upload date:
- Size: 3.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
020e3860107f1f685e90c68a4f13be06b421d48c50f42a94a70855613b7c3b51
|
|
| MD5 |
2dea748b4d0def2ab5ddf03b8d93e712
|
|
| BLAKE2b-256 |
6d7b1d624c4c05eca33e94a316e28e540b82269f007a7ae9c51cbae94fa7e9e6
|
File details
Details for the file circuit_breaker_fastapi-0.1.0-py3-none-any.whl.
File metadata
- Download URL: circuit_breaker_fastapi-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a449f7bae82b8f8ef72d7e3be5cc09c7c13a31757157881ff9e75a4fa51cbd91
|
|
| MD5 |
8b40a5feac82cc705ace102324b66e75
|
|
| BLAKE2b-256 |
52ceeceabebdf5efdc02c9384685d26bc74865937c79311863171d7ce51663e7
|