hewei.it 5 سال پیش
والد
کامیت
54cd91642c
3فایلهای تغییر یافته به همراه23 افزوده شده و 1 حذف شده
  1. 16 0
      examples/httpd/handler.h
  2. 3 0
      examples/httpd/router.h
  3. 4 1
      http/server/HttpServer.cpp

+ 16 - 0
examples/httpd/handler.h

@@ -37,6 +37,22 @@ public:
         return 0;
     }
 
+    static int sleep(HttpRequest* req, HttpResponse* res) {
+        time_t start_time = time(NULL);
+        std::string strTime = req->GetParam("t");
+        if (!strTime.empty()) {
+            int sec = atoi(strTime.c_str());
+            if (sec > 0) {
+                hv_delay(sec*1000);
+            }
+        }
+        time_t end_time = time(NULL);
+        res->Set("start_time", start_time);
+        res->Set("end_time", end_time);
+        response_status(res, 0, "OK");
+        return 200;
+    }
+
     static int query(HttpRequest* req, HttpResponse* res) {
         // scheme:[//[user[:password]@]host[:port]][/path][?query][#fragment]
         // ?query => HttpRequest::query_params

+ 3 - 0
examples/httpd/router.h

@@ -25,6 +25,9 @@ public:
             return 200;
         });
 
+        // curl -v http://ip:port/sleep?t=3
+        http.GET("/sleep", Handler::sleep);
+
         // curl -v http://ip:port/query?page_no=1\&page_size=10
         http.GET("/query", Handler::query);
 

+ 4 - 1
http/server/HttpServer.cpp

@@ -189,7 +189,10 @@ handle_request:
         }
     }
 
-    hlogi("[%s:%d][%s %s]=>[%d %s]",
+    static long s_pid = hv_getpid();
+    long tid = hv_gettid();
+    hlogi("[%ld-%ld][%s:%d][%s %s]=>[%d %s]",
+        s_pid, tid,
         handler->ip, handler->port,
         http_method_str(req->method), req->path.c_str(),
         res->status_code, http_status_str(res->status_code));