@@ -173,16 +173,9 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
173
173
174
174
this . opts = mergeOptions . call ( { ignoreUndefined : true } , defaultOptions , init )
175
175
176
- if ( this . opts . maxIncomingConnections < this . opts . minConnections ) {
176
+ if ( ( this . opts . maxIncomingConnections + this . opts . maxOutgoingConnections ) < this . opts . minConnections ) {
177
177
throw errCode (
178
- new Error ( 'Connection Manager maxIncomingConnections must be greater than minConnections' ) ,
179
- codes . ERR_INVALID_PARAMETERS
180
- )
181
- }
182
-
183
- if ( this . opts . maxOutgoingConnections < this . opts . minConnections ) {
184
- throw errCode (
185
- new Error ( 'Connection Manager maxOutgoingConnections must be greater than minConnections' ) ,
178
+ new Error ( 'Connection Manager maxIncomingConnections + maxOutgoingConnections must be greater than minConnections' ) ,
186
179
codes . ERR_INVALID_PARAMETERS
187
180
)
188
181
}
@@ -442,10 +435,24 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
442
435
await this . components . peerStore . keyBook . set ( peerId , peerId . publicKey )
443
436
}
444
437
445
- const numConnections = this . getConnections ( ) . length
446
- const toPrune = numConnections - this . opts . maxOutgoingConnections
438
+ let [ outboundConnections , inboundConnections ] = [ 0 , 0 ]
439
+
440
+ const connections = this . getConnections ( )
441
+
442
+ connections . forEach ( connection => {
443
+ if ( connection . stat . direction === 'inbound' ) {
444
+ inboundConnections ++
445
+ } else {
446
+ outboundConnections ++
447
+ }
448
+ } )
449
+
450
+ const numIncomingToPrune = inboundConnections - this . opts . maxIncomingConnections
451
+ const numOutgoingToPrune = outboundConnections - this . opts . maxOutgoingConnections
452
+
453
+ await this . _checkMaxLimit ( 'maxOutgoingConnections' , outboundConnections , numOutgoingToPrune )
454
+ await this . _checkMaxLimit ( 'maxIncomingConnections' , inboundConnections , numIncomingToPrune )
447
455
448
- await this . _checkMaxLimit ( 'maxOutgoingConnections' , numConnections , toPrune )
449
456
this . dispatchEvent ( new CustomEvent < Connection > ( 'peer:connect' , { detail : connection } ) )
450
457
}
451
458
@@ -505,9 +512,12 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
505
512
}
506
513
}
507
514
508
- if ( ( this . getConnections ( ) . length + 1 ) > this . opts . maxOutgoingConnections ) {
515
+ const connections = this . getConnections ( )
516
+ const totalOutboundConnections = connections . filter ( connection => connection . stat . direction === 'outbound' ) . length
517
+
518
+ if ( ( totalOutboundConnections + 1 ) > this . opts . maxOutgoingConnections ) {
509
519
throw errCode (
510
- new Error ( 'Connection Manager maxOutgoing connections exceeded' ) ,
520
+ new Error ( 'Connection Manager max connections exceeded' ) ,
511
521
codes . ERR_CONNECTION_DENIED
512
522
)
513
523
}
@@ -548,6 +558,8 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
548
558
peerConnections . push ( connection )
549
559
}
550
560
561
+ connection . stat . direction = 'outbound'
562
+
551
563
return connection
552
564
} finally {
553
565
if ( timeoutController != null ) {
@@ -707,23 +719,27 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
707
719
return false
708
720
}
709
721
722
+ // check pending connections
723
+ if ( this . incomingPendingConnections >= Number ( this . opts . maxIncomingPendingConnections ) ) {
724
+ log ( 'connection from %s refused - incomingPendingConnections exceeded by peer %s' , maConn . remoteAddr )
725
+ return false
726
+ }
727
+
728
+ const connections = this . getConnections ( )
729
+
730
+ const totalIncomingConnections = connections . filter ( connection => connection . stat . direction === 'inbound' ) . length
731
+
710
732
// check allow list
711
733
const allowConnection = this . allow . some ( ma => {
712
734
return maConn . remoteAddr . toString ( ) . startsWith ( ma . toString ( ) )
713
735
} )
714
736
715
- if ( allowConnection ) {
716
- this . incomingPendingConnections ++
717
-
718
- return true
719
- }
720
-
721
- // check pending connections
722
- if ( this . incomingPendingConnections === this . opts . maxIncomingPendingConnections ) {
723
- log ( 'connection from %s refused - incomingPendingConnections exceeded by peer %s' , maConn . remoteAddr )
737
+ if ( ( totalIncomingConnections + 1 > this . opts . maxIncomingConnections ) && ! allowConnection ) {
738
+ log ( 'connection from %s refused - maxIncomingConnections exceeded' , maConn . remoteAddr )
724
739
return false
725
740
}
726
741
742
+ // Check the rate limiter
727
743
if ( maConn . remoteAddr . isThinWaistAddress ( ) ) {
728
744
const host = maConn . remoteAddr . nodeAddress ( ) . address
729
745
@@ -735,14 +751,9 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
735
751
}
736
752
}
737
753
738
- if ( this . getConnections ( ) . length < this . opts . maxIncomingConnections ) {
739
- this . incomingPendingConnections ++
740
-
741
- return true
742
- }
754
+ this . incomingPendingConnections ++
743
755
744
- log ( 'connection from %s refused - maxConnections exceeded' , maConn . remoteAddr )
745
- return false
756
+ return true
746
757
}
747
758
748
759
afterUpgradeInbound ( ) {
0 commit comments