diff options
author | isaacs <i@izs.me> | 2013-03-03 23:29:22 -0800 |
---|---|---|
committer | isaacs <i@izs.me> | 2013-03-06 12:43:48 -0800 |
commit | d258fb0212530329b9941be18bc6e90d7afec5b5 (patch) | |
tree | 8b5f89e7c4cd1791df6832f22914dbaf1eb30e9b /lib | |
parent | db5d58e84252042236b8de80fe0e1b722a83e61a (diff) | |
download | android-node-v8-d258fb0212530329b9941be18bc6e90d7afec5b5.tar.gz android-node-v8-d258fb0212530329b9941be18bc6e90d7afec5b5.tar.bz2 android-node-v8-d258fb0212530329b9941be18bc6e90d7afec5b5.zip |
http: More useful setTimeout API on server
This adds the following to HTTP:
* server.setTimeout(msecs, callback)
Sets all new connections to time out after the specified time, at
which point it emits 'timeout' on the server, passing the socket as an
argument.
In this way, timeouts can be handled in one place consistently.
* req.setTimeout(), res.setTimeout()
Essentially an alias to req/res.socket.setTimeout(), but without
having to delve into a "buried" object. Adds a listener on the
req/res object, but not on the socket.
* server.timeout
Number of milliseconds before incoming connections time out.
(Default=1000*60*2, as before.)
Furthermore, if the user sets up their own timeout listener on either
the server, the request, or the response, then the default behavior
(destroying the socket) is suppressed.
Fix #3460
Diffstat (limited to 'lib')
-rw-r--r-- | lib/http.js | 40 |
1 files changed, 37 insertions, 3 deletions
diff --git a/lib/http.js b/lib/http.js index 50571bf313..aafbe5f60f 100644 --- a/lib/http.js +++ b/lib/http.js @@ -324,6 +324,13 @@ util.inherits(IncomingMessage, Stream.Readable); exports.IncomingMessage = IncomingMessage; +IncomingMessage.prototype.setTimeout = function(msecs, callback) { + if (callback) + this.on('timeout', callback); + this.socket.setTimeout(msecs); +}; + + IncomingMessage.prototype.read = function(n) { this._consuming = true; this.read = Stream.Readable.prototype.read; @@ -445,6 +452,13 @@ util.inherits(OutgoingMessage, Stream); exports.OutgoingMessage = OutgoingMessage; +OutgoingMessage.prototype.setTimeout = function(msecs, callback) { + if (callback) + this.on('timeout', callback); + this.socket.setTimeout(msecs); +}; + + OutgoingMessage.prototype.destroy = function(error) { this.socket.destroy(error); }; @@ -1795,10 +1809,19 @@ function Server(requestListener) { this.addListener('clientError', function(err, conn) { conn.destroy(err); }); + + this.timeout = 2 * 60 * 1000; } util.inherits(Server, net.Server); +Server.prototype.setTimeout = function(msecs, callback) { + this.timeout = msecs; + if (callback) + this.on('timeout', callback); +}; + + exports.Server = Server; @@ -1834,9 +1857,20 @@ function connectionListener(socket) { httpSocketSetup(socket); - socket.setTimeout(2 * 60 * 1000); // 2 minute timeout - socket.once('timeout', function() { - socket.destroy(); + // If the user has added a listener to the server, + // request, or response, then it's their responsibility. + // otherwise, destroy on timeout by default + if (self.timeout) + socket.setTimeout(self.timeout); + socket.on('timeout', function() { + var req = socket.parser && socket.parser.incoming; + var reqTimeout = req && req.emit('timeout', socket); + var res = socket._httpMessage; + var resTimeout = res && res.emit('timeout', socket); + var serverTimeout = self.emit('timeout', socket); + + if (!reqTimeout && !resTimeout && !serverTimeout) + socket.destroy(); }); var parser = parsers.alloc(); |