Skip to content

Commit 0b40568

Browse files
szmarczakcodebytere
authored andcommitted
http2: delay session.receive() by a tick
PR-URL: #35985 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 1924255 commit 0b40568

File tree

2 files changed

+30
-39
lines changed

2 files changed

+30
-39
lines changed

‎lib/internal/http2/core.js‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3139,16 +3139,21 @@ function connect(authority, options, listener){
31393139
if(typeoflistener==='function')
31403140
session.once('connect',listener);
31413141

3142-
debug('Http2Session connect',options.createConnection);
3143-
// Socket already has some buffered data - emulate receiving it
3144-
// https://github.com/nodejs/node/issues/35475
3145-
if(socket&&socket.readableLength){
3146-
letbuf;
3147-
while((buf=socket.read())!==null){
3148-
debug(`Http2Session connect: injecting ${buf.length} already in buffer`);
3149-
session[kHandle].receive(buf);
3142+
// Process data on the next tick - a remoteSettings handler may be attached.
3143+
// https://github.com/nodejs/node/issues/35981
3144+
process.nextTick(()=>{
3145+
debug('Http2Session connect',options.createConnection);
3146+
// Socket already has some buffered data - emulate receiving it
3147+
// https://github.com/nodejs/node/issues/35475
3148+
if(socket&&socket.readableLength){
3149+
letbuf;
3150+
while((buf=socket.read())!==null){
3151+
debug(`Http2Session connect: ${buf.length} bytes already in buffer`);
3152+
session[kHandle].receive(buf);
3153+
}
31503154
}
3151-
}
3155+
});
3156+
31523157
returnsession;
31533158
}
31543159

‎test/parallel/test-http2-connect-tls-with-delay.js‎

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,61 +4,47 @@ const common = require('../common');
44
if(!common.hasCrypto)
55
common.skip('missing crypto');
66

7-
if(!common.hasMultiLocalhost())
8-
common.skip('platform-specific test.');
9-
107
consthttp2=require('http2');
11-
constassert=require('assert');
128
consttls=require('tls');
139
constfixtures=require('../common/fixtures');
1410

1511
constserverOptions={
1612
key: fixtures.readKey('agent1-key.pem'),
1713
cert: fixtures.readKey('agent1-cert.pem')
1814
};
19-
constserver=http2.createSecureServer(serverOptions,(req,res)=>{
20-
console.log(`Connect from: ${req.connection.remoteAddress}`);
21-
assert.strictEqual(req.connection.remoteAddress,'127.0.0.2');
2215

23-
req.on('end',common.mustCall(()=>{
24-
res.writeHead(200,{'Content-Type': 'text/plain'});
25-
res.end(`You are from: ${req.connection.remoteAddress}`);
26-
}));
27-
req.resume();
16+
constserver=http2.createSecureServer(serverOptions,(req,res)=>{
17+
res.end();
2818
});
2919

3020
server.listen(0,'127.0.0.1',common.mustCall(()=>{
3121
constoptions={
3222
ALPNProtocols: ['h2'],
3323
host: '127.0.0.1',
3424
servername: 'localhost',
35-
localAddress: '127.0.0.2',
3625
port: server.address().port,
3726
rejectUnauthorized: false
3827
};
3928

40-
console.log('Server ready',server.address().port);
41-
4229
constsocket=tls.connect(options,async()=>{
43-
44-
console.log('TLS Connected!');
45-
46-
setTimeout(()=>{
47-
30+
socket.once('readable',()=>{
4831
constclient=http2.connect(
4932
'https://localhost:'+server.address().port,
5033
{ ...options,createConnection: ()=>socket}
5134
);
52-
constreq=client.request({
53-
':path': '/'
54-
});
55-
req.on('data',()=>req.resume());
56-
req.on('end',common.mustCall(function(){
57-
client.close();
58-
req.close();
59-
server.close();
35+
36+
client.once('remoteSettings',common.mustCall(()=>{
37+
constreq=client.request({
38+
':path': '/'
39+
});
40+
req.on('data',()=>req.resume());
41+
req.on('end',common.mustCall(()=>{
42+
client.close();
43+
req.close();
44+
server.close();
45+
}));
46+
req.end();
6047
}));
61-
req.end();
62-
},1000);
48+
});
6349
});
6450
}));

0 commit comments

Comments
(0)