Skip to main content

CircuitBreaker

Struct CircuitBreaker 

Source
pub struct CircuitBreaker { /* private fields */ }
Expand description

Circuit breaker for preventing cascading failures.

The circuit breaker monitors request success/failure patterns and automatically blocks requests to failing endpoints, allowing the system to recover.

§Thread Safety

This implementation uses atomic operations for all state management, making it safe to use from multiple threads without external locking.

§Example

use ccxt_core::circuit_breaker::{CircuitBreaker, CircuitBreakerConfig};

let breaker = CircuitBreaker::new(CircuitBreakerConfig::default());

// Before making a request
if let Err(e) = breaker.allow_request() {
    // Circuit is open, handle the error
    println!("Circuit is open: {}", e);
} else {
    // Make the request
    let success = true; // result of the request
    if success {
        breaker.record_success();
    } else {
        breaker.record_failure();
    }
}

Implementations§

Source§

impl CircuitBreaker

Source

pub fn new(config: CircuitBreakerConfig) -> CircuitBreaker

Creates a new circuit breaker with the given configuration.

§Arguments
  • config - Circuit breaker configuration
§Example
use ccxt_core::circuit_breaker::{CircuitBreaker, CircuitBreakerConfig};

let breaker = CircuitBreaker::new(CircuitBreakerConfig::default());
Source

pub fn with_events( config: CircuitBreakerConfig, event_tx: UnboundedSender<CircuitBreakerEvent>, ) -> CircuitBreaker

Creates a new circuit breaker with an event channel for observability.

§Arguments
  • config - Circuit breaker configuration
  • event_tx - Channel sender for circuit breaker events
§Example
use ccxt_core::circuit_breaker::{CircuitBreaker, CircuitBreakerConfig, CircuitBreakerEvent};
use tokio::sync::mpsc;

let (tx, mut rx) = mpsc::unbounded_channel::<CircuitBreakerEvent>();
let breaker = CircuitBreaker::with_events(CircuitBreakerConfig::default(), tx);

// Events will be sent to the channel when state changes occur
Source

pub fn allow_request(&self) -> Result<(), Error>

Checks if a request should be allowed.

§Returns

Returns Ok(()) if the request is allowed. Returns Err(Error::ResourceExhausted) if the circuit is open.

§State Transitions
  • Closed: Always allows requests
  • Open: Checks if reset_timeout has elapsed; if so, transitions to HalfOpen
  • HalfOpen: Allows requests (for testing recovery)
§Example
use ccxt_core::circuit_breaker::{CircuitBreaker, CircuitBreakerConfig};

let breaker = CircuitBreaker::new(CircuitBreakerConfig::default());

match breaker.allow_request() {
    Ok(()) => println!("Request allowed"),
    Err(e) => println!("Request blocked: {}", e),
}
Source

pub fn record_success(&self)

Records a successful request.

§State Transitions
  • Closed: Resets the failure count to 0
  • HalfOpen: Increments success count; if it reaches success_threshold, transitions to Closed
  • Open: No effect
§Example
use ccxt_core::circuit_breaker::{CircuitBreaker, CircuitBreakerConfig};

let breaker = CircuitBreaker::new(CircuitBreakerConfig::default());

// After a successful request
breaker.record_success();
Source

pub fn record_failure(&self)

Records a failed request.

§State Transitions
  • Closed: Increments failure count; if it reaches failure_threshold, transitions to Open
  • HalfOpen: Transitions immediately to Open (test request failed)
  • Open: No effect
§Example
use ccxt_core::circuit_breaker::{CircuitBreaker, CircuitBreakerConfig};

let breaker = CircuitBreaker::new(CircuitBreakerConfig::default());

// After a failed request
breaker.record_failure();
Source

pub fn state(&self) -> CircuitState

Returns the current state of the circuit breaker.

§Example
use ccxt_core::circuit_breaker::{CircuitBreaker, CircuitBreakerConfig, CircuitState};

let breaker = CircuitBreaker::new(CircuitBreakerConfig::default());
assert_eq!(breaker.state(), CircuitState::Closed);
Source

pub fn failure_count(&self) -> u32

Returns the current failure count.

This is primarily useful for monitoring and debugging.

Source

pub fn success_count(&self) -> u32

Returns the current success count (in HalfOpen state).

This is primarily useful for monitoring and debugging.

Source

pub fn config(&self) -> &CircuitBreakerConfig

Returns a reference to the configuration.

Source

pub fn reset(&self)

Resets the circuit breaker to its initial state (Closed).

This can be useful for testing or manual intervention.

§Example
use ccxt_core::circuit_breaker::{CircuitBreaker, CircuitBreakerConfig};

let breaker = CircuitBreaker::new(CircuitBreakerConfig::default());
// ... some operations that opened the circuit ...
breaker.reset();
// Circuit is now Closed again

Trait Implementations§

Source§

impl Debug for CircuitBreaker

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more