diff --git a/p2p/src/conn.rs b/p2p/src/conn.rs index 1e704b0d7..121f9deb5 100644 --- a/p2p/src/conn.rs +++ b/p2p/src/conn.rs @@ -38,6 +38,8 @@ use std::{ thread::{self, JoinHandle}, }; +pub const SEND_CHANNEL_CAP: usize = 100; + const HEADER_IO_TIMEOUT: Duration = Duration::from_millis(2000); const CHANNEL_TIMEOUT: Duration = Duration::from_millis(1000); const BODY_IO_TIMEOUT: Duration = Duration::from_millis(60000); @@ -130,8 +132,6 @@ impl<'a> Message<'a> { } } -pub const SEND_CHANNEL_CAP: usize = 100; - pub struct StopHandle { /// Channel to close the connection stopped: Arc, @@ -178,9 +178,24 @@ pub struct ConnHandle { } impl ConnHandle { + /// Send msg via the synchronous, bounded channel (sync_sender). + /// Two possible failure cases - + /// * Disconnected: Propagate this up to the caller so the peer connection can be closed. + /// * Full: Our internal msg buffer is full. This is not a problem with the peer connection + /// and we do not want to close the connection. We drop the msg rather than blocking here. + /// If the buffer is full because there is an underlying issue with the peer + /// and potentially the peer connection. We assume this will be handled at the peer level. pub fn send(&self, msg: Msg) -> Result<(), Error> { - self.send_channel.try_send(msg)?; - Ok(()) + match self.send_channel.try_send(msg) { + Ok(()) => Ok(()), + Err(mpsc::TrySendError::Disconnected(_)) => { + Err(Error::Send("try_send disconnected".to_owned())) + } + Err(mpsc::TrySendError::Full(_)) => { + debug!("conn_handle: try_send but buffer is full, dropping msg"); + Ok(()) + } + } } } diff --git a/p2p/src/types.rs b/p2p/src/types.rs index a0da98a50..1a6ceddd1 100644 --- a/p2p/src/types.rs +++ b/p2p/src/types.rs @@ -18,8 +18,6 @@ use std::fs::File; use std::io::{self, Read}; use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6}; use std::path::PathBuf; - -use std::sync::mpsc; use std::sync::Arc; use chrono::prelude::*; @@ -106,11 +104,6 @@ impl From for Error { Error::Connection(e) } } -impl From> for Error { - fn from(e: mpsc::TrySendError) -> Error { - Error::Send(e.to_string()) - } -} #[derive(Debug, Clone, Copy, Serialize, Deserialize)] pub struct PeerAddr(pub SocketAddr);