Permalink
Browse files

manage request timeout without destroying socket (for reuse)

  • Loading branch information...
1 parent ed913b0 commit f8550a80324cc1f66b4792f18a7ea925735b98dd @jairajs89 committed Dec 6, 2012
Showing with 16 additions and 9 deletions.
  1. +1 −1 package.json
  2. +15 −8 zerver.js
View
@@ -2,7 +2,7 @@
"author": "Jairaj Sethi <j@jairaj.org> (http://jairaj.org/)",
"name": "zerver",
"description": "client-integrated webapp server",
- "version": "0.6.13",
+ "version": "0.6.14",
"repository": {
"type": "git",
"url": "git://github.com/jairajs89/zerver.git"
View
@@ -247,18 +247,25 @@ function handleRequest (request, response) {
pathname = handler.pathname,
isApiCall = pathname.substr(0, API_URL_LENGTH + 2) === '/'+API_URL+'/';
- setRequestTimeout(request, response);
+ setRequestTimeout(response);
tryResponseFromCache(handler, pathname, isApiCall, dynamicResponse);
}
-function setRequestTimeout (request, response) {
- request.socket.removeAllListeners('timeout');
- request.socket.setTimeout(REQUEST_TIMEOUT);
- request.socket.once('timeout', function () {
- console.log('zerver: request timeout, closing socket');
- request.socket.destroy();
- });
+function setRequestTimeout (response) {
+ var responseEnd = response.end,
+ timeout;
+
+ timeout = setTimeout(function () {
+ console.log('zerver: request timeout');
+ response.end('');
+ }, REQUEST_TIMEOUT);
+
+ response.end = function () {
+ clearTimeout(timeout);
+ response.end = responseEnd;
+ response.end.apply(this, arguments);
+ };
}
function tryResponseFromCache (handler, pathname, isApiCall, fallback) {

0 comments on commit f8550a8

Please sign in to comment.