Skip to content

Commit 5065dbf

Browse files
committed
Use monotonic time for timeout logic
1 parent cb8380e commit 5065dbf

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

src/lib.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,17 +270,21 @@ impl<'conn> Notifications<'conn> {
270270
/// If no notifications are pending, blocks for up to `timeout` time, after
271271
/// which an `IoError` with the `TimedOut` kind is returned.
272272
pub fn next_block_for(&mut self, timeout: Duration) -> Result<Notification> {
273+
fn now() -> i64 {
274+
(time::precise_time_ns() / 100_000) as i64
275+
}
276+
273277
if let Some(notification) = self.next() {
274278
return Ok(notification);
275279
}
276280

277281
let mut conn = self.conn.conn.borrow_mut();
278282
check_desync!(conn);
279283

280-
let end = time::now().to_timespec() + timeout;
284+
let end = now() + timeout.num_milliseconds();
281285
loop {
282-
let now = time::now().to_timespec();
283-
let timeout = max(0, (end - now).num_milliseconds()) as u64;
286+
let now = now();
287+
let timeout = max(0, end - now) as u64;
284288
conn.stream.set_read_timeout(Some(timeout));
285289
match conn.read_one_message() {
286290
Ok(Some(NotificationResponse { pid, channel, payload })) => {

0 commit comments

Comments
 (0)