@@ -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.
2728pub const READ_TIMEOUT : Duration = Duration :: from_secs ( 60 ) ;
29+ /// The interval to send a new ping message.
2830pub 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.
3135pub 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 ) ]
154159pub struct TimeoutParams {
155160 read : Option < Duration > ,
156161 write : Option < Duration > ,
162+ tcp : Duration ,
157163 ping_interval : Duration ,
158164}
159165
160166impl 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