Skip to content
Merged
Show file tree
Hide file tree
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
13 changes: 12 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Bitcoin Peer-to-Peer connections.
#![warn(missing_docs)]
use std::{
collections::HashMap,
sync::{
Expand Down Expand Up @@ -143,8 +145,15 @@ impl ConnectionMetrics {
/// The rate at which a peer sends a particular message
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub enum MessageRate {
/// No message of this type has been received.
NoneReceived,
Ongoing { count: f64, start: Instant },
/// The total count of messages along with the first message of this type.
Ongoing {
/// Total count of messages
count: f64,
/// The time of the first message
start: Instant,
},
}

impl MessageRate {
Expand Down Expand Up @@ -257,7 +266,9 @@ enum OutboundPing {
LastReceived { then: Instant },
}

/// DNS seed provider
pub trait SeedsExt {
/// List DNS seeds
fn seeds(&self) -> Vec<&str>;
}

Expand Down
18 changes: 17 additions & 1 deletion src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ use crate::{
ConnectionMetrics, OutboundPing, Preferences, TimedMessage, TimedMessages,
};

/// Maximum amount of time the peer has to seed a message after idling.
pub const READ_TIMEOUT: Duration = Duration::from_secs(60);
/// The interval to send a new ping message.
pub const PING_INTERVAL: Duration = Duration::from_secs(30);
/// The initial TCP handshake timeout.
pub const TCP_TIMEOUT: Duration = Duration::from_secs(2);

/// Open or begin a connection to an inbound or outbound peer.
pub trait ConnectionExt: Send + Sync {
Expand Down Expand Up @@ -58,7 +62,7 @@ impl ConnectionExt for ConnectionConfig {
to: impl Into<SocketAddr>,
timeout_params: TimeoutParams,
) -> Result<(ConnectionWriter, ConnectionReader, ConnectionMetrics), Error> {
let tcp_stream = TcpStream::connect(to.into())?;
let tcp_stream = TcpStream::connect_timeout(&to.into(), timeout_params.tcp)?;
tcp_stream.set_read_timeout(timeout_params.read)?;
tcp_stream.set_write_timeout(timeout_params.write)?;
Self::handshake(self, tcp_stream, timeout_params)
Expand Down Expand Up @@ -150,26 +154,37 @@ impl ConnectionExt for ConnectionConfig {
}
}

/// Configurations for ending a connection due to inactivity.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TimeoutParams {
read: Option<Duration>,
write: Option<Duration>,
tcp: Duration,
ping_interval: Duration,
}

impl TimeoutParams {
/// Construct new timeout parameters
pub fn new() -> Self {
Self::default()
}

/// Set the time a peer has until they have must sent a message.
pub fn read_timeout(&mut self, timeout: Duration) {
self.read = Some(timeout)
}

/// Maximum amount of time it should take to write a message.
pub fn write_timeout(&mut self, timeout: Duration) {
self.write = Some(timeout)
}

/// The initial TCP handshake timeout.
pub fn tcp_handshake_timeout(&mut self, timeout: Duration) {
self.tcp = timeout;
}

/// How often is this peer pinged for activity
pub fn ping_interval(&mut self, every: Duration) {
self.ping_interval = every
}
Expand All @@ -180,6 +195,7 @@ impl Default for TimeoutParams {
Self {
read: Some(READ_TIMEOUT),
write: None,
tcp: TCP_TIMEOUT,
ping_interval: PING_INTERVAL,
}
}
Expand Down
Loading