Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #28 from rixth/server_timeouts

Server timeouts
  • Loading branch information...
commit 7c047b2ebdc9bbd5140d1f4cbe5f5dba45f37bc8 2 parents 31ca651 + 3b26bf4
@mjackson authored
Showing with 41 additions and 0 deletions.
  1. +1 −0  lib/index.js
  2. +40 −0 lib/servertimeout.js
View
1  lib/index.js
@@ -356,6 +356,7 @@ var modules = {
"rewrite": "rewrite",
"route": "route",
"sessionCookie": "session/cookie",
+ "timeout": "servertimeout",
// Other
"manual": "manual",
"mock": "mock",
View
40 lib/servertimeout.js
@@ -0,0 +1,40 @@
+module.exports = function (app, defaultTimeout, timeoutHandler) {
+ return function appWithTimeout(env, callback) {
+ env.serverTimeout = defaultTimeout || 150;
+
+ var timer,
+ timeoutValue = env.serverTimeout,
+ timedOut = false;
+
+ function createTimeout(timeout) {
+ timer = setTimeout(function () {
+ if (env.serverTimeout === false) {
+ // Timeout was disabled by the app
+ return;
+ } else if (env.serverTimeout > timeoutValue) {
+ // Timeout value was increased, so account for this and make
+ // a new timer
+ createTimeout(env.serverTimeout - timeoutValue);
+ timeoutValue = env.serverTimeout;
+ } else {
+ // Timeout value was reduced or the same, so error out
+ timedOut = true;
+ if (timeoutHandler) {
+ timeoutHandler(env, callback);
+ } else {
+ callback(500, {}, 'Internal server error.');
+ }
+ }
+ }, env.serverTimeout);
+ }
+
+ createTimeout(timeoutValue);
+
+ app(env, function () {
+ if (!timedOut) {
+ clearTimeout(timer);
+ callback.apply(this, arguments);
+ }
+ });
+ };
+};
Please sign in to comment.
Something went wrong with that request. Please try again.