Skip to content

Commit ac59c84

Browse files
committed
Add next_block_for example
1 parent 5065dbf commit ac59c84

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

src/lib.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,24 @@ impl<'conn> Notifications<'conn> {
269269
///
270270
/// If no notifications are pending, blocks for up to `timeout` time, after
271271
/// which an `IoError` with the `TimedOut` kind is returned.
272+
///
273+
/// ## Example
274+
///
275+
/// ```rust,no_run
276+
/// use std::io::{IoError, IoErrorKind};
277+
/// use std::time::Duration;
278+
///
279+
/// use postgres::Error;
280+
///
281+
/// # let conn = postgres::Connection::connect("", &postgres::SslMode::None).unwrap();
282+
/// match conn.notifications().next_block_for(Duration::seconds(2)) {
283+
/// Ok(notification) => println!("notification: {}", notification.payload),
284+
/// Err(Error::IoError(IoError { kind: IoErrorKind::TimedOut, .. })) => {
285+
/// println!("Wait for notification timed out");
286+
/// }
287+
/// Err(e) => println!("Other error: {}", e),
288+
/// }
289+
/// ```
272290
pub fn next_block_for(&mut self, timeout: Duration) -> Result<Notification> {
273291
fn now() -> i64 {
274292
(time::precise_time_ns() / 100_000) as i64
@@ -283,8 +301,7 @@ impl<'conn> Notifications<'conn> {
283301

284302
let end = now() + timeout.num_milliseconds();
285303
loop {
286-
let now = now();
287-
let timeout = max(0, end - now) as u64;
304+
let timeout = max(0, end - now()) as u64;
288305
conn.stream.set_read_timeout(Some(timeout));
289306
match conn.read_one_message() {
290307
Ok(Some(NotificationResponse { pid, channel, payload })) => {

0 commit comments

Comments
 (0)