Skip to main content

Crate tower_resilience_outlier

Crate tower_resilience_outlier 

Source
Expand description

Outlier detection middleware for Tower services.

Outlier detection tracks per-instance health on live traffic and ejects unhealthy instances from a fleet. This is complementary to circuit breaker:

Circuit BreakerOutlier Detection
TriggerFailure rate over sliding windowConsecutive errors
ScopePer-service, isolatedFleet-aware (max_ejection_percent)
Detection speedNeeds minimum_calls before evaluatingCatches hard-down immediately
RecoveryHalf-open state with probe callsTime-based automatic recovery

§Quick Start

use tower_resilience_outlier::{OutlierDetectionLayer, OutlierDetector};
use tower::{ServiceBuilder, service_fn};
use std::time::Duration;

// Create a shared detector for the fleet
let detector = OutlierDetector::new()
    .max_ejection_percent(50)
    .base_ejection_duration(Duration::from_secs(30));

// Register instances
detector.register("backend-1", 5);  // eject after 5 consecutive errors
detector.register("backend-2", 5);

// Create per-instance layers sharing the same detector
let layer1 = OutlierDetectionLayer::builder()
    .detector(detector.clone())
    .instance_name("backend-1")
    .build();

let layer2 = OutlierDetectionLayer::builder()
    .detector(detector.clone())
    .instance_name("backend-2")
    .build();

// Apply to services
let svc1 = ServiceBuilder::new()
    .layer(layer1)
    .service(service_fn(|req: String| async move { Ok::<_, std::io::Error>(req) }));

§Backpressure vs Error Mode

By default, ejected instances return Pending from poll_ready(), which causes Tower load balancers to route around them. For cases where you want an explicit error, use .error_on_ejection():

let layer = OutlierDetectionLayer::builder()
    .detector(detector)
    .instance_name("backend-1")
    .error_on_ejection()
    .build();

Re-exports§

pub use config::OutlierDetectionConfig;
pub use config::OutlierDetectionConfigBuilder;
pub use detector::OutlierDetector;
pub use error::OutlierDetectionError;
pub use error::OutlierDetectionServiceError;
pub use events::OutlierDetectionEvent;
pub use layer::OutlierDetectionLayer;
pub use service::OutlierDetectionService;
pub use strategy::ConsecutiveErrors;
pub use strategy::EjectionStrategy;

Modules§

config
Configuration types for outlier detection. Configuration and builder for the outlier detection middleware.
detector
Shared fleet-level outlier detector state. Shared fleet-level outlier detector state.
error
Error types for outlier detection. Error types for the outlier detection middleware.
events
Event types emitted by outlier detection. Events emitted by the outlier detection middleware.
layer
Tower Layer implementation for outlier detection. Tower Layer implementation for outlier detection.
service
Tower Service implementation for outlier detection. Tower Service implementation for outlier detection.
strategy
Ejection strategies (e.g., consecutive errors). Ejection strategies for outlier detection.