Skip to content

Commit 85b333b

Browse files
ronagBridgeAR
authored andcommitted
http: refactor agent 'free' handler
Remove nesting in favor of early returns. PR-URL: #32801 Reviewed-By: Zeyu Yang <[email protected]> Reviewed-By: Denys Otrishko <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 5b01772 commit 85b333b

File tree

1 file changed

+49
-48
lines changed

1 file changed

+49
-48
lines changed

lib/_http_agent.js

+49-48
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ const {
3737
ERR_INVALID_ARG_TYPE,
3838
},
3939
} = require('internal/errors');
40+
const { once } = require('internal/util');
41+
4042
const kOnKeylog = Symbol('onkeylog');
4143
// New Agent code.
4244

@@ -94,52 +96,55 @@ function Agent(options) {
9496
// case of socket.destroy() below this 'error' has no handler
9597
// and could cause unhandled exception.
9698

97-
if (socket.writable &&
98-
this.requests[name] && this.requests[name].length) {
99-
const req = this.requests[name].shift();
99+
if (!socket.writable) {
100+
socket.destroy();
101+
return;
102+
}
103+
104+
const requests = this.requests[name];
105+
if (requests && requests.length) {
106+
const req = requests.shift();
100107
setRequestSocket(this, req, socket);
101-
if (this.requests[name].length === 0) {
102-
// don't leak
108+
if (requests.length === 0) {
103109
delete this.requests[name];
104110
}
105-
} else {
106-
// If there are no pending requests, then put it in
107-
// the freeSockets pool, but only if we're allowed to do so.
108-
const req = socket._httpMessage;
109-
if (req &&
110-
req.shouldKeepAlive &&
111-
socket.writable &&
112-
this.keepAlive) {
113-
let freeSockets = this.freeSockets[name];
114-
const freeLen = freeSockets ? freeSockets.length : 0;
115-
let count = freeLen;
116-
if (this.sockets[name])
117-
count += this.sockets[name].length;
118-
119-
if (count > this.maxSockets || freeLen >= this.maxFreeSockets) {
120-
socket.destroy();
121-
} else if (this.keepSocketAlive(socket)) {
122-
freeSockets = freeSockets || [];
123-
this.freeSockets[name] = freeSockets;
124-
socket[async_id_symbol] = -1;
125-
socket._httpMessage = null;
126-
this.removeSocket(socket, options);
127-
128-
const agentTimeout = this.options.timeout || 0;
129-
if (socket.timeout !== agentTimeout) {
130-
socket.setTimeout(agentTimeout);
131-
}
132-
133-
socket.once('error', freeSocketErrorListener);
134-
freeSockets.push(socket);
135-
} else {
136-
// Implementation doesn't want to keep socket alive
137-
socket.destroy();
138-
}
139-
} else {
140-
socket.destroy();
141-
}
111+
return;
112+
}
113+
114+
// If there are no pending requests, then put it in
115+
// the freeSockets pool, but only if we're allowed to do so.
116+
const req = socket._httpMessage;
117+
if (!req || !req.shouldKeepAlive || !this.keepAlive) {
118+
socket.destroy();
119+
return;
142120
}
121+
122+
let freeSockets = this.freeSockets[name];
123+
const freeLen = freeSockets ? freeSockets.length : 0;
124+
let count = freeLen;
125+
if (this.sockets[name])
126+
count += this.sockets[name].length;
127+
128+
if (count > this.maxSockets ||
129+
freeLen >= this.maxFreeSockets ||
130+
!this.keepSocketAlive(socket)) {
131+
socket.destroy();
132+
return;
133+
}
134+
135+
freeSockets = freeSockets || [];
136+
this.freeSockets[name] = freeSockets;
137+
socket[async_id_symbol] = -1;
138+
socket._httpMessage = null;
139+
this.removeSocket(socket, options);
140+
141+
const agentTimeout = this.options.timeout || 0;
142+
if (socket.timeout !== agentTimeout) {
143+
socket.setTimeout(agentTimeout);
144+
}
145+
146+
socket.once('error', freeSocketErrorListener);
147+
freeSockets.push(socket);
143148
});
144149

145150
// Don't emit keylog events unless there is a listener for them.
@@ -266,12 +271,8 @@ Agent.prototype.createSocket = function createSocket(req, options, cb) {
266271

267272
debug('createConnection', name, options);
268273
options.encoding = null;
269-
let called = false;
270274

271-
const oncreate = (err, s) => {
272-
if (called)
273-
return;
274-
called = true;
275+
const oncreate = once((err, s) => {
275276
if (err)
276277
return cb(err);
277278
if (!this.sockets[name]) {
@@ -281,7 +282,7 @@ Agent.prototype.createSocket = function createSocket(req, options, cb) {
281282
debug('sockets', name, this.sockets[name].length);
282283
installListeners(this, s, options);
283284
cb(null, s);
284-
};
285+
});
285286

286287
const newSocket = this.createConnection(options, oncreate);
287288
if (newSocket)

0 commit comments

Comments
 (0)