|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +if (!common.hasCrypto) |
| 5 | + common.skip('missing crypto'); |
| 6 | + |
| 7 | +const assert = require('assert'); |
| 8 | +const http2 = require('http2'); |
| 9 | +const { once } = require('events'); |
| 10 | + |
| 11 | +const { NGHTTP2_ENHANCE_YOUR_CALM } = http2.constants; |
| 12 | + |
| 13 | +// maxSessionMemory is documented to reject new streams while the limit is |
| 14 | +// exceeded. Make sure that also holds for requests initiated by a client |
| 15 | +// session, so that a peer that does not read cannot make the client queue |
| 16 | +// frames without bound. As for response headers, request bodies that are |
| 17 | +// queued waiting for flow control are not counted, so a large upload does not |
| 18 | +// prevent unrelated requests on the same session. |
| 19 | + |
| 20 | +async function queuedUploadDoesNotBlockRequests() { |
| 21 | + // The server never opens its flow-control window, so the upload stays queued |
| 22 | + // in the client session. |
| 23 | + const server = http2.createServer({ settings: { initialWindowSize: 0 } }); |
| 24 | + server.on('stream', common.mustCall((stream) => { |
| 25 | + stream.on('error', () => {}); |
| 26 | + }, 2)); |
| 27 | + server.listen(0); |
| 28 | + await once(server, 'listening'); |
| 29 | + |
| 30 | + const client = http2.connect(`http://localhost:${server.address().port}`, { |
| 31 | + maxSessionMemory: 1, |
| 32 | + }); |
| 33 | + client.on('error', common.mustNotCall()); |
| 34 | + await once(client, 'remoteSettings'); |
| 35 | + |
| 36 | + const uploadArrived = once(server, 'stream'); |
| 37 | + const upload = client.request({ ':method': 'POST', ':path': '/upload' }); |
| 38 | + upload.on('error', () => {}); |
| 39 | + upload.end(Buffer.alloc(4 * 1024 * 1024)); |
| 40 | + await uploadArrived; |
| 41 | + |
| 42 | + const other = client.request({ ':path': '/other' }); |
| 43 | + other.on('error', () => {}); |
| 44 | + await once(server, 'stream'); |
| 45 | + |
| 46 | + other.destroy(); |
| 47 | + upload.destroy(); |
| 48 | + client.destroy(); |
| 49 | + server.close(); |
| 50 | +} |
| 51 | + |
| 52 | +async function manyRequestsAreBounded() { |
| 53 | + // A peer that accepts the connection and then stops reading. Before the |
| 54 | + // session memory check, every request was queued regardless of the limit. |
| 55 | + const server = http2.createServer(); |
| 56 | + server.on('connection', (socket) => setTimeout(() => socket.pause(), 30)); |
| 57 | + server.on('stream', (stream) => stream.on('error', () => {})); |
| 58 | + server.listen(0); |
| 59 | + await once(server, 'listening'); |
| 60 | + |
| 61 | + const client = http2.connect(`http://localhost:${server.address().port}`, { |
| 62 | + maxSessionMemory: 1, |
| 63 | + }); |
| 64 | + client.on('error', () => {}); |
| 65 | + await once(client, 'connect'); |
| 66 | + |
| 67 | + const total = 30_000; |
| 68 | + let rejected = 0; |
| 69 | + for (let i = 0; i < total; i++) { |
| 70 | + const req = client.request({ ':path': `/${i}` }); |
| 71 | + req.on('error', (err) => { |
| 72 | + if (err.code === 'ERR_HTTP2_STREAM_ERROR' && |
| 73 | + err.message.includes('NGHTTP2_ENHANCE_YOUR_CALM')) { |
| 74 | + rejected++; |
| 75 | + } |
| 76 | + }); |
| 77 | + req.end(); |
| 78 | + } |
| 79 | + await new Promise(setImmediate); |
| 80 | + |
| 81 | + assert.ok(rejected > 0, 'expected some requests to be rejected'); |
| 82 | + // Without the check all of the requests (plus the connection preface) were |
| 83 | + // queued: the number of queued frames grew with the number submitted. |
| 84 | + assert.ok(client.state.outboundQueueSize < total, |
| 85 | + `queued ${client.state.outboundQueueSize} of ${total}`); |
| 86 | + assert.strictEqual(NGHTTP2_ENHANCE_YOUR_CALM, 11); |
| 87 | + |
| 88 | + client.destroy(); |
| 89 | + server.close(); |
| 90 | +} |
| 91 | + |
| 92 | +(async () => { |
| 93 | + await queuedUploadDoesNotBlockRequests(); |
| 94 | + await manyRequestsAreBounded(); |
| 95 | +})().then(common.mustCall()); |
0 commit comments