@@ -195,18 +195,26 @@ impl WebSocketConnection {
195
195
stream : S ,
196
196
role : & Role ,
197
197
protocols : Vec < & str > ,
198
+ substream_open_timeout : Duration ,
198
199
) -> Result < ( Negotiated < S > , ProtocolName ) , NegotiationError > {
199
200
tracing:: trace!( target: LOG_TARGET , ?protocols, "negotiating protocols" ) ;
200
201
201
- let ( protocol, socket) = match role {
202
- Role :: Dialer => dialer_select_proto ( stream, protocols, Version :: V1 ) . await ,
203
- Role :: Listener => listener_select_proto ( stream, protocols) . await ,
204
- }
205
- . map_err ( NegotiationError :: MultistreamSelectError ) ?;
206
-
207
- tracing:: trace!( target: LOG_TARGET , ?protocol, "protocol negotiated" ) ;
202
+ match tokio:: time:: timeout ( substream_open_timeout, async move {
203
+ match role {
204
+ Role :: Dialer => dialer_select_proto ( stream, protocols, Version :: V1 ) . await ,
205
+ Role :: Listener => listener_select_proto ( stream, protocols) . await ,
206
+ }
207
+ } )
208
+ . await
209
+ {
210
+ Err ( _) => Err ( NegotiationError :: Timeout ) ,
211
+ Ok ( Err ( error) ) => Err ( NegotiationError :: MultistreamSelectError ( error) ) ,
212
+ Ok ( Ok ( ( protocol, socket) ) ) => {
213
+ tracing:: trace!( target: LOG_TARGET , ?protocol, "protocol negotiated" ) ;
208
214
209
- Ok ( ( socket, ProtocolName :: from ( protocol. to_string ( ) ) ) )
215
+ Ok ( ( socket, ProtocolName :: from ( protocol. to_string ( ) ) ) )
216
+ }
217
+ }
210
218
}
211
219
212
220
/// Open WebSocket connection.
@@ -220,6 +228,7 @@ impl WebSocketConnection {
220
228
yamux_config : crate :: yamux:: Config ,
221
229
max_read_ahead_factor : usize ,
222
230
max_write_buffer_size : usize ,
231
+ substream_open_timeout : Duration ,
223
232
) -> Result < NegotiatedConnection , NegotiationError > {
224
233
tracing:: trace!(
225
234
target: LOG_TARGET ,
@@ -239,6 +248,7 @@ impl WebSocketConnection {
239
248
yamux_config,
240
249
max_read_ahead_factor,
241
250
max_write_buffer_size,
251
+ substream_open_timeout,
242
252
)
243
253
. await
244
254
}
@@ -252,6 +262,7 @@ impl WebSocketConnection {
252
262
yamux_config : crate :: yamux:: Config ,
253
263
max_read_ahead_factor : usize ,
254
264
max_write_buffer_size : usize ,
265
+ substream_open_timeout : Duration ,
255
266
) -> Result < NegotiatedConnection , NegotiationError > {
256
267
let stream = MaybeTlsStream :: Plain ( stream) ;
257
268
@@ -267,6 +278,7 @@ impl WebSocketConnection {
267
278
yamux_config,
268
279
max_read_ahead_factor,
269
280
max_write_buffer_size,
281
+ substream_open_timeout,
270
282
)
271
283
. await
272
284
}
@@ -282,6 +294,7 @@ impl WebSocketConnection {
282
294
yamux_config : crate :: yamux:: Config ,
283
295
max_read_ahead_factor : usize ,
284
296
max_write_buffer_size : usize ,
297
+ substream_open_timeout : Duration ,
285
298
) -> Result < NegotiatedConnection , NegotiationError > {
286
299
tracing:: trace!(
287
300
target: LOG_TARGET ,
@@ -294,7 +307,8 @@ impl WebSocketConnection {
294
307
let stream = BufferedStream :: new ( stream) ;
295
308
296
309
// negotiate `noise`
297
- let ( stream, _) = Self :: negotiate_protocol ( stream, & role, vec ! [ "/noise" ] ) . await ?;
310
+ let ( stream, _) =
311
+ Self :: negotiate_protocol ( stream, & role, vec ! [ "/noise" ] , substream_open_timeout) . await ?;
298
312
299
313
tracing:: trace!(
300
314
target: LOG_TARGET ,
@@ -308,6 +322,7 @@ impl WebSocketConnection {
308
322
role,
309
323
max_read_ahead_factor,
310
324
max_write_buffer_size,
325
+ substream_open_timeout,
311
326
)
312
327
. await ?;
313
328
@@ -321,7 +336,9 @@ impl WebSocketConnection {
321
336
tracing:: trace!( target: LOG_TARGET , "noise handshake done" ) ;
322
337
323
338
// negotiate `yamux`
324
- let ( stream, _) = Self :: negotiate_protocol ( stream, & role, vec ! [ "/yamux/1.0.0" ] ) . await ?;
339
+ let ( stream, _) =
340
+ Self :: negotiate_protocol ( stream, & role, vec ! [ "/yamux/1.0.0" ] , substream_open_timeout)
341
+ . await ?;
325
342
tracing:: trace!( target: LOG_TARGET , "`yamux` negotiated" ) ;
326
343
327
344
let connection = crate :: yamux:: Connection :: new ( stream. inner ( ) , yamux_config, role. into ( ) ) ;
@@ -349,6 +366,7 @@ impl WebSocketConnection {
349
366
permit : Permit ,
350
367
substream_id : SubstreamId ,
351
368
protocols : Vec < ProtocolName > ,
369
+ substream_open_timeout : Duration ,
352
370
) -> Result < NegotiatedSubstream , NegotiationError > {
353
371
tracing:: trace!(
354
372
target: LOG_TARGET ,
@@ -357,7 +375,9 @@ impl WebSocketConnection {
357
375
) ;
358
376
359
377
let protocols = protocols. iter ( ) . map ( |protocol| & * * protocol) . collect :: < Vec < & str > > ( ) ;
360
- let ( io, protocol) = Self :: negotiate_protocol ( stream, & Role :: Listener , protocols) . await ?;
378
+ let ( io, protocol) =
379
+ Self :: negotiate_protocol ( stream, & Role :: Listener , protocols, substream_open_timeout)
380
+ . await ?;
361
381
362
382
tracing:: trace!(
363
383
target: LOG_TARGET ,
@@ -381,6 +401,7 @@ impl WebSocketConnection {
381
401
substream_id : SubstreamId ,
382
402
protocol : ProtocolName ,
383
403
fallback_names : Vec < ProtocolName > ,
404
+ substream_open_timeout : Duration ,
384
405
) -> Result < NegotiatedSubstream , SubstreamError > {
385
406
tracing:: debug!( target: LOG_TARGET , ?protocol, ?substream_id, "open substream" ) ;
386
407
@@ -409,7 +430,9 @@ impl WebSocketConnection {
409
430
. chain ( fallback_names. iter ( ) . map ( |protocol| & * * protocol) )
410
431
. collect ( ) ;
411
432
412
- let ( io, protocol) = Self :: negotiate_protocol ( stream, & Role :: Dialer , protocols) . await ?;
433
+ let ( io, protocol) =
434
+ Self :: negotiate_protocol ( stream, & Role :: Dialer , protocols, substream_open_timeout)
435
+ . await ?;
413
436
414
437
Ok ( NegotiatedSubstream {
415
438
io : io. inner ( ) ,
@@ -438,7 +461,7 @@ impl WebSocketConnection {
438
461
self . pending_substreams. push( Box :: pin( async move {
439
462
match tokio:: time:: timeout(
440
463
substream_open_timeout,
441
- Self :: accept_substream( stream, permit, substream, protocols) ,
464
+ Self :: accept_substream( stream, permit, substream, protocols, substream_open_timeout ) ,
442
465
)
443
466
. await
444
467
{
@@ -537,7 +560,8 @@ impl WebSocketConnection {
537
560
permit,
538
561
substream_id,
539
562
protocol. clone( ) ,
540
- fallback_names
563
+ fallback_names,
564
+ substream_open_timeout
541
565
) ,
542
566
)
543
567
. await
0 commit comments