- relay token 必设 DF_RELAY_TOKEN 环境变量,移除硬编码默认 - AiProviderRecord Debug 脱敏 api_key/model_configs/config - ScriptNode 危险命令告警(rm -rf/DROP TABLE 等) - bind_directory 路径规范化,拒绝含 .. 的原始路径 - create_project 目录不存在时自动创建 - probe_pwsh OnceLock 全局缓存,异步探测不阻塞 tokio - retry jitter 改 rand,范围扩大到 ±50% - EventBus send 错误改 tracing::warn - StateMachine 文档警告不得在 await 期间持锁
58 lines
1.3 KiB
Rust
58 lines
1.3 KiB
Rust
//! 事件总线 — 基于 tokio::sync::broadcast 的发布/订阅
|
|
|
|
use tokio::sync::broadcast;
|
|
use df_types::events::WorkflowEvent;
|
|
|
|
/// 事件总线
|
|
#[derive(Debug)]
|
|
pub struct EventBus {
|
|
sender: broadcast::Sender<WorkflowEvent>,
|
|
}
|
|
|
|
/// 事件订阅者
|
|
pub type EventSubscriber = broadcast::Receiver<WorkflowEvent>;
|
|
|
|
/// 默认事件通道容量
|
|
const DEFAULT_CAPACITY: usize = 256;
|
|
|
|
impl EventBus {
|
|
/// 创建事件总线
|
|
pub fn new() -> Self {
|
|
let (sender, _) = broadcast::channel(DEFAULT_CAPACITY);
|
|
Self { sender }
|
|
}
|
|
|
|
/// 创建指定容量的事件总线
|
|
pub fn with_capacity(capacity: usize) -> Self {
|
|
let (sender, _) = broadcast::channel(capacity);
|
|
Self { sender }
|
|
}
|
|
|
|
/// 发送事件
|
|
pub async fn send(&self, event: WorkflowEvent) {
|
|
// broadcast::send 是同步的;接收者已关闭时记录警告
|
|
if let Err(e) = self.sender.send(event) {
|
|
tracing::warn!("EventBus 发送事件失败: {}", e);
|
|
}
|
|
}
|
|
|
|
/// 订阅事件
|
|
pub fn subscribe(&self) -> EventSubscriber {
|
|
self.sender.subscribe()
|
|
}
|
|
}
|
|
|
|
impl Default for EventBus {
|
|
fn default() -> Self {
|
|
Self::new()
|
|
}
|
|
}
|
|
|
|
impl Clone for EventBus {
|
|
fn clone(&self) -> Self {
|
|
Self {
|
|
sender: self.sender.clone(),
|
|
}
|
|
}
|
|
}
|