-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
http: Change free sockets behavior to LIFO from FIFO. #31526
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
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
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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(); | ||
| oldest.destroy(); | ||
| } else { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit, else if (!freeSockets)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not quite following, could you explain a bit more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the block inside of the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
|
@@ -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') { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit, freeSockets.shift()