sol_parser_sdk/common/
subscription.rs

1use tokio::task::JoinHandle;
2
3/// Subscription handle for managing and stopping subscriptions
4pub struct SubscriptionHandle {
5    stream_handle: JoinHandle<()>,
6    event_handle: Option<JoinHandle<()>>,
7    metrics_handle: Option<JoinHandle<()>>,
8}
9
10impl SubscriptionHandle {
11    /// Create a new subscription handle
12    pub fn new(
13        stream_handle: JoinHandle<()>,
14        event_handle: Option<JoinHandle<()>>,
15        metrics_handle: Option<JoinHandle<()>>,
16    ) -> Self {
17        Self { stream_handle, event_handle, metrics_handle }
18    }
19
20    /// Stop subscription and abort all related tasks
21    pub fn stop(self) {
22        self.stream_handle.abort();
23        if let Some(handle) = self.event_handle {
24            handle.abort();
25        }
26        if let Some(handle) = self.metrics_handle {
27            handle.abort();
28        }
29    }
30
31    /// Asynchronously wait for all tasks to complete
32    pub async fn join(self) -> Result<(), tokio::task::JoinError> {
33        let _ = self.stream_handle.await;
34        if let Some(handle) = self.event_handle {
35            let _ = handle.await;
36        }
37        if let Some(handle) = self.metrics_handle {
38            let _ = handle.await;
39        }
40        Ok(())
41    }
42}