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

add timeout to the Framed flush operation. #540

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
24 changes: 21 additions & 3 deletions mycelium/src/peer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use futures::{SinkExt, StreamExt};
use std::time::Duration;
use std::{
error::Error,
io,
Expand All @@ -7,6 +8,7 @@ use std::{
Arc, RwLock, Weak,
},
};
use tokio::time::timeout;
use tokio::{
select,
sync::{mpsc, Notify},
Expand Down Expand Up @@ -90,6 +92,10 @@ impl Peer {
}),
};

// timeout to flush buffered packets.
// we need this to avoid waiting indefinitely for the peer to flush packets.
const FLUSH_TIMEOUT: Duration = Duration::from_secs(180);

// Framed for peer
// Used to send and receive packets from a TCP stream
let mut framed = Framed::new(connection, packet::Codec::new());
Expand Down Expand Up @@ -156,7 +162,13 @@ impl Peer {
}
}

if let Err(e) = framed.flush().await {
if let Err(e) = match timeout(FLUSH_TIMEOUT, framed.flush()).await {
Ok(result) => result,
Err(e) => {
error!("Timeout while flushing buffered peer connection data packets. elapsed time:{}",e);
break;
}
} {
error!("Failed to flush buffered peer connection data packets: {e}");
break
}
Expand All @@ -182,8 +194,14 @@ impl Peer {
}
}

if let Err(e) = framed.flush().await {
error!("Failed to flush buffered peer connection control packets: {e}");
if let Err(e) = match timeout(FLUSH_TIMEOUT, framed.flush()).await {
Ok(result) => result,
Err(e) => {
error!("Timeout while flushing buffered peer connection data packets.elapsed time:{}",e);
break;
}
} {
error!("Failed to flush buffered peer connection data packets: {e}");
break
}
}
Expand Down
Loading