-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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: overridable keep-alive behavior of Agent
#13005
Closed
indutny
wants to merge
1
commit into
nodejs:master
from
indutny:feature/http-agent-keepalive-override
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,15 +90,16 @@ function Agent(options) { | |
|
||
if (count > self.maxSockets || freeLen >= self.maxFreeSockets) { | ||
socket.destroy(); | ||
} else { | ||
} else if (self.keepSocketAlive(socket)) { | ||
freeSockets = freeSockets || []; | ||
self.freeSockets[name] = freeSockets; | ||
socket.setKeepAlive(true, self.keepAliveMsecs); | ||
socket.unref(); | ||
socket[async_id_symbol] = -1; | ||
socket._httpMessage = null; | ||
self.removeSocket(socket, options); | ||
freeSockets.push(socket); | ||
} else { | ||
// Implementation doesn't want to keep socket alive | ||
socket.destroy(); | ||
} | ||
} else { | ||
socket.destroy(); | ||
|
@@ -169,13 +170,12 @@ Agent.prototype.addRequest = function addRequest(req, options, port/*legacy*/, | |
// Assign the handle a new asyncId and run any init() hooks. | ||
socket._handle.asyncReset(); | ||
socket[async_id_symbol] = socket._handle.getAsyncId(); | ||
debug('have free socket'); | ||
|
||
// don't leak | ||
if (!this.freeSockets[name].length) | ||
delete this.freeSockets[name]; | ||
|
||
socket.ref(); | ||
this.reuseSocket(socket, req); | ||
req.onSocket(socket); | ||
this.sockets[name].push(socket); | ||
} else if (sockLen < this.maxSockets) { | ||
|
@@ -306,6 +306,18 @@ Agent.prototype.removeSocket = function removeSocket(s, options) { | |
} | ||
}; | ||
|
||
Agent.prototype.keepSocketAlive = function keepSocketAlive(socket) { | ||
socket.setKeepAlive(true, this.keepAliveMsecs); | ||
socket.unref(); | ||
|
||
return true; | ||
}; | ||
|
||
Agent.prototype.reuseSocket = function reuseSocket(socket, req) { | ||
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. Why is 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. Yes, this is correct. |
||
debug('have free socket'); | ||
socket.ref(); | ||
}; | ||
|
||
Agent.prototype.destroy = function destroy() { | ||
var sets = [this.freeSockets, this.sockets]; | ||
for (var s = 0; s < sets.length; s++) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
|
||
const http = require('http'); | ||
|
||
const server = http.createServer((req, res) => { | ||
res.end('ok'); | ||
}).listen(0, common.mustCall(() => { | ||
const agent = http.Agent({ | ||
keepAlive: true, | ||
maxSockets: 5, | ||
maxFreeSockets: 2 | ||
}); | ||
|
||
const keepSocketAlive = agent.keepSocketAlive; | ||
const reuseSocket = agent.reuseSocket; | ||
|
||
let called = 0; | ||
let expectedSocket; | ||
agent.keepSocketAlive = common.mustCall((socket) => { | ||
assert(socket); | ||
|
||
called++; | ||
if (called === 1) { | ||
return false; | ||
} else if (called === 2) { | ||
expectedSocket = socket; | ||
return keepSocketAlive.call(agent, socket); | ||
} | ||
|
||
assert.strictEqual(socket, expectedSocket); | ||
return false; | ||
}, 3); | ||
|
||
agent.reuseSocket = common.mustCall((socket, req) => { | ||
assert.strictEqual(socket, expectedSocket); | ||
assert(req); | ||
|
||
return reuseSocket.call(agent, socket, req); | ||
}, 1); | ||
|
||
function req(callback) { | ||
http.request({ | ||
method: 'GET', | ||
path: '/', | ||
agent, | ||
port: server.address().port | ||
}, common.mustCall((res) => { | ||
res.resume(); | ||
res.once('end', common.mustCall(() => { | ||
setImmediate(callback); | ||
})); | ||
})).end(); | ||
} | ||
|
||
// Should destroy socket instead of keeping it alive | ||
req(common.mustCall(() => { | ||
// Should keep socket alive | ||
req(common.mustCall(() => { | ||
// Should reuse the socket | ||
req(common.mustCall(() => { | ||
server.close(); | ||
})); | ||
})); | ||
})); | ||
})); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
this should list the arguments... e.g.
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.
Ack.