This repository has been archived by the owner on Jul 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add per-connection stream limit (#173)
Limits the total number of streams that can be opened to 1024 per connection. This can be overridden by passing an option to the constructor.
- Loading branch information
1 parent
00b8cf1
commit 21371e7
Showing
4 changed files
with
104 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* eslint-env mocha */ | ||
/* eslint max-nested-callbacks: ["error", 5] */ | ||
|
||
import { expect } from 'aegir/chai' | ||
import { Mplex } from '../src/index.js' | ||
import { Components } from '@libp2p/interfaces/components' | ||
import type { NewStreamMessage } from '../src/message-types.js' | ||
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' | ||
import { concat as uint8ArrayConcat } from 'uint8arrays/concat' | ||
import { encode } from '../src/encode.js' | ||
import all from 'it-all' | ||
|
||
describe('mplex', () => { | ||
it('should restrict number of initiator streams per connection', async () => { | ||
const maxStreamsPerConnection = 10 | ||
const factory = new Mplex({ | ||
maxStreamsPerConnection | ||
}) | ||
const components = new Components() | ||
const muxer = factory.createStreamMuxer(components) | ||
|
||
// max out the streams for this connection | ||
for (let i = 0; i < maxStreamsPerConnection; i++) { | ||
muxer.newStream() | ||
} | ||
|
||
// open one more | ||
expect(() => muxer.newStream()).to.throw().with.property('code', 'ERR_TOO_MANY_STREAMS') | ||
}) | ||
|
||
it('should restrict number of recipient streams per connection', async () => { | ||
const maxStreamsPerConnection = 10 | ||
const factory = new Mplex({ | ||
maxStreamsPerConnection | ||
}) | ||
const components = new Components() | ||
const muxer = factory.createStreamMuxer(components) | ||
|
||
// max out the streams for this connection | ||
for (let i = 0; i < maxStreamsPerConnection; i++) { | ||
muxer.newStream() | ||
} | ||
|
||
// simulate a new incoming stream | ||
const source: NewStreamMessage[] = [{ | ||
id: 17, | ||
type: 0, | ||
data: uint8ArrayFromString('17') | ||
}] | ||
|
||
const data = uint8ArrayConcat(await all(encode(source))) | ||
|
||
await muxer.sink([data]) | ||
|
||
await expect(all(muxer.source)).to.eventually.be.rejected.with.property('code', 'ERR_TOO_MANY_STREAMS') | ||
}) | ||
}) |