pub trait Middleware: Send + Sync {
// Required method
fn process<'life0, 'async_trait>(
&'life0 self,
request: Request,
next: Arc<dyn Handler>,
) -> Pin<Box<dyn Future<Output = Result<Response>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
// Provided method
fn should_continue(&self, _request: &Request) -> bool { ... }
}Expand description
Middleware trait for request/response processing.
Uses composition pattern instead of inheritance. Middleware can modify requests before passing to the next handler, or modify responses after the handler processes the request.
Required Methods§
Provided Methods§
Sourcefn should_continue(&self, _request: &Request) -> bool
fn should_continue(&self, _request: &Request) -> bool
Determines whether this middleware should be executed for the given request.
This method enables conditional execution of middleware, allowing the middleware chain to skip unnecessary middleware based on request properties.
§Performance Benefits
By implementing this method, middleware chains can achieve O(k) complexity instead of O(n), where k is the number of middleware that should run, and k <= n (total middleware count).
§Common Use Cases
- Skip authentication middleware for public endpoints
- Skip compression middleware for already compressed responses
- Skip CORS middleware for same-origin requests
- Skip rate limiting for internal/admin requests
§Default Implementation
By default, returns true (always execute), maintaining backward compatibility.