Skip to content

Commit

Permalink
refactor: Simplify timeout calculation in Poll::poll()
Browse files Browse the repository at this point in the history
  • Loading branch information
kenoss authored Nov 21, 2024
1 parent 9afad51 commit 5c90098
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/sys.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
use std::{cell::RefCell, collections::HashMap, rc::Rc, sync::Arc, time::Duration};
use std::{
cell::RefCell,
collections::HashMap,
rc::Rc,
sync::Arc,
time::{Duration, Instant},
};

#[cfg(unix)]
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd as Borrowed, RawFd as Raw};
Expand Down Expand Up @@ -215,17 +221,15 @@ impl Poll {
}

pub(crate) fn poll(&self, mut timeout: Option<Duration>) -> crate::Result<Vec<PollEvent>> {
let now = std::time::Instant::now();

// Adjust the timeout for the timers.
if let Some(next_timeout) = self.timers.borrow().next_deadline() {
if next_timeout <= now {
timeout = Some(Duration::ZERO);
} else if let Some(deadline) = timeout {
timeout = Some(std::cmp::min(deadline, next_timeout - now));
} else {
timeout = Some(next_timeout - now);
}
let next_timeout = self
.timers
.borrow()
.next_deadline()
.map(|deadline| deadline.saturating_duration_since(Instant::now()));
timeout = match (timeout, next_timeout) {
(Some(timeout), Some(next_timeout)) => Some(timeout.min(next_timeout)),
_ => timeout.or(next_timeout),
};

let mut events = self.events.borrow_mut();
Expand Down Expand Up @@ -263,8 +267,7 @@ impl Poll {

drop(events);

// Update 'now' as some time may have elapsed in poll()
let now = std::time::Instant::now();
let now = Instant::now();
let mut timers = self.timers.borrow_mut();
while let Some((_, token)) = timers.next_expired(now) {
poll_events.push(PollEvent {
Expand Down

0 comments on commit 5c90098

Please sign in to comment.