mirror of
https://github.com/launchbadge/sqlx.git
synced 2026-03-19 08:39:44 +00:00
Add PgListener::next_buffered(), to support batch processing of notifications (#3560)
* Implement and test PgListener::try_recv_buffered(). * rustfmt * Fix warnings. * Fix test. * Rename try_recv_buffered() -> next_buffered().
This commit is contained in:
@@ -255,8 +255,8 @@ impl PgListener {
|
||||
pub async fn try_recv(&mut self) -> Result<Option<PgNotification>, Error> {
|
||||
// Flush the buffer first, if anything
|
||||
// This would only fill up if this listener is used as a connection
|
||||
if let Ok(Some(notification)) = self.buffer_rx.try_next() {
|
||||
return Ok(Some(PgNotification(notification)));
|
||||
if let Some(notification) = self.next_buffered() {
|
||||
return Ok(Some(notification));
|
||||
}
|
||||
|
||||
// Fetch our `CloseEvent` listener, if applicable.
|
||||
@@ -319,6 +319,19 @@ impl PgListener {
|
||||
}
|
||||
}
|
||||
|
||||
/// Receives the next notification that already exists in the connection buffer, if any.
|
||||
///
|
||||
/// This is similar to `try_recv`, except it will not wait if the connection has not yet received a notification.
|
||||
///
|
||||
/// This is helpful if you want to retrieve all buffered notifications and process them in batches.
|
||||
pub fn next_buffered(&mut self) -> Option<PgNotification> {
|
||||
if let Ok(Some(notification)) = self.buffer_rx.try_next() {
|
||||
Some(PgNotification(notification))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Consume this listener, returning a `Stream` of notifications.
|
||||
///
|
||||
/// The backing connection will be automatically reconnected should it be lost.
|
||||
|
||||
Reference in New Issue
Block a user