pub trait Middleware: Send + Sync {
// Required method
fn name(&self) -> &str;
// Provided methods
fn before<'life0, 'life1, 'async_trait>(
&'life0 self,
task_name: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<(), MiddlewareError>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait { ... }
fn after<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
task_name: &'life1 str,
result: &'life2 Result<(), Box<dyn Error + Send + Sync>>,
) -> Pin<Box<dyn Future<Output = Result<(), MiddlewareError>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait { ... }
}Expand description
中间件抽象
支持在任务执行前后插入自定义逻辑
§实现说明
- 使用 async-trait 宏支持 trait objects
- 所有中间件必须实现此 trait
- 中间件按照注册顺序执行
§示例
ⓘ
use flare_core_runtime::middleware::Middleware;
use flare_core_runtime::error::MiddlewareError;
struct LoggingMiddleware;
impl Middleware for LoggingMiddleware {
fn name(&self) -> &str {
"logging"
}
async fn before(&self, task_name: &str) -> Result<(), MiddlewareError> {
tracing::info!("Task {} starting", task_name);
Ok(())
}
async fn after(&self, task_name: &str, result: &Result<(), Box<dyn std::error::Error + Send + Sync>>) -> Result<(), MiddlewareError> {
tracing::info!("Task {} completed", task_name);
Ok(())
}
}Required Methods§
Provided Methods§
Sourcefn before<'life0, 'life1, 'async_trait>(
&'life0 self,
task_name: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<(), MiddlewareError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn before<'life0, 'life1, 'async_trait>(
&'life0 self,
task_name: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<(), MiddlewareError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Sourcefn after<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
task_name: &'life1 str,
result: &'life2 Result<(), Box<dyn Error + Send + Sync>>,
) -> Pin<Box<dyn Future<Output = Result<(), MiddlewareError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn after<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
task_name: &'life1 str,
result: &'life2 Result<(), Box<dyn Error + Send + Sync>>,
) -> Pin<Box<dyn Future<Output = Result<(), MiddlewareError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".