circuitbreaker-rs
A Rust implementation by copyleftdev
A production-grade, zero-boilerplate, lock-efficient, observability-ready Circuit Breaker library for Rust applications. Fast, reliable, and well-tested.
Overview
circuitbreaker-rs is a high-performance circuit breaker implementation designed for integration into performance-critical Rust systems. It provides both synchronous and asynchronous interfaces, integrates with observability stacks, and supports custom policies, configurable time-windows, and recovery strategies.
Features
- Lock-free State Management: Enum-based FSM using atomic operations for state transitions
- Flexible Failure Tracking: Support for both fixed-window and exponential moving average (EMA) metrics
- Customizable Policies: Implement your own tripping and reset logic or use the provided policies
- Sync and Async Support: Works with both blocking and async code
- Observability Ready: Built-in support for metrics collection and hooks for state transitions
- Zero-alloc Hot Path: Optimized for performance in critical paths
- Rich Error Handling: Detailed error information without using panics
Quick Start
Add the dependency to your Cargo.toml:
[]
= "0.1.0"
Basic usage:
use ;
use Duration;
use Error;
// Your function that might fail - must implement std::error::Error
use Error;
use fmt;
;
Error Handling Requirements
Important: The error type used with the circuit breaker must implement the std::error::Error trait. Using types like String as errors directly will not work. Here's a simple pattern for creating a compatible error type:
use Error;
use fmt;
;
Advanced Configuration
The library offers extensive configuration options through the builder pattern:
let breaker = builder
.failure_threshold // Trip when error rate exceeds 50%
.min_throughput // Require at least 10 calls before considering error rate
.cooldown // Wait 30 seconds before trying half-open state
.probe_interval // Allow 3 test requests when half-open
.consecutive_failures // Trip after 5 consecutive failures regardless of rate
.consecutive_successes // Reset after 2 consecutive successes in half-open state
.metric_sink // Use Prometheus for metrics
.build;
Custom Policies
Implement the BreakerPolicy trait to create custom circuit breaker policies:
use ;
Async Support
Async support is available with the async feature:
[]
= { = "0.1.0", = ["async"] }
And then use the call_async method:
let breaker = builder.build;
let result = breaker.call_async.await;
Observability
The library provides hooks for state transitions and metric collection:
let mut hooks = new;
hooks.set_on_open;
hooks.set_on_close;
let breaker = builder
.hooks
.metric_sink
.build;
Features Flags
std- Standard library support (default)async- Async support with Tokioprometheus- Prometheus metrics integrationtracing- Tracing integration
Performance
circuitbreaker-rs is designed for high-performance scenarios. Here are some benchmark results from the included benchmarks:
| Benchmark | Description | Performance |
|---|---|---|
circuit_breaker_closed_success |
Regular operation (circuit closed) | ~80 ns per call |
circuit_breaker_transition |
State transition performance | ~600 ns per transition |
circuit_breaker_concurrent |
Multi-threaded performance (4 threads) | ~530 μs for 4000 operations |
These benchmarks demonstrate the library's minimal overhead during normal operation and efficient state transitions, making it suitable for high-throughput systems and latency-sensitive applications.
Run the benchmarks yourself with: cargo bench
License
This project is licensed under either of:
- MIT license
- Apache License, Version 2.0
at your option.