Expand description
Circuit breaker state machine and retry middleware.
§Circuit breaker
CircuitBreaker tracks per-backend failure counts and transitions through
three states:
- Closed — the backend is healthy; failures are counted. When the count
reaches
failure_thresholdthe breaker moves to Open. - Open — the backend is considered unhealthy; all requests are rejected
immediately (no TCP connection is attempted). After
recoveryseconds the breaker moves to HalfOpen. - HalfOpen — one probe request is let through. On success the breaker closes; on failure it re-opens and the recovery timer resets.
§Retry middleware
RetryLayer wraps any Application and re-dispatches the request when
the inner app returns one of the configured status codes (default: 502, 503,
504) up to max_retries additional times.
§Persistence
CircuitBreaker keeps state in a plain in-process HashMap — a restart
(or a deploy) resets every backend back to Closed, so a backend that
tripped the breaker moments before a restart looks healthy again
immediately, and may cascade failures again before anything notices.
RedisCircuitBreaker has the same state machine and the same
is_available/record_success/record_failure/reset/state shape, but
persists each backend’s state in Redis (via the same hand-rolled RESP
client crate::rate_limit::RedisRateLimiter and
crate::session::RedisSessionStore use) — surviving a restart, and
shared across every rws instance pointed at that Redis server.
§Example
use rust_web_server::app::App;
use rust_web_server::core::New;
use rust_web_server::circuit_breaker::RetryLayer;
use rust_web_server::middleware::WithMiddleware;
let app = WithMiddleware::new(App::new())
.wrap(RetryLayer::new().max_retries(2));Structs§
- Circuit
Breaker - Per-backend circuit breaker.
- Redis
Circuit Breaker - Per-backend circuit breaker, persisted in Redis.
- Retry
Layer - Retry middleware.
Enums§
- Breaker
State - Current state of a single backend’s circuit breaker.
Functions§
- global
- Return the process-wide default circuit breaker (threshold=5, recovery=30 s).