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

Add timeout and timeouts config options #12

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
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
23 changes: 21 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ function ForeverAgent(options) {
self.freeSockets = {}
self.maxSockets = self.options.maxSockets || Agent.defaultMaxSockets
self.minSockets = self.options.minSockets || ForeverAgent.defaultMinSockets
self.timeout = self.options.timeout || 0
self.timeouts = {}
for (var i in self.options.timeouts) {
self.timeouts[i] = self.options.timeouts[i]
}
self.on('free', function(socket, host, port) {
var name = host + ':' + port
if (self.requests[name] && self.requests[name].length) {
self.requests[name].shift().onSocket(socket)
} else if (self.sockets[name].length < self.minSockets) {
} else if (self.sockets[name].length <= self.minSockets) {
if (!self.freeSockets[name]) self.freeSockets[name] = []
self.freeSockets[name].push(socket)

Expand All @@ -44,7 +49,21 @@ util.inherits(ForeverAgent, Agent)
ForeverAgent.defaultMinSockets = 5


ForeverAgent.prototype.createConnection = net.createConnection

ForeverAgent.prototype.createConnection = function() {
var self = this
var socket = net.createConnection.apply(this, arguments)

socket.on('connect', function() {
var name = socket.remoteAddress + ':' + socket.remotePort

socket.setTimeout(self.timeouts[name] || self.timeout, function() {

Choose a reason for hiding this comment

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

This will close the socket after it's been open for a certain amount of time. Shouldn't it close the socket after it's been idle for a certain amount of time?

socket.end();
})
})
return socket;
};

ForeverAgent.prototype.addRequestNoreuse = Agent.prototype.addRequest
ForeverAgent.prototype.addRequest = function(req, host, port) {
var name = host + ':' + port
Expand Down