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

http: cleanup ClientRequest oncreate #36862

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
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
39 changes: 19 additions & 20 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const {
const net = require('net');
const url = require('url');
const assert = require('internal/assert');
const { once } = require('internal/util');
const {
_checkIsHttpToken: checkIsHttpToken,
debug,
Expand Down Expand Up @@ -240,8 +241,6 @@ function ClientRequest(input, options, cb) {
this.host = host;
this.protocol = protocol;

let called = false;

if (this.agent) {
// If there is an agent we should default to Connection:keep-alive,
// but only if the Agent will actually reuse the connection!
Expand Down Expand Up @@ -305,18 +304,6 @@ function ClientRequest(input, options, cb) {
options.headers);
}

const oncreate = (err, socket) => {
if (called)
return;
called = true;
if (err) {
process.nextTick(() => this.emit('error', err));
return;
}
this.onSocket(socket);
this._deferToConnect(null, null, () => this._flush());
};

// initiate connection
if (this.agent) {
this.agent.addRequest(this, options);
Expand All @@ -325,12 +312,24 @@ function ClientRequest(input, options, cb) {
this._last = true;
this.shouldKeepAlive = false;
if (typeof options.createConnection === 'function') {
const newSocket = options.createConnection(options, oncreate);
if (newSocket && !called) {
called = true;
this.onSocket(newSocket);
} else {
return;
const oncreate = once((err, socket) => {
if (err) {
process.nextTick(() => this.emit('error', err));
} else {
this.onSocket(socket);
this._deferToConnect(null, null, () => this._flush());
}
});

try {
const newSocket = options.createConnection(options, oncreate);
if (newSocket) {
oncreate(null, newSocket);
ronag marked this conversation as resolved.
Show resolved Hide resolved
} else {
return;
}
} catch (err) {
oncreate(err);
}
} else {
debug('CLIENT use net.createConnection', options);
Expand Down