Skip to content

Commit b095e35

Browse files
jasnellBethGriggs
authored andcommitted
http2: improve http2 code a bit
Multiple general improvements to http2 internals for readability and efficiency [This backport applied to v10.x cleanly but had several merge conflicts on v8.x.] PR-URL: #23984 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Ujjwal Sharma <usharma1998@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 07458ba commit b095e35

File tree

5 files changed

+152
-135
lines changed

5 files changed

+152
-135
lines changed

benchmark/http2/headers.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ function main(conf) {
4242

4343
function doRequest(remaining) {
4444
const req = client.request(headersObject);
45-
req.end();
46-
req.on('data', () => {});
45+
req.resume();
4746
req.on('end', () => {
4847
if (remaining > 0) {
4948
doRequest(remaining - 1);

benchmark/http2/respond-with-fd.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ const fs = require('fs');
88
const file = path.join(path.resolve(__dirname, '../fixtures'), 'alice.html');
99

1010
const bench = common.createBenchmark(main, {
11-
requests: [100, 1000, 10000, 100000, 1000000],
12-
streams: [100, 200, 1000],
13-
clients: [1, 2],
11+
requests: [100, 1000, 5000],
12+
streams: [1, 10, 20, 40, 100, 200],
13+
clients: [2],
1414
benchmarker: ['h2load']
1515
}, { flags: ['--no-warnings', '--expose-http2'] });
1616

benchmark/http2/simple.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ const fs = require('fs');
99
const file = path.join(path.resolve(__dirname, '../fixtures'), 'alice.html');
1010

1111
const bench = common.createBenchmark(main, {
12-
requests: [100, 1000, 10000, 100000],
13-
streams: [100, 200, 1000],
14-
clients: [1, 2],
12+
requests: [100, 1000, 5000],
13+
streams: [1, 10, 20, 40, 100, 200],
14+
clients: [2],
1515
benchmarker: ['h2load']
1616
}, { flags: ['--no-warnings', '--expose-http2'] });
1717

lib/internal/http2/util.js

+25-19
Original file line numberDiff line numberDiff line change
@@ -408,14 +408,20 @@ function mapToHeaders(map,
408408
let count = 0;
409409
const keys = Object.keys(map);
410410
const singles = new Set();
411-
for (var i = 0; i < keys.length; i++) {
412-
let key = keys[i];
413-
let value = map[key];
411+
let i;
412+
let isArray;
413+
let key;
414+
let value;
415+
let isSingleValueHeader;
416+
let err;
417+
for (i = 0; i < keys.length; i++) {
418+
key = keys[i];
419+
value = map[key];
414420
if (value === undefined || key === '')
415421
continue;
416422
key = key.toLowerCase();
417-
const isSingleValueHeader = kSingleValueHeaders.has(key);
418-
let isArray = Array.isArray(value);
423+
isSingleValueHeader = kSingleValueHeaders.has(key);
424+
isArray = Array.isArray(value);
419425
if (isArray) {
420426
switch (value.length) {
421427
case 0:
@@ -437,26 +443,26 @@ function mapToHeaders(map,
437443
singles.add(key);
438444
}
439445
if (key[0] === ':') {
440-
const err = assertValuePseudoHeader(key);
446+
err = assertValuePseudoHeader(key);
441447
if (err !== undefined)
442448
return err;
443449
ret = `${key}\0${value}\0${ret}`;
444450
count++;
445-
} else {
446-
if (isIllegalConnectionSpecificHeader(key, value)) {
447-
return new errors.Error('ERR_HTTP2_INVALID_CONNECTION_HEADERS', key);
448-
}
449-
if (isArray) {
450-
for (var k = 0; k < value.length; k++) {
451-
const val = String(value[k]);
452-
ret += `${key}\0${val}\0`;
453-
}
454-
count += value.length;
455-
} else {
456-
ret += `${key}\0${value}\0`;
457-
count++;
451+
continue;
452+
}
453+
if (isIllegalConnectionSpecificHeader(key, value)) {
454+
return new errors.Error('ERR_HTTP2_INVALID_CONNECTION_HEADERS', key);
455+
}
456+
if (isArray) {
457+
for (var k = 0; k < value.length; k++) {
458+
const val = String(value[k]);
459+
ret += `${key}\0${val}\0`;
458460
}
461+
count += value.length;
462+
continue;
459463
}
464+
ret += `${key}\0${value}\0`;
465+
count++;
460466
}
461467

462468
return [ret, count];

0 commit comments

Comments
 (0)