pub struct CircuitBreakerMiddleware { /* private fields */ }
Expand description
Circuit breaker middleware for fault tolerance.
§Examples
use pmcp::shared::{CircuitBreakerMiddleware, AdvancedMiddleware, MiddlewareContext};
use pmcp::types::{JSONRPCRequest, RequestId};
use std::time::Duration;
// Circuit breaker: 5 failures in 60s window trips for 30s
let circuit_breaker = CircuitBreakerMiddleware::new(
5, // failure_threshold
Duration::from_secs(60), // time_window
Duration::from_secs(30), // timeout_duration
);
let context = MiddlewareContext::default();
let mut request = JSONRPCRequest {
jsonrpc: "2.0".to_string(),
method: "external_service.call".to_string(),
params: Some(serde_json::json!({"data": "test"})),
id: RequestId::from(456i64),
};
// This will fail fast if circuit is open
circuit_breaker.on_request_with_context(&mut request, &context).await?;
Implementations§
Trait Implementations§
Source§impl AdvancedMiddleware for CircuitBreakerMiddleware
impl AdvancedMiddleware for CircuitBreakerMiddleware
Source§fn priority(&self) -> MiddlewarePriority
fn priority(&self) -> MiddlewarePriority
Get middleware priority for execution ordering
Source§fn on_request_with_context<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
request: &'life1 mut JSONRPCRequest,
context: &'life2 MiddlewareContext,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn on_request_with_context<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
request: &'life1 mut JSONRPCRequest,
context: &'life2 MiddlewareContext,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Called before a request is sent with context.
Source§fn on_error<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_error: &'life1 Error,
_context: &'life2 MiddlewareContext,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn on_error<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_error: &'life1 Error,
_context: &'life2 MiddlewareContext,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Called when an error occurs in the chain
Source§fn should_execute<'life0, 'life1, 'async_trait>(
&'life0 self,
_context: &'life1 MiddlewareContext,
) -> Pin<Box<dyn Future<Output = bool> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn should_execute<'life0, 'life1, 'async_trait>(
&'life0 self,
_context: &'life1 MiddlewareContext,
) -> Pin<Box<dyn Future<Output = bool> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Check if middleware should be executed for this context
Source§fn on_response_with_context<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
response: &'life1 mut JSONRPCResponse,
context: &'life2 MiddlewareContext,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn on_response_with_context<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
response: &'life1 mut JSONRPCResponse,
context: &'life2 MiddlewareContext,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Called after a response is received with context.
Source§fn on_send_with_context<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
message: &'life1 TransportMessage,
context: &'life2 MiddlewareContext,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn on_send_with_context<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
message: &'life1 TransportMessage,
context: &'life2 MiddlewareContext,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Called when a message is sent with context.
Source§fn on_receive_with_context<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
message: &'life1 TransportMessage,
context: &'life2 MiddlewareContext,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn on_receive_with_context<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
message: &'life1 TransportMessage,
context: &'life2 MiddlewareContext,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Called when a message is received with context.
Source§fn on_chain_start<'life0, 'life1, 'async_trait>(
&'life0 self,
_context: &'life1 MiddlewareContext,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn on_chain_start<'life0, 'life1, 'async_trait>(
&'life0 self,
_context: &'life1 MiddlewareContext,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Called when middleware chain starts
Source§fn on_chain_complete<'life0, 'life1, 'async_trait>(
&'life0 self,
_context: &'life1 MiddlewareContext,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn on_chain_complete<'life0, 'life1, 'async_trait>(
&'life0 self,
_context: &'life1 MiddlewareContext,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Called when middleware chain completes
Auto Trait Implementations§
impl Freeze for CircuitBreakerMiddleware
impl !RefUnwindSafe for CircuitBreakerMiddleware
impl Send for CircuitBreakerMiddleware
impl Sync for CircuitBreakerMiddleware
impl Unpin for CircuitBreakerMiddleware
impl !UnwindSafe for CircuitBreakerMiddleware
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more