Selaa lähdekoodia

Add requests::async retry_delay for AsyncHttpClient

hewei.it 4 vuotta sitten
vanhempi
commit
db5f753a84

+ 14 - 2
docs/API.md

@@ -9,13 +9,12 @@
 - BYTE_ORDER: BIG_ENDIAN, LITTLE_ENDIAN
 - stdbool.h: bool, true, false
 - stdint.h: int8_t, int16_t, int32_t, int64_t
-- var
 - hv_sleep, hv_msleep, hv_usleep, hv_delay
 - hv_mkdir
 - stricmp, strcasecmp
 
 ### hexport.h
-- HV_EXPORT
+- HV_EXPORT, HV_INLINE
 - HV_SOURCE, HV_STATICLIB, HV_DYNAMICLIB
 - HV_DEPRECATED
 - HV_UNUSED
@@ -325,6 +324,8 @@
 - hloop_create_tcp_server
 - hloop_create_udp_client
 - hloop_create_udp_server
+- hloop_create_ssl_client
+- hloop_create_ssl_server
 - hloop_new
 - hloop_free
 - hloop_run
@@ -477,6 +478,17 @@
 - http_client_get_header
 - http_client_clear_headers
 
+### requests.h
+- requests::request
+- requests::get
+- requests::post
+- requests::put
+- requests::patch
+- requests::Delete
+- requests::head
+- requests::options
+- requests::async
+
 ### HttpServer.h
 - http_server_run
 - http_server_stop

+ 2 - 2
examples/httpd/router.h

@@ -4,7 +4,7 @@
 #include <future> // import std::async
 
 #include "HttpService.h"
-#include "http_client.h"
+#include "requests.h"
 
 #include "handler.h"
 
@@ -76,7 +76,7 @@ public:
         router.GET("/www.*", [](const HttpRequestPtr& req, const HttpResponseWriterPtr& writer) {
             HttpRequestPtr req2(new HttpRequest);
             req2->url = req->path.substr(1);
-            http_client_send_async(req2, [writer](const HttpResponsePtr& resp2){
+            requests::async(req2, [writer](const HttpResponsePtr& resp2){
                 writer->Begin();
                 if (resp2 == NULL) {
                     writer->WriteStatus(HTTP_STATUS_NOT_FOUND);

+ 10 - 3
http/client/AsyncHttpClient.cpp

@@ -90,9 +90,16 @@ int AsyncHttpClient::doTask(const HttpClientTaskPtr& task) {
         if (iter != conn_pools.end()) {
             iter->second.remove(channel->fd());
         }
-        if (ctx->task && ctx->task->retry_cnt-- > 0) {
-            // try again
-            send(ctx->task);
+        const HttpClientTaskPtr& task = ctx->task;
+        if (task && task->retry_cnt-- > 0) {
+            if (task->retry_delay) {
+                // try again after delay
+                setTimeout(ctx->task->retry_delay, [this, task](TimerID timerID){
+                    doTask(task);
+                });
+            } else {
+                send(task);
+            }
         } else {
             ctx->errorCallback();
         }

+ 8 - 3
http/client/AsyncHttpClient.h

@@ -9,6 +9,9 @@
 #include "HttpMessage.h"
 #include "HttpParser.h"
 
+#define DEFAULT_FAIL_RETRY_COUNT  3
+#define DEFAULT_FAIL_RETRY_DELAY  1000  // ms
+
 // async => keepalive => connect_pool
 
 namespace hv {
@@ -53,8 +56,9 @@ struct HttpClientTask {
     HttpRequestPtr          req;
     HttpResponseCallback    cb;
 
-    uint64_t  start_time;
-    int       retry_cnt;
+    uint64_t    start_time;
+    int         retry_cnt;
+    int         retry_delay;
 };
 typedef std::shared_ptr<HttpClientTask> HttpClientTaskPtr;
 
@@ -113,7 +117,8 @@ public:
         task->req = req;
         task->cb = resp_cb;
         task->start_time = hloop_now_hrtime(loop_thread.hloop());
-        task->retry_cnt = 3;
+        task->retry_cnt = DEFAULT_FAIL_RETRY_COUNT;
+        task->retry_delay = DEFAULT_FAIL_RETRY_DELAY;
         return send(task);
     }
 

+ 7 - 2
http/client/requests.h

@@ -35,8 +35,9 @@ int main() {
 
 namespace requests {
 
-typedef std::shared_ptr<HttpRequest>  Request;
-typedef std::shared_ptr<HttpResponse> Response;
+typedef HttpRequestPtr          Request;
+typedef HttpResponsePtr         Response;
+typedef HttpResponseCallback    ResponseCallback;
 
 static http_headers DefaultHeaders;
 static http_body    NoBody;
@@ -89,6 +90,10 @@ HV_INLINE Response Delete(const char* url, const http_body& body = NoBody, const
     return request(HTTP_DELETE, url, body, headers);
 }
 
+HV_INLINE int async(Request req, ResponseCallback resp_cb) {
+    return http_client_send_async(req, resp_cb);
+}
+
 }
 
 #endif // HV_REQUESTS_H_