Skip to content
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
Next Next commit
http: Change free sockets behavior to LIFO from FIFO.
Sockets are added to the free list with .push() but they were
being removed with .shift().  This meant the sockets where being
removed in FIFO order, but this changes it to LIFO.  Since older
sockets may be closed based due to inactivity on the server it is
more likely that a socket that is recently used will be able to
successfully process the next request.

Rather than destroying the last used socket destroy
the oldest socket in the free list in push() on the
last recently used socket.
  • Loading branch information
rustyconover committed Feb 17, 2020
commit 5af8b39c968fc03d4aa0134ef6f2ce9ea621f463
17 changes: 11 additions & 6 deletions lib/_http_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,16 @@ function Agent(options) {
if (this.sockets[name])
count += this.sockets[name].length;

if (count > this.maxSockets || freeLen >= this.maxFreeSockets) {
socket.destroy();
} else if (this.keepSocketAlive(socket)) {
freeSockets = freeSockets || [];
this.freeSockets[name] = freeSockets;
if (this.keepSocketAlive(socket) &&
this.maxFreeSockets > 0 &&
count <= this.maxSockets) {
if (freeLen >= this.maxFreeSockets) {
const oldest = this.freeSockets[name].shift();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, freeSockets.shift()

oldest.destroy();
} else {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, else if (!freeSockets)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not quite following, could you explain a bit more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the block inside of the else is for the case when !freeSockets, could be simplified

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a nit, no biggie

freeSockets = freeSockets || [];
this.freeSockets[name] = freeSockets;
}
socket[async_id_symbol] = -1;
socket._httpMessage = null;
this.removeSocket(socket, options);
Expand Down Expand Up @@ -207,7 +212,7 @@ Agent.prototype.addRequest = function addRequest(req, options, port/* legacy */,

if (freeLen) {
// We have a free socket, so use that.
const socket = this.freeSockets[name].shift();
const socket = this.freeSockets[name].pop();
// Guard against an uninitialized or user supplied Socket.
const handle = socket._handle;
if (handle && typeof handle.asyncReset === 'function') {
Expand Down