Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http2: invoke connect callback immediately if session was connected #19842

Closed
wants to merge 3 commits into from
Closed
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
11 changes: 11 additions & 0 deletions doc/api/http2.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,17 @@ added: v9.4.0
Will be `true` if this `Http2Session` instance has been closed, otherwise
`false`.

#### http2session.connecting
<!-- YAML
added: REPLACEME
-->

* {boolean}

Will be `true` if this `Http2Session` instance is still connecting, will be set
to `false` before emitting `connect` event and/or calling the `http2.connect`
callback.

#### http2session.destroy([error,][code])
<!-- YAML
added: v8.4.0
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ function setupHandle(socket, type, options) {
// core will check for session.destroyed before progressing, this
// ensures that those at l`east get cleared out.
if (this.destroyed) {
this.emit('connect', this, socket);
process.nextTick(emit, this, 'connect', this, socket);
return;
}
debug(`Http2Session ${sessionName(type)}: setting up session handle`);
Expand Down Expand Up @@ -816,7 +816,7 @@ function setupHandle(socket, type, options) {
options.settings : {};

this.settings(settings);
this.emit('connect', this, socket);
process.nextTick(emit, this, 'connect', this, socket);
}

// Emits a close event followed by an error event if err is truthy. Used
Expand Down
25 changes: 25 additions & 0 deletions test/parallel/test-http2-connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const { mustCall, hasCrypto, skip, expectsError } = require('../common');
if (!hasCrypto)
skip('missing crypto');
const { createServer, connect } = require('http2');
const { connect: netConnect } = require('net');

// check for session connect callback and event
{
const server = createServer();
server.listen(0, mustCall(() => {
Expand All @@ -30,6 +33,28 @@ const { createServer, connect } = require('http2');
}));
}

// check for session connect callback on already connected socket
{
const server = createServer();
server.listen(0, mustCall(() => {
const { port } = server.address();

const onSocketConnect = () => {
const authority = `http://localhost:${port}`;
const createConnection = mustCall(() => socket);
const options = { createConnection };
connect(authority, options, mustCall(onSessionConnect));
};

const onSessionConnect = (session) => {
session.close();
server.close();
};

const socket = netConnect(port, mustCall(onSocketConnect));
}));
}

// check for https as protocol
{
const authority = 'https://localhost';
Expand Down