Skip to main content

Module circuit_breaker

Module circuit_breaker 

Source
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_threshold the breaker moves to Open.
  • Open — the backend is considered unhealthy; all requests are rejected immediately (no TCP connection is attempted). After recovery seconds 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.

§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§

CircuitBreaker
Per-backend circuit breaker.
RetryLayer
Retry middleware.

Enums§

BreakerState
Current state of a single backend’s circuit breaker.

Functions§

global
Return the process-wide default circuit breaker (threshold=5, recovery=30 s).