Skip to content

Commit 13e0e8d

Browse files
committed
Add HttpRequest::connect_timeout
1 parent 6c0ae4c commit 13e0e8d

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

http/HttpMessage.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ class HV_EXPORT HttpMessage {
395395

396396
#define DEFAULT_HTTP_USER_AGENT "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
397397
#define DEFAULT_HTTP_TIMEOUT 60 // s
398+
#define DEFAULT_HTTP_CONNECT_TIMEOUT 10 // s
398399
#define DEFAULT_HTTP_FAIL_RETRY_COUNT 1
399400
#define DEFAULT_HTTP_FAIL_RETRY_DELAY 1000 // ms
400401

@@ -412,9 +413,10 @@ class HV_EXPORT HttpRequest : public HttpMessage {
412413
// client_addr
413414
hv::NetAddr client_addr; // for http server save client addr of request
414415
// for HttpClient
415-
int timeout; // unit: s
416-
int retry_count; // just for AsyncHttpClient fail retry
417-
int retry_delay; // just for AsyncHttpClient fail retry
416+
uint16_t timeout; // unit: s
417+
uint16_t connect_timeout;// unit: s
418+
uint32_t retry_count; // just for AsyncHttpClient fail retry
419+
uint32_t retry_delay; // just for AsyncHttpClient fail retry
418420
unsigned redirect: 1;
419421
unsigned proxy : 1;
420422

@@ -432,6 +434,7 @@ class HV_EXPORT HttpRequest : public HttpMessage {
432434
port = DEFAULT_HTTP_PORT;
433435
path = "/";
434436
timeout = DEFAULT_HTTP_TIMEOUT;
437+
connect_timeout = DEFAULT_HTTP_CONNECT_TIMEOUT;
435438
retry_count = DEFAULT_HTTP_FAIL_RETRY_COUNT;
436439
retry_delay = DEFAULT_HTTP_FAIL_RETRY_DELAY;
437440
redirect = 1;

http/client/AsyncHttpClient.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ int AsyncHttpClient::doTask(const HttpClientTaskPtr& task) {
128128
sendRequest(channel);
129129
} else {
130130
// startConnect
131+
if (req->connect_timeout > 0) {
132+
channel->setConnectTimeout(req->connect_timeout * 1000);
133+
}
131134
channel->startConnect();
132135
}
133136

http/client/http_client.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ int __http_client_send(http_client_t* cli, HttpRequest* req, HttpResponse* resp)
487487
int fail_cnt = 0;
488488
if (connfd <= 0) {
489489
connect:
490-
connfd = http_client_connect(cli, req->host.c_str(), req->port, https, req->timeout);
490+
connfd = http_client_connect(cli, req->host.c_str(), req->port, https, MIN(req->connect_timeout, req->timeout));
491491
if (connfd < 0) {
492492
return connfd;
493493
}

0 commit comments

Comments
 (0)