diff --git a/core/common/http/AsynCurlRunner.cpp b/core/common/http/AsynCurlRunner.cpp index f0647c4cd3..a8592f33d6 100644 --- a/core/common/http/AsynCurlRunner.cpp +++ b/core/common/http/AsynCurlRunner.cpp @@ -83,6 +83,7 @@ bool AsynCurlRunner::AddRequestToClient(unique_ptr&& request) { request->mBody, request->mResponse, headers, + request->mTimeout, AppConfig::GetInstance()->IsHostIPReplacePolicyEnabled(), AppConfig::GetInstance()->GetBindInterface()); if (curl == nullptr) { @@ -192,7 +193,7 @@ void AsynCurlRunner::HandleCompletedRequests() { } default: // considered as network error - if (++request->mTryCnt <= 3) { + if (++request->mTryCnt <= request->mMaxTryCnt) { LOG_WARNING(sLogger, ("failed to send request", "retry immediately")("retryCnt", request->mTryCnt)( "errMsg", curl_easy_strerror(msg->data.result))); diff --git a/core/common/http/Curl.cpp b/core/common/http/Curl.cpp index 5a45d4f48a..905ccf3a18 100644 --- a/core/common/http/Curl.cpp +++ b/core/common/http/Curl.cpp @@ -70,9 +70,9 @@ CURL* CreateCurlHandler(const std::string& method, const std::string& body, HttpResponse& response, curl_slist*& headers, + uint32_t timeout, bool replaceHostWithIp, - const std::string& intf, - uint32_t timeout) { + const std::string& intf) { static DnsCache* dnsCache = DnsCache::GetInstance(); CURL* curl = curl_easy_init(); diff --git a/core/common/http/Curl.h b/core/common/http/Curl.h index 109d3d0cd4..c9aa79f576 100644 --- a/core/common/http/Curl.h +++ b/core/common/http/Curl.h @@ -35,8 +35,8 @@ CURL* CreateCurlHandler(const std::string& method, const std::string& body, HttpResponse& response, curl_slist*& headers, + uint32_t timeout, bool replaceHostWithIp = true, - const std::string& intf = "", - uint32_t timeout = 15); + const std::string& intf = ""); } // namespace logtail diff --git a/core/common/http/HttpRequest.h b/core/common/http/HttpRequest.h index da4ae71cc5..c03d03a75d 100644 --- a/core/common/http/HttpRequest.h +++ b/core/common/http/HttpRequest.h @@ -24,6 +24,9 @@ namespace logtail { +static constexpr uint32_t sDefaultTimeoutSec = 15; +static constexpr uint32_t sDefaultMaxTryCnt = 3; + struct HttpRequest { std::string mMethod; // TODO: upgrade curl to 7.62, and replace the following 4 members @@ -35,6 +38,8 @@ struct HttpRequest { std::map mHeader; std::string mBody; std::string mHost; + uint32_t mTimeout = sDefaultTimeoutSec; + uint32_t mMaxTryCnt = sDefaultMaxTryCnt; uint32_t mTryCnt = 1; time_t mLastSendTime = 0; @@ -45,14 +50,18 @@ struct HttpRequest { const std::string& url, const std::string& query, const std::map& header, - const std::string& body) + const std::string& body, + uint32_t timeout = sDefaultTimeoutSec, + uint32_t maxTryCnt = sDefaultMaxTryCnt) : mMethod(method), mHTTPSFlag(httpsFlag), mUrl(url), mQueryString(query), mHeader(header), mBody(body), - mHost(host) {} + mHost(host), + mTimeout(timeout), + mMaxTryCnt(maxTryCnt) {} virtual ~HttpRequest() = default; }; @@ -67,8 +76,10 @@ struct AsynHttpRequest : public HttpRequest { const std::string& url, const std::string& query, const std::map& header, - const std::string& body) - : HttpRequest(method, httpsFlag, host, url, query, header, body) {} + const std::string& body, + uint32_t timeout = sDefaultTimeoutSec, + uint32_t maxTryCnt = sDefaultMaxTryCnt) + : HttpRequest(method, httpsFlag, host, url, query, header, body, timeout, maxTryCnt) {} virtual bool IsContextValid() const = 0; virtual void OnSendDone(const HttpResponse& response) = 0; diff --git a/core/sink/http/HttpSink.cpp b/core/sink/http/HttpSink.cpp index da78ca46e6..3771a4fe39 100644 --- a/core/sink/http/HttpSink.cpp +++ b/core/sink/http/HttpSink.cpp @@ -84,6 +84,7 @@ bool HttpSink::AddRequestToClient(std::unique_ptr&& request) { request->mBody, request->mResponse, headers, + request->mTimeout, AppConfig::GetInstance()->IsHostIPReplacePolicyEnabled(), AppConfig::GetInstance()->GetBindInterface()); if (curl == nullptr) { @@ -204,7 +205,7 @@ void HttpSink::HandleCompletedRequests() { } default: // considered as network error - if (++request->mTryCnt <= 3) { + if (++request->mTryCnt <= request->mMaxTryCnt) { LOG_WARNING( sLogger, ("failed to send request", "retry immediately")("item address", request->mItem)(