feat: add subscription management and graceful shutdown support (v0.3.6)

- Add SubscriptionHandle for managing stream lifecycle
- Implement graceful shutdown with stop() methods
- Update documentation with new features
- Bump version to 0.3.6
This commit is contained in:
ysq
2025-08-21 15:47:31 +08:00
parent 372bb765af
commit 9ec7194d2a
10 changed files with 238 additions and 68 deletions
+4 -3
View File
@@ -199,14 +199,14 @@ impl MetricsManager {
}
/// 启动自动性能监控任务
pub async fn start_auto_monitoring(&self) {
pub async fn start_auto_monitoring(&self) -> Option<tokio::task::JoinHandle<()>> {
// 检查是否启用性能监控
if !self.config.enable_metrics {
return; // 如果未启用性能监控,不启动监控任务
return None; // 如果未启用性能监控,不启动监控任务
}
let metrics_manager = self.clone();
tokio::spawn(async move {
let handle = tokio::spawn(async move {
let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(
DEFAULT_METRICS_PRINT_INTERVAL_SECONDS,
));
@@ -215,6 +215,7 @@ impl MetricsManager {
metrics_manager.print_metrics().await;
}
});
Some(handle)
}
/// 更新处理次数
+2
View File
@@ -3,9 +3,11 @@ pub mod config;
pub mod metrics;
pub mod batch;
pub mod constants;
pub mod subscription;
// 重新导出主要类型
pub use config::*;
pub use metrics::*;
pub use batch::*;
pub use constants::*;
pub use subscription::*;
+38
View File
@@ -0,0 +1,38 @@
use tokio::task::JoinHandle;
/// Subscription handle for managing and stopping subscriptions
pub struct SubscriptionHandle {
stream_handle: JoinHandle<()>,
event_handle: JoinHandle<()>,
metrics_handle: Option<JoinHandle<()>>,
}
impl SubscriptionHandle {
/// Create a new subscription handle
pub fn new(
stream_handle: JoinHandle<()>,
event_handle: JoinHandle<()>,
metrics_handle: Option<JoinHandle<()>>,
) -> Self {
Self { stream_handle, event_handle, metrics_handle }
}
/// Stop subscription and abort all related tasks
pub fn stop(self) {
self.stream_handle.abort();
self.event_handle.abort();
if let Some(handle) = self.metrics_handle {
handle.abort();
}
}
/// Asynchronously wait for all tasks to complete
pub async fn join(self) -> Result<(), tokio::task::JoinError> {
let _ = self.stream_handle.await;
let _ = self.event_handle.await;
if let Some(handle) = self.metrics_handle {
let _ = handle.await;
}
Ok(())
}
}