pub struct MiddlewareChain { /* private fields */ }
Expand description
Chain of middleware for sequential execution
The MiddlewareChain manages the execution of multiple middleware components in a defined order. It provides error handling and short-circuiting behavior.
§Examples
use turbomcp_client::plugins::middleware::{MiddlewareChain, RequestMiddleware};
use std::sync::Arc;
let mut chain = MiddlewareChain::new();
// chain.add_request_middleware(Arc::new(some_middleware));
// chain.add_response_middleware(Arc::new(other_middleware));
Implementations§
Source§impl MiddlewareChain
impl MiddlewareChain
Sourcepub fn add_request_middleware(&mut self, middleware: Arc<dyn RequestMiddleware>)
pub fn add_request_middleware(&mut self, middleware: Arc<dyn RequestMiddleware>)
Add request middleware to the chain
Middleware will be executed in the order they are added.
§Arguments
middleware
- The request middleware to add
Sourcepub fn add_response_middleware(
&mut self,
middleware: Arc<dyn ResponseMiddleware>,
)
pub fn add_response_middleware( &mut self, middleware: Arc<dyn ResponseMiddleware>, )
Add response middleware to the chain
Middleware will be executed in the order they are added.
§Arguments
middleware
- The response middleware to add
Sourcepub async fn execute_request_chain(
&self,
context: &mut RequestContext,
) -> MiddlewareResult<()>
pub async fn execute_request_chain( &self, context: &mut RequestContext, ) -> MiddlewareResult<()>
Execute the request middleware chain
Processes the request context through all registered request middleware in order. If any middleware returns an error, processing is aborted and the error is returned.
§Arguments
context
- Mutable request context
§Returns
Returns Ok(())
if all middleware succeed, or the first error encountered.
Sourcepub async fn execute_response_chain(
&self,
context: &mut ResponseContext,
) -> MiddlewareResult<()>
pub async fn execute_response_chain( &self, context: &mut ResponseContext, ) -> MiddlewareResult<()>
Execute the response middleware chain
Processes the response context through all registered response middleware in order. Unlike request middleware, this continues execution even if a middleware fails, logging errors but not aborting the chain.
§Arguments
context
- Mutable response context
§Returns
Returns Ok(())
unless all middleware fail, in which case returns the last error.
Sourcepub fn request_middleware_count(&self) -> usize
pub fn request_middleware_count(&self) -> usize
Get the number of request middleware
Sourcepub fn response_middleware_count(&self) -> usize
pub fn response_middleware_count(&self) -> usize
Get the number of response middleware
Sourcepub fn get_request_middleware_names(&self) -> Vec<String>
pub fn get_request_middleware_names(&self) -> Vec<String>
Get names of all request middleware
Sourcepub fn get_response_middleware_names(&self) -> Vec<String>
pub fn get_response_middleware_names(&self) -> Vec<String>
Get names of all response middleware