Browse Source

fix redirect not killTimer

ithewei 2 years ago
parent
commit
bfd8c0bfb9
3 changed files with 20 additions and 12 deletions
  1. 5 5
      examples/curl.cpp
  2. 4 3
      http/client/AsyncHttpClient.cpp
  3. 11 4
      http/client/AsyncHttpClient.h

+ 5 - 5
examples/curl.cpp

@@ -226,16 +226,16 @@ static int parse_cmdline(int argc, char* argv[], HttpRequest* req) {
         }
     }
 
-    // --http2
-    if (http_version == 2) {
-        req->http_major = 2;
-        req->http_minor = 0;
-    }
     // --grpc
     if (grpc) {
         http_version = 2;
         req->content_type = APPLICATION_GRPC;
     }
+    // --http2
+    if (http_version == 2) {
+        req->http_major = 2;
+        req->http_minor = 0;
+    }
     // --timeout
     if (timeout > 0) {
         req->timeout = timeout;

+ 4 - 3
http/client/AsyncHttpClient.cpp

@@ -88,7 +88,7 @@ int AsyncHttpClient::doTask(const HttpClientTaskPtr& task) {
                     resp->Reset();
                     send(ctx->task);
                     // NOTE: detatch from original channel->context
-                    ctx->task = NULL;
+                    ctx->cancelTask();
                 }
             } else {
                 ctx->successCallback();
@@ -139,8 +139,9 @@ int AsyncHttpClient::doTask(const HttpClientTaskPtr& task) {
     if (timeout_ms > 0) {
         ctx->timerID = setTimeout(timeout_ms - elapsed_ms, [&channel](TimerID timerID){
             HttpClientContext* ctx = channel->getContext<HttpClientContext>();
-            assert(ctx->task != NULL);
-            hlogw("%s timeout!", ctx->task->req->url.c_str());
+            if (ctx && ctx->task) {
+                hlogw("%s timeout!", ctx->task->req->url.c_str());
+            }
             if (channel) {
                 channel->close();
             }

+ 11 - 4
http/client/AsyncHttpClient.h

@@ -65,18 +65,25 @@ struct HttpClientContext {
     HttpClientContext() {
         timerID = INVALID_TIMER_ID;
     }
+
     ~HttpClientContext() {
+        cancelTimer();
+    }
+
+    void cancelTimer() {
         if (timerID != INVALID_TIMER_ID) {
             killTimer(timerID);
             timerID = INVALID_TIMER_ID;
         }
     }
 
+    void cancelTask() {
+        cancelTimer();
+        task = NULL;
+    }
+
     void callback() {
-        if (timerID != INVALID_TIMER_ID) {
-            killTimer(timerID);
-            timerID = INVALID_TIMER_ID;
-        }
+        cancelTimer();
         if (task && task->cb) {
             task->cb(resp);
         }