@@ -40,6 +40,9 @@ const IO_ERROR_RETRIES: u8 = 3;
40
40
/// Maximum time given to the handler to perform shutdown operations.
41
41
const SHUTDOWN_TIMEOUT_SECS : u8 = 15 ;
42
42
43
+ /// Maximum number of simultaneous inbound substreams we keep for this peer.
44
+ const MAX_INBOUND_SUBSTREAMS : usize = 32 ;
45
+
43
46
/// Identifier of inbound and outbound substreams from the handler's perspective.
44
47
#[ derive( Debug , Clone , Copy , Hash , Eq , PartialEq ) ]
45
48
pub struct SubstreamId ( usize ) ;
@@ -241,7 +244,7 @@ where
241
244
// We now drive to completion communications already dialed/established
242
245
while let Some ( ( id, req) ) = self . dial_queue . pop ( ) {
243
246
self . events_out . push ( Err ( HandlerErr :: Outbound {
244
- error : RPCError :: HandlerRejected ,
247
+ error : RPCError :: Disconnected ,
245
248
proto : req. protocol ( ) ,
246
249
id,
247
250
} ) ) ;
@@ -265,7 +268,7 @@ where
265
268
self . dial_queue . push ( ( id, req) ) ;
266
269
}
267
270
_ => self . events_out . push ( Err ( HandlerErr :: Outbound {
268
- error : RPCError :: HandlerRejected ,
271
+ error : RPCError :: Disconnected ,
269
272
proto : req. protocol ( ) ,
270
273
id,
271
274
} ) ) ,
@@ -339,23 +342,32 @@ where
339
342
340
343
// store requests that expect responses
341
344
if expected_responses > 0 {
342
- // Store the stream and tag the output.
343
- let delay_key = self . inbound_substreams_delay . insert (
344
- self . current_inbound_substream_id ,
345
- Duration :: from_secs ( RESPONSE_TIMEOUT ) ,
346
- ) ;
347
- let awaiting_stream = InboundState :: Idle ( substream) ;
348
- self . inbound_substreams . insert (
349
- self . current_inbound_substream_id ,
350
- InboundInfo {
351
- state : awaiting_stream,
352
- pending_items : VecDeque :: with_capacity ( expected_responses as usize ) ,
353
- delay_key : Some ( delay_key) ,
354
- protocol : req. protocol ( ) ,
355
- request_start_time : Instant :: now ( ) ,
356
- remaining_chunks : expected_responses,
357
- } ,
358
- ) ;
345
+ if self . inbound_substreams . len ( ) < MAX_INBOUND_SUBSTREAMS {
346
+ // Store the stream and tag the output.
347
+ let delay_key = self . inbound_substreams_delay . insert (
348
+ self . current_inbound_substream_id ,
349
+ Duration :: from_secs ( RESPONSE_TIMEOUT ) ,
350
+ ) ;
351
+ let awaiting_stream = InboundState :: Idle ( substream) ;
352
+ self . inbound_substreams . insert (
353
+ self . current_inbound_substream_id ,
354
+ InboundInfo {
355
+ state : awaiting_stream,
356
+ pending_items : VecDeque :: with_capacity ( expected_responses as usize ) ,
357
+ delay_key : Some ( delay_key) ,
358
+ protocol : req. protocol ( ) ,
359
+ request_start_time : Instant :: now ( ) ,
360
+ remaining_chunks : expected_responses,
361
+ } ,
362
+ ) ;
363
+ } else {
364
+ self . events_out . push ( Err ( HandlerErr :: Inbound {
365
+ id : self . current_inbound_substream_id ,
366
+ proto : req. protocol ( ) ,
367
+ error : RPCError :: HandlerRejected ,
368
+ } ) ) ;
369
+ return self . shutdown ( None ) ;
370
+ }
359
371
}
360
372
361
373
// If we received a goodbye, shutdown the connection.
@@ -382,7 +394,7 @@ where
382
394
// accept outbound connections only if the handler is not deactivated
383
395
if matches ! ( self . state, HandlerState :: Deactivated ) {
384
396
self . events_out . push ( Err ( HandlerErr :: Outbound {
385
- error : RPCError :: HandlerRejected ,
397
+ error : RPCError :: Disconnected ,
386
398
proto,
387
399
id,
388
400
} ) ) ;
@@ -671,7 +683,7 @@ where
671
683
{
672
684
// if the request was still active, report back to cancel it
673
685
self . events_out . push ( Err ( HandlerErr :: Inbound {
674
- error : RPCError :: HandlerRejected ,
686
+ error : RPCError :: Disconnected ,
675
687
proto : info. protocol ,
676
688
id : * id,
677
689
} ) ) ;
@@ -803,7 +815,7 @@ where
803
815
// the handler is deactivated. Close the stream
804
816
entry. get_mut ( ) . state = OutboundSubstreamState :: Closing ( substream) ;
805
817
self . events_out . push ( Err ( HandlerErr :: Outbound {
806
- error : RPCError :: HandlerRejected ,
818
+ error : RPCError :: Disconnected ,
807
819
proto : entry. get ( ) . proto ,
808
820
id : entry. get ( ) . req_id ,
809
821
} ) )
0 commit comments