Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/dispatcher/client-h2.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,14 @@ function connectH2 (client, socket) {
* @returns {boolean}
*/
busy (request) {
if (session[kRemoteSettings] === false && client[kRunning] > 0) {
return true
}

if (client[kRunning] >= client[kMaxConcurrentStreams]) {
return true
}

if (request != null) {
if (client[kRunning] > 0) {
// We are already processing requests
Expand Down
50 changes: 50 additions & 0 deletions test/h2c-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,56 @@ test('Should support h2c connection with body', async t => {
planner.equal(Buffer.concat(bodyChunks).toString(), 'Hello, world!')
})

test('Should queue h2c requests above the remote max concurrent streams setting', async t => {
const planner = tspl(t, { plan: 3 })
let activeStreams = 0
let maxActiveStreams = 0
const paths = []

const server = createServer({
settings: {
maxConcurrentStreams: 1
}
})

server.on('stream', (stream, headers) => {
activeStreams++
maxActiveStreams = Math.max(maxActiveStreams, activeStreams)
paths.push(headers[':path'])

stream.respond({ ':status': 200 })
setTimeout(() => {
activeStreams--
stream.end('Hello, world!')
}, 50)
})

server.listen()
await once(server, 'listening')
const client = new H2CClient(`http://localhost:${server.address().port}/`, {
maxConcurrentStreams: 10,
pipelining: 10
})

t.after(() => client.close())
t.after(() => server.close())

const responses = await Promise.all(Array.from({ length: 5 }, async (_, i) => {
const response = await client.request({ path: `/${i}`, method: 'GET' })
return {
statusCode: response.statusCode,
body: await response.body.text()
}
}))

planner.strictEqual(maxActiveStreams, 1)
planner.deepStrictEqual(paths, ['/0', '/1', '/2', '/3', '/4'])
planner.deepStrictEqual(responses, Array.from({ length: 5 }, () => ({
statusCode: 200,
body: 'Hello, world!'
})))
})

test('Should reject request if not h2c supported', async t => {
const planner = tspl(t, { plan: 1 })

Expand Down
Loading