Skip to main content

sentinel_driver/connection/
notify_impl.rs

1use super::{notify, Connection, Notification, Result};
2
3impl Connection {
4    /// Subscribe to LISTEN/NOTIFY on a channel.
5    pub async fn listen(&mut self, channel: &str) -> Result<()> {
6        notify::listen(&mut self.conn, channel).await
7    }
8
9    /// Unsubscribe from a channel.
10    pub async fn unlisten(&mut self, channel: &str) -> Result<()> {
11        notify::unlisten(&mut self.conn, channel).await
12    }
13
14    /// Unsubscribe from all channels.
15    pub async fn unlisten_all(&mut self) -> Result<()> {
16        notify::unlisten_all(&mut self.conn).await
17    }
18
19    /// Send a notification on a channel.
20    pub async fn notify(&mut self, channel: &str, payload: &str) -> Result<()> {
21        notify::notify(&mut self.conn, channel, payload).await
22    }
23
24    /// Wait for the next LISTEN/NOTIFY notification.
25    ///
26    /// Blocks until a notification arrives on any subscribed channel.
27    ///
28    /// Emits a [`crate::Event::Notification`] event on success.
29    ///
30    /// Note: in-band NoticeResponse messages during regular queries are not
31    /// emitted as Notice events — the driver's query path doesn't yet handle
32    /// them gracefully. Startup-time notices log via tracing::debug only
33    /// (instrumentation isn't installed until after startup).
34    pub async fn wait_for_notification(&mut self) -> Result<Notification> {
35        let res = notify::wait_for_notification(&mut self.conn).await;
36        if let Ok(n) = &res {
37            self.instr().on_event(&crate::Event::Notification {
38                channel: &n.channel,
39                payload: &n.payload,
40                pid: n.process_id,
41            });
42        }
43        res
44    }
45}