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
+17 -10
View File
@@ -3,10 +3,10 @@ use tokio::sync::Mutex;
use tonic::transport::Channel;
use crate::common::AnyResult;
use crate::streaming::common::{
MetricsManager, PerformanceMetrics, StreamClientConfig,
};
use crate::protos::shredstream::shredstream_proxy_client::ShredstreamProxyClient;
use crate::streaming::common::{
MetricsManager, PerformanceMetrics, StreamClientConfig, SubscriptionHandle,
};
/// ShredStream gRPC 客户端
#[derive(Clone)]
@@ -15,6 +15,7 @@ pub struct ShredStreamGrpc {
pub config: StreamClientConfig,
pub metrics: Arc<Mutex<PerformanceMetrics>>,
pub metrics_manager: MetricsManager,
pub subscription_handle: Arc<Mutex<Option<SubscriptionHandle>>>,
}
impl ShredStreamGrpc {
@@ -28,18 +29,16 @@ impl ShredStreamGrpc {
let shredstream_client = ShredstreamProxyClient::connect(endpoint.clone()).await?;
let metrics = Arc::new(Mutex::new(PerformanceMetrics::new()));
let config_arc = Arc::new(config.clone());
let metrics_manager = MetricsManager::new(
metrics.clone(),
config_arc,
"ShredStream".to_string()
);
let metrics_manager =
MetricsManager::new(metrics.clone(), config_arc, "ShredStream".to_string());
Ok(Self {
shredstream_client: Arc::new(shredstream_client),
config,
metrics,
metrics_manager,
subscription_handle: Arc::new(Mutex::new(None)),
})
}
@@ -82,4 +81,12 @@ impl ShredStreamGrpc {
pub async fn start_auto_metrics_monitoring(&self) {
self.metrics_manager.start_auto_monitoring().await;
}
/// 停止当前订阅
pub async fn stop(&self) {
let mut handle_guard = self.subscription_handle.lock().await;
if let Some(handle) = handle_guard.take() {
handle.stop();
}
}
}