Skip to content

Commit 4f67fbe

Browse files
authored
Merge pull request #11 from 2140-dev/8-16-tcp-timeout
TCP handshake timeout
2 parents 7584002 + 796ec35 commit 4f67fbe

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/lib.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Bitcoin Peer-to-Peer connections.
2+
#![warn(missing_docs)]
13
use std::{
24
collections::HashMap,
35
sync::{
@@ -143,8 +145,15 @@ impl ConnectionMetrics {
143145
/// The rate at which a peer sends a particular message
144146
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
145147
pub enum MessageRate {
148+
/// No message of this type has been received.
146149
NoneReceived,
147-
Ongoing { count: f64, start: Instant },
150+
/// The total count of messages along with the first message of this type.
151+
Ongoing {
152+
/// Total count of messages
153+
count: f64,
154+
/// The time of the first message
155+
start: Instant,
156+
},
148157
}
149158

150159
impl MessageRate {
@@ -257,7 +266,9 @@ enum OutboundPing {
257266
LastReceived { then: Instant },
258267
}
259268

269+
/// DNS seed provider
260270
pub trait SeedsExt {
271+
/// List DNS seeds
261272
fn seeds(&self) -> Vec<&str>;
262273
}
263274

src/net.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@ use crate::{
2424
ConnectionMetrics, OutboundPing, Preferences, TimedMessage, TimedMessages,
2525
};
2626

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

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

157+
/// Configurations for ending a connection due to inactivity.
153158
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
154159
pub struct TimeoutParams {
155160
read: Option<Duration>,
156161
write: Option<Duration>,
162+
tcp: Duration,
157163
ping_interval: Duration,
158164
}
159165

160166
impl TimeoutParams {
167+
/// Construct new timeout parameters
161168
pub fn new() -> Self {
162169
Self::default()
163170
}
164171

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

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

182+
/// The initial TCP handshake timeout.
183+
pub fn tcp_handshake_timeout(&mut self, timeout: Duration) {
184+
self.tcp = timeout;
185+
}
186+
187+
/// How often is this peer pinged for activity
173188
pub fn ping_interval(&mut self, every: Duration) {
174189
self.ping_interval = every
175190
}
@@ -180,6 +195,7 @@ impl Default for TimeoutParams {
180195
Self {
181196
read: Some(READ_TIMEOUT),
182197
write: None,
198+
tcp: TCP_TIMEOUT,
183199
ping_interval: PING_INTERVAL,
184200
}
185201
}

0 commit comments

Comments
 (0)