Jelajahi Sumber

http_async_handler use hv::async

ithewei 4 tahun lalu
induk
melakukan
3805f6a25c

+ 15 - 9
examples/httpd/handler.cpp

@@ -136,30 +136,36 @@ int Handler::largeFileHandler(const HttpContextPtr& ctx) {
     return HTTP_STATUS_UNFINISHED;
 }
 
-int Handler::sleep(const HttpContextPtr& ctx) {
-    ctx->set("start_ms", gettimeofday_ms());
-    std::string strTime = ctx->param("t", "1000");
+int Handler::sleep(const HttpRequestPtr& req, const HttpResponseWriterPtr& writer) {
+    writer->WriteHeader("X-Response-tid", hv_gettid());
+    unsigned long long start_ms = gettimeofday_ms();
+    writer->response->Set("start_ms", start_ms);
+    std::string strTime = req->GetParam("t", "1000");
     if (!strTime.empty()) {
         int ms = atoi(strTime.c_str());
         if (ms > 0) {
             hv_delay(ms);
         }
     }
-    ctx->set("end_ms", gettimeofday_ms());
-    response_status(ctx, 0, "OK");
+    unsigned long long end_ms = gettimeofday_ms();
+    writer->response->Set("end_ms", end_ms);
+    writer->response->Set("cost_ms", end_ms - start_ms);
+    response_status(writer, 0, "OK");
     return 200;
 }
 
 int Handler::setTimeout(const HttpContextPtr& ctx) {
-    ctx->set("start_ms", gettimeofday_ms());
+    unsigned long long start_ms = gettimeofday_ms();
+    ctx->set("start_ms", start_ms);
     std::string strTime = ctx->param("t", "1000");
     if (!strTime.empty()) {
         int ms = atoi(strTime.c_str());
         if (ms > 0) {
-            hv::setTimeout(ms, [ctx](hv::TimerID timerID){
-                ctx->set("end_ms", gettimeofday_ms());
+            hv::setTimeout(ms, [ctx, start_ms](hv::TimerID timerID){
+                unsigned long long end_ms = gettimeofday_ms();
+                ctx->set("end_ms", end_ms);
+                ctx->set("cost_ms", end_ms - start_ms);
                 response_status(ctx, 0, "OK");
-                ctx->send();
             });
         }
     }

+ 8 - 4
examples/httpd/handler.h

@@ -12,7 +12,7 @@ public:
     static int errorHandler(const HttpContextPtr& ctx);
     static int largeFileHandler(const HttpContextPtr& ctx);
 
-    static int sleep(const HttpContextPtr& ctx);
+    static int sleep(const HttpRequestPtr& req, const HttpResponseWriterPtr& writer);
     static int setTimeout(const HttpContextPtr& ctx);
     static int query(const HttpContextPtr& ctx);
 
@@ -34,10 +34,14 @@ private:
         resp->Set("message", message);
         return code;
     }
+    static int response_status(const HttpResponseWriterPtr& writer, int code = 200, const char* message = NULL) {
+        response_status(writer->response.get(), code, message);
+        writer->End();
+        return code;
+    }
     static int response_status(const HttpContextPtr& ctx, int code = 200, const char* message = NULL) {
-        if (message == NULL) message = http_status_str((enum http_status)code);
-        ctx->set("code", code);
-        ctx->set("message", message);
+        response_status(ctx->response.get(), code, message);
+        ctx->send();
         return code;
     }
 };

+ 5 - 7
examples/httpd/router.cpp

@@ -67,13 +67,11 @@ void Router::Register(hv::HttpService& router) {
 
     // curl -v http://ip:port/async
     router.GET("/async", [](const HttpRequestPtr& req, const HttpResponseWriterPtr& writer) {
-        writer->WriteHeader("X-Request-tid", hv_gettid());
-        hv::async([req, writer](){
-            writer->WriteHeader("X-Response-tid", hv_gettid());
-            writer->WriteHeader("Content-Type", "text/plain");
-            writer->WriteBody("This is an async response.\n");
-            writer->End();
-        });
+        writer->Begin();
+        writer->WriteHeader("X-Response-tid", hv_gettid());
+        writer->WriteHeader("Content-Type", "text/plain");
+        writer->WriteBody("This is an async response.\n");
+        writer->End();
     });
 
     // curl -v http://ip:port/www.*

+ 2 - 0
getting_started.sh

@@ -47,6 +47,8 @@ cmd="bin/curl -v localhost:8080/get?env=1" && run_cmd
 
 cmd="bin/curl -v localhost:8080/service" && run_cmd
 
+cmd="bin/curl -v localhost:8080/async" && run_cmd
+
 cmd="bin/curl -v localhost:8080/wildcard/test" && run_cmd
 
 cmd="bin/curl -v localhost:8080/echo -d 'hello,world!'" && echo_cmd

+ 2 - 1
http/server/HttpHandler.cpp

@@ -3,6 +3,7 @@
 #include "hbase.h"
 #include "herr.h"
 #include "hlog.h"
+#include "hasync.h" // import hv::async for http_async_handler
 #include "http_page.h"
 
 int HttpHandler::customHttpHandler(const http_handler& handler) {
@@ -14,7 +15,7 @@ int HttpHandler::invokeHttpHandler(const http_handler* handler) {
     if (handler->sync_handler) {
         status_code = handler->sync_handler(req.get(), resp.get());
     } else if (handler->async_handler) {
-        handler->async_handler(req, writer);
+        hv::async(std::bind(handler->async_handler, req, writer));
         status_code = HTTP_STATUS_UNFINISHED;
     } else if (handler->ctx_handler) {
         HttpContextPtr ctx(new hv::HttpContext);