Skip to content

Commit

Permalink
polish
Browse files Browse the repository at this point in the history
  • Loading branch information
henryzhx8 committed Aug 8, 2024
1 parent 9f54dd3 commit c5e2580
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 10 deletions.
3 changes: 2 additions & 1 deletion core/common/http/AsynCurlRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ bool AsynCurlRunner::AddRequestToClient(unique_ptr<AsynHttpRequest>&& request) {
request->mBody,
request->mResponse,
headers,
request->mTimeout,
AppConfig::GetInstance()->IsHostIPReplacePolicyEnabled(),
AppConfig::GetInstance()->GetBindInterface());
if (curl == nullptr) {
Expand Down Expand Up @@ -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)));
Expand Down
4 changes: 2 additions & 2 deletions core/common/http/Curl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions core/common/http/Curl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 15 additions & 4 deletions core/common/http/HttpRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,6 +38,8 @@ struct HttpRequest {
std::map<std::string, std::string> mHeader;
std::string mBody;
std::string mHost;
uint32_t mTimeout = sDefaultTimeoutSec;
uint32_t mMaxTryCnt = sDefaultMaxTryCnt;

uint32_t mTryCnt = 1;
time_t mLastSendTime = 0;
Expand All @@ -45,14 +50,18 @@ struct HttpRequest {
const std::string& url,
const std::string& query,
const std::map<std::string, std::string>& 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;
};

Expand All @@ -67,8 +76,10 @@ struct AsynHttpRequest : public HttpRequest {
const std::string& url,
const std::string& query,
const std::map<std::string, std::string>& 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;
Expand Down
3 changes: 2 additions & 1 deletion core/sink/http/HttpSink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ bool HttpSink::AddRequestToClient(std::unique_ptr<HttpSinkRequest>&& request) {
request->mBody,
request->mResponse,
headers,
request->mTimeout,
AppConfig::GetInstance()->IsHostIPReplacePolicyEnabled(),
AppConfig::GetInstance()->GetBindInterface());
if (curl == nullptr) {
Expand Down Expand Up @@ -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)(
Expand Down

0 comments on commit c5e2580

Please sign in to comment.