diff --git a/pgdog/src/backend/pool/connection/mod.rs b/pgdog/src/backend/pool/connection/mod.rs index b35db44df..fcf57d40d 100644 --- a/pgdog/src/backend/pool/connection/mod.rs +++ b/pgdog/src/backend/pool/connection/mod.rs @@ -316,7 +316,9 @@ impl Connection { /// pub(crate) async fn read(&mut self) -> Result { select! { - notification = self.pub_sub.recv() => { + // If we're listening on a NOTIFY channel, wait for a message, and if + // one is available, then we return it here. + notification = self.pub_sub.recv(), if self.pub_sub.is_listening() => { Ok(notification.ok_or_else(|| Error::ProtocolOutOfSync)?.message()) } diff --git a/pgdog/src/backend/pub_sub/client.rs b/pgdog/src/backend/pub_sub/client.rs index 4f246cbad..7caeecf29 100644 --- a/pgdog/src/backend/pub_sub/client.rs +++ b/pgdog/src/backend/pub_sub/client.rs @@ -31,6 +31,14 @@ impl PubSubClient { } } + // Do we have an active `listen` on anything + // This is solely used to determine if we should have an extra select! branch + // within our `read()` method. If we're not listening on anything, + // no point in adding the extra overhead within the branch. + pub(crate) fn is_listening(&self) -> bool { + !self.unlisten.is_empty() + } + /// Listen on a channel. pub(crate) fn listen(&mut self, channel: &str, mut rx: Listener) { let tx = self.tx.clone();