Skip to content

Commit d4f1779

Browse files
authored
fix: limit stream concurrency (#77)
Pass `maxInboundStreams` and `maxOutboundStreams` options to registrar
1 parent a0ef679 commit d4f1779

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@
174174
"dependencies": {
175175
"@libp2p/components": "^2.0.0",
176176
"@libp2p/crypto": "^1.0.0",
177+
"@libp2p/interface-connection": "^2.0.0",
178+
"@libp2p/interface-peer-id": "^1.0.2",
179+
"@libp2p/interface-pubsub": "^1.0.3",
180+
"@libp2p/interface-registrar": "^2.0.0",
177181
"@libp2p/interfaces": "^3.0.2",
178182
"@libp2p/logger": "^2.0.0",
179183
"@libp2p/peer-collections": "^1.0.0",
@@ -191,10 +195,6 @@
191195
"uint8arrays": "^3.0.0"
192196
},
193197
"devDependencies": {
194-
"@libp2p/interface-connection": "^2.0.0",
195-
"@libp2p/interface-peer-id": "^1.0.2",
196-
"@libp2p/interface-pubsub": "^1.0.1",
197-
"@libp2p/interface-registrar": "^2.0.0",
198198
"@libp2p/peer-id-factory": "^1.0.0",
199199
"aegir": "^37.2.0",
200200
"delay": "^5.0.0",

src/index.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ export abstract class PubSubBaseProtocol<Events = PubSubEvents> extends EventEmi
6565

6666
private _registrarTopologyIds: string[] | undefined
6767
protected enabled: boolean
68+
private readonly maxInboundStreams: number
69+
private readonly maxOutboundStreams: number
6870

6971
constructor (props: PubSubInit) {
7072
super()
@@ -74,7 +76,9 @@ export abstract class PubSubBaseProtocol<Events = PubSubEvents> extends EventEmi
7476
globalSignaturePolicy = 'StrictSign',
7577
canRelayMessage = false,
7678
emitSelf = false,
77-
messageProcessingConcurrency = 10
79+
messageProcessingConcurrency = 10,
80+
maxInboundStreams = 1,
81+
maxOutboundStreams = 1
7882
} = props
7983

8084
this.multicodecs = ensureArray(multicodecs)
@@ -88,6 +92,8 @@ export abstract class PubSubBaseProtocol<Events = PubSubEvents> extends EventEmi
8892
this.emitSelf = emitSelf
8993
this.topicValidators = new Map()
9094
this.queue = new Queue({ concurrency: messageProcessingConcurrency })
95+
this.maxInboundStreams = maxInboundStreams
96+
this.maxOutboundStreams = maxOutboundStreams
9197

9298
this._onIncomingStream = this._onIncomingStream.bind(this)
9399
this._onPeerConnected = this._onPeerConnected.bind(this)
@@ -115,7 +121,10 @@ export abstract class PubSubBaseProtocol<Events = PubSubEvents> extends EventEmi
115121
const registrar = this.components.getRegistrar()
116122
// Incoming streams
117123
// Called after a peer dials us
118-
await Promise.all(this.multicodecs.map(async multicodec => await registrar.handle(multicodec, this._onIncomingStream)))
124+
await Promise.all(this.multicodecs.map(async multicodec => await registrar.handle(multicodec, this._onIncomingStream, {
125+
maxInboundStreams: this.maxInboundStreams,
126+
maxOutboundStreams: this.maxOutboundStreams
127+
})))
119128

120129
// register protocol with topology
121130
// Topology callbacks called on connection manager changes

0 commit comments

Comments
 (0)