Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make initial poll loop less aggressive #15

Merged
merged 1 commit into from
Apr 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 29 additions & 13 deletions src/linux.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{IfEvent, IpNet, Ipv4Net, Ipv6Net};
use fnv::FnvHashSet;
use futures::channel::mpsc::UnboundedReceiver;
use futures::future::FutureExt;
use futures::future::Either;
use futures::stream::{Stream, TryStreamExt};
use rtnetlink::constants::{RTMGRP_IPV4_IFADDR, RTMGRP_IPV6_IFADDR};
use rtnetlink::packet::address::nlas::Nla;
Expand All @@ -12,6 +12,7 @@ use std::collections::VecDeque;
use std::future::Future;
use std::io::{Error, ErrorKind, Result};
use std::net::{Ipv4Addr, Ipv6Addr};
use std::ops::DerefMut;
use std::pin::Pin;
use std::task::{Context, Poll};

Expand Down Expand Up @@ -41,19 +42,28 @@ impl IfWatcher {
let mut queue = VecDeque::default();

loop {
futures::select! {
msg = stream.try_next().fuse() => match msg {
Ok(Some(msg)) => {
for net in iter_nets(msg) {
if addrs.insert(net) {
queue.push_back(IfEvent::Up(net));
let fut = futures::future::select(conn, stream.try_next());
match fut.await {
Either::Left(_) => {
return Err(std::io::Error::new(
ErrorKind::BrokenPipe,
"rtnetlink socket closed",
))
}
Either::Right((x, c)) => {
conn = c;
match x {
Ok(Some(msg)) => {
for net in iter_nets(msg) {
if addrs.insert(net) {
queue.push_back(IfEvent::Up(net));
}
}
}
},
Ok(None) => break,
Err(err) => return Err(Error::new(ErrorKind::Other, err)),
},
_r = (&mut conn).fuse() => {}
Ok(None) => break,
Err(err) => return Err(Error::new(ErrorKind::Other, err)),
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this should be equivalent. is it really easier to read? in the special case that you only need to poll two futures, maybe, maybe not. for polling more futures/streams select! macro is the way to go.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rust async poses an interesting challenge for developers due to the interactions with other language features. Having all .await points exposed and clearly visible in an async scope is the only way to make that manageable — a macro that expands to .await within the current async scope is actively harmful. Imagine having to debug compiler error messages that some faraway type is not Sync?

}
}
Ok(Self {
Expand Down Expand Up @@ -89,7 +99,13 @@ impl Future for IfWatcher {
type Output = Result<IfEvent>;

fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
while Pin::new(&mut self.conn).poll(cx).is_ready() {}
log::trace!("polling IfWatcher {:p}", self.deref_mut());
if Pin::new(&mut self.conn).poll(cx).is_ready() {
return Poll::Ready(Err(std::io::Error::new(
ErrorKind::BrokenPipe,
"rtnetlink socket closed",
)));
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change looks fine to me

while let Poll::Ready(Some((message, _))) = Pin::new(&mut self.messages).poll_next(cx) {
match message.payload {
NetlinkPayload::Error(err) => return Poll::Ready(Err(err.to_io())),
Expand Down