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

socket.setTimeout for foreverAgent #11

Open
CrabDude opened this issue Aug 21, 2013 · 2 comments
Open

socket.setTimeout for foreverAgent #11

CrabDude opened this issue Aug 21, 2013 · 2 comments

Comments

@CrabDude
Copy link

What would be the best way to call socket.setTimeout?

This would be from the request level, i.e.,

var request = require('request')
request.agent.timeout = 30 * 1000
@CrabDude
Copy link
Author

Seems like this should work, though it's a bit hacky:

var ForeverAgent = require('request/node_modules/forever-agent');

// Set inactive socket timeout by shimming createConnection
var createConnection = ForeverAgent.prototype.createConnection;
ForeverAgent.prototype.createConnection = function() {
  var socket = createConnection.apply(this, arguments);
  socket.setTimeout(Config.foreverAgent.timeout);
  return socket;
};

Would be nice if this were exposed via agentOptions.timeout. Open to a pull request?

@CrabDude
Copy link
Author

For posterity, looks like socket.setTimeout is a no-op in that you have to close the socket yourself. Here's the working code I ended up with:

var ForeverAgent = require('request/node_modules/forever-agent');

// Set inactive socket timeout by shimming createConnection
var createConnection = ForeverAgent.prototype.createConnection;
ForeverAgent.prototype.createConnection = function() {
  var socket = createConnection.apply(this, arguments);
  socket.setTimeout(Config.foreverAgent.timeout || 0, socket.end.bind(socket));
  return socket;
};

It looks like we're going to need per-service (host:port) timeouts though, so I'll need to fork forever-agent (and submit a pull request), will update here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant