@@ -3,9 +3,9 @@ use super::store::Store;
33use crate :: bee_msg:: misc:: AuthenticateChannel ;
44use crate :: bee_msg:: { Header , Msg , deserialize_body, deserialize_header, serialize} ;
55use crate :: bee_serde:: { Deserializable , Serializable } ;
6- use crate :: conn:: TCP_BUF_LEN ;
76use crate :: conn:: store:: StoredStream ;
87use crate :: conn:: stream:: Stream ;
8+ use crate :: conn:: { DEFAULT_TIME_LIMIT , TCP_BUF_LEN } ;
99use crate :: types:: { AuthSecret , Uid } ;
1010use anyhow:: { Context , Result , bail} ;
1111use std:: fmt:: Debug ;
@@ -58,7 +58,9 @@ impl Pool {
5858 let mut buf = self . store . pop_buf_or_create ( ) ;
5959
6060 let msg_len = serialize ( msg, & mut buf) ?;
61- let resp_header = self . comm_stream ( node_uid, & mut buf, msg_len, true ) . await ?;
61+ let resp_header = self
62+ . comm_stream ( node_uid, & mut buf, msg_len, Some ( M :: RESPONSE_TIME_LIMIT ) )
63+ . await ?;
6264 let resp_msg = deserialize_body ( & resp_header, & buf[ Header :: LEN ..] ) ?;
6365
6466 self . store . push_buf ( buf) ;
@@ -75,7 +77,7 @@ impl Pool {
7577 let mut buf = self . store . pop_buf_or_create ( ) ;
7678
7779 let msg_len = serialize ( msg, & mut buf) ?;
78- self . comm_stream ( node_uid, & mut buf, msg_len, false ) . await ?;
80+ self . comm_stream ( node_uid, & mut buf, msg_len, None ) . await ?;
7981
8082 self . store . push_buf ( buf) ;
8183
@@ -94,19 +96,21 @@ impl Pool {
9496 /// 2. Get a permit that allows opening a new stream. Try to open a new stream using the
9597 /// available addresses.
9698 /// 3. Pop an open stream from the store, waiting until one gets available.
99+ ///
100+ /// If ``response_time_limit`` is set to `Some(t)`, a response is expected.
97101 async fn comm_stream (
98102 & self ,
99103 node_uid : Uid ,
100104 buf : & mut [ u8 ] ,
101105 send_len : usize ,
102- expect_response : bool ,
106+ response_time_limit : Option < Duration > ,
103107 ) -> Result < Header > {
104108 debug_assert_eq ! ( buf. len( ) , TCP_BUF_LEN ) ;
105109
106110 // 1. Pop open streams until communication succeeds or none are left
107111 while let Some ( stream) = self . store . try_pop_stream ( node_uid) {
108112 match self
109- . write_and_read_stream ( buf, stream, send_len, expect_response )
113+ . write_and_read_stream ( buf, stream, send_len, response_time_limit )
110114 . await
111115 {
112116 Ok ( header) => return Ok ( header) ,
@@ -132,7 +136,7 @@ impl Pool {
132136 continue ;
133137 }
134138
135- match Stream :: connect_tcp ( addr) . await {
139+ match Stream :: connect_tcp ( addr, DEFAULT_TIME_LIMIT ) . await {
136140 Ok ( stream) => {
137141 let mut stream = StoredStream :: from_stream ( stream, permit) ;
138142
@@ -152,7 +156,7 @@ impl Pool {
152156
153157 stream
154158 . as_mut ( )
155- . write_all ( & auth_buf[ 0 ..msg_len] )
159+ . write_all ( & auth_buf[ 0 ..msg_len] , DEFAULT_TIME_LIMIT )
156160 . await
157161 . with_context ( err_context) ?;
158162
@@ -162,7 +166,7 @@ impl Pool {
162166 // Communication using the newly opened stream should usually not fail. If
163167 // it does, abort. It might be better to just try the next address though.
164168 let resp_header = self
165- . write_and_read_stream ( buf, stream, send_len, expect_response )
169+ . write_and_read_stream ( buf, stream, send_len, response_time_limit )
166170 . await
167171 . with_context ( err_context) ?;
168172
@@ -189,7 +193,7 @@ impl Pool {
189193 } ) ?;
190194
191195 let resp_header = self
192- . write_and_read_stream ( buf, stream, send_len, expect_response )
196+ . write_and_read_stream ( buf, stream, send_len, response_time_limit )
193197 . await
194198 . with_context ( || {
195199 format ! ( "Communication using existing stream to node with uid {node_uid} failed" )
@@ -205,20 +209,27 @@ impl Pool {
205209 buf : & mut [ u8 ] ,
206210 mut stream : StoredStream < Uid > ,
207211 send_len : usize ,
208- expect_response : bool ,
212+ response_time_limit : Option < Duration > ,
209213 ) -> Result < Header > {
210- stream. as_mut ( ) . write_all ( & buf[ 0 ..send_len] ) . await ?;
214+ stream
215+ . as_mut ( )
216+ . write_all ( & buf[ 0 ..send_len] , DEFAULT_TIME_LIMIT )
217+ . await ?;
211218
212- let header = if expect_response {
219+ let header = if let Some ( tl ) = response_time_limit {
213220 // Read header
214- stream. as_mut ( ) . read_exact ( & mut buf[ 0 ..Header :: LEN ] ) . await ?;
221+ stream
222+ . as_mut ( )
223+ . read_exact ( & mut buf[ 0 ..Header :: LEN ] , tl)
224+ . await ?;
215225 let header = deserialize_header ( & buf[ 0 ..Header :: LEN ] ) ?;
216226
217227 // Read body
218228 stream
219229 . as_mut ( )
220- . read_exact ( & mut buf[ Header :: LEN ..header. msg_len ( ) ] )
230+ . read_exact ( & mut buf[ Header :: LEN ..header. msg_len ( ) ] , tl )
221231 . await ?;
232+
222233 header
223234 } else {
224235 Header :: default ( )
0 commit comments