浏览代码

add toobusy 503 response

jomo 10 年之前
父节点
当前提交
e1f1fe7c57
共有 2 个文件被更改,包括 14 次插入3 次删除
  1. 11 1
      lib/server.js
  2. 3 2
      package.json

+ 11 - 1
lib/server.js

@@ -1,7 +1,8 @@
 #!/usr/bin/env node
-var logging = require("./logging");
 var querystring = require("querystring");
 var response = require("./response");
+var toobusy = require("toobusy-js");
+var logging = require("./logging");
 var config = require("./config");
 var http = require("http");
 var mime = require("mime");
@@ -65,6 +66,15 @@ function requestHandler(req, res) {
 
   var local_path = req.url.path_list[0];
   logging.log(req.id, req.method, req.url.href);
+
+  if (toobusy()) {
+    res.writeHead(503, {
+      "Content-Type": "text/plain"
+    });
+    res.end("Server is over capaacity :/");
+    return;
+  }
+
   if (req.method === "GET" || req.method === "HEAD") {
     try {
       switch (local_path) {

+ 3 - 2
package.json

@@ -31,6 +31,7 @@
   },
   "dependencies": {
     "canvas": "crafatar/node-canvas",
+    "crc": "3.2.1",
     "forever": "0.14.1",
     "jade": "~1.9.1",
     "lwip": "crafatar/lwip",
@@ -38,7 +39,7 @@
     "node-df": "0.1.1",
     "redis": "0.12.1",
     "request": "^2.51.0",
-    "crc": "3.2.1"
+    "toobusy-js": "^0.4.1"
   },
   "devDependencies": {
     "coveralls": "^2.11.2",
@@ -46,4 +47,4 @@
     "mocha": "2.1.0",
     "mocha-lcov-reporter": "0.0.1"
   }
-}
+}