Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Fix http curl client timeout API.

Kishore Nallan176a72e6ca05
diff --git a/include/http_client.h b/include/http_client.h
index 8305d1b5..94cb636c 100644
--- a/include/http_client.h
+++ b/include/http_client.h
@@ -26,7 +26,7 @@ private:
static size_t curl_write_download(void *ptr, size_t size, size_t nmemb, FILE *stream);
- static CURL* init_curl(const std::string& url, std::string& response);
+ static CURL* init_curl(const std::string& url, std::string& response, const size_t timeout_ms = 0);
static CURL* init_curl_async(const std::string& url, deferred_req_res_t* req_res, curl_slist*& chunk,
bool send_ts_api_header);
@@ -57,7 +57,7 @@ public:
bool send_ts_api_header = false);
static long delete_response(const std::string& url, std::string& response,
- std::map<std::string, std::string>& res_headers, long timeout_ms=120000,
+ std::map<std::string, std::string>& res_headers, long timeout_ms=4000,
bool send_ts_api_header = false);
static long post_response(const std::string & url, const std::string & body, std::string & response,
diff --git a/src/http_client.cpp b/src/http_client.cpp
index 7b3c274b..c6e4b7fd 100644
--- a/src/http_client.cpp
+++ b/src/http_client.cpp
@@ -19,7 +19,7 @@ long HttpClient::post_response(const std::string &url, const std::string &body,
std::map<std::string, std::string>& res_headers,
const std::unordered_map<std::string, std::string>& headers, long timeout_ms,
bool send_ts_api_header) {
- CURL *curl = init_curl(url, response);
+ CURL *curl = init_curl(url, response, timeout_ms);
if(curl == nullptr) {
return 500;
}
@@ -60,7 +60,7 @@ long HttpClient::post_response_async(const std::string &url, const std::shared_p
long HttpClient::put_response(const std::string &url, const std::string &body, std::string &response,
std::map<std::string, std::string>& res_headers, long timeout_ms,
bool send_ts_api_header) {
- CURL *curl = init_curl(url, response);
+ CURL *curl = init_curl(url, response, timeout_ms);
if(curl == nullptr) {
return 500;
}
@@ -73,7 +73,7 @@ long HttpClient::put_response(const std::string &url, const std::string &body, s
long HttpClient::patch_response(const std::string &url, const std::string &body, std::string &response,
std::map<std::string, std::string>& res_headers, long timeout_ms,
bool send_ts_api_header) {
- CURL *curl = init_curl(url, response);
+ CURL *curl = init_curl(url, response, timeout_ms);
if(curl == nullptr) {
return 500;
}
@@ -86,7 +86,7 @@ long HttpClient::patch_response(const std::string &url, const std::string &body,
long HttpClient::delete_response(const std::string &url, std::string &response,
std::map<std::string, std::string>& res_headers, long timeout_ms,
bool send_ts_api_header) {
- CURL *curl = init_curl(url, response);
+ CURL *curl = init_curl(url, response, timeout_ms);
if(curl == nullptr) {
return 500;
}
@@ -99,7 +99,7 @@ long HttpClient::get_response(const std::string &url, std::string &response,
std::map<std::string, std::string>& res_headers,
const std::unordered_map<std::string, std::string>& headers,
long timeout_ms, bool send_ts_api_header) {
- CURL *curl = init_curl(url, response);
+ CURL *curl = init_curl(url, response, timeout_ms);
if(curl == nullptr) {
return 500;
}
@@ -351,7 +351,7 @@ CURL *HttpClient::init_curl_async(const std::string& url, deferred_req_res_t* re
return curl;
}
-CURL *HttpClient::init_curl(const std::string& url, std::string& response) {
+CURL *HttpClient::init_curl(const std::string& url, std::string& response, const size_t timeout_ms) {
CURL *curl = curl_easy_init();
if(curl == nullptr) {
@@ -369,6 +369,7 @@ CURL *HttpClient::init_curl(const std::string& url, std::string& response) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS, 4000);
+ curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, timeout_ms);
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE);
// to allow self-signed certs
diff --git a/src/raft_server.cpp b/src/raft_server.cpp
index 0ca0b7de..7e18456e 100644
--- a/src/raft_server.cpp
+++ b/src/raft_server.cpp
@@ -308,18 +308,19 @@ void ReplicationState::write_to_leader(const std::shared_ptr<http_req>& request,
}
} else {
std::string api_res;
- long status = HttpClient::post_response(url, request->body, api_res, res_headers, {}, 4000, true);
+ long status = HttpClient::post_response(url, request->body, api_res, res_headers, {}, 10*1000, true);
response->content_type_header = res_headers["content-type"];
response->set_body(status, api_res);
}
} else if(request->http_method == "PUT") {
std::string api_res;
- long status = HttpClient::put_response(url, request->body, api_res, res_headers, 4000, true);
+ long status = HttpClient::put_response(url, request->body, api_res, res_headers, 10*1000, true);
response->content_type_header = res_headers["content-type"];
response->set_body(status, api_res);
} else if(request->http_method == "DELETE") {
std::string api_res;
- long status = HttpClient::delete_response(url, api_res, res_headers, 120000, true);
+ // timeout: 0 since delete can take a long time
+ long status = HttpClient::delete_response(url, api_res, res_headers, 0, true);
response->content_type_header = res_headers["content-type"];
response->set_body(status, api_res);
} else if(request->http_method == "PATCH") {
@@ -327,9 +328,9 @@ void ReplicationState::write_to_leader(const std::shared_ptr<http_req>& request,
route_path* rpath = nullptr;
bool route_found = server->get_route(request->route_hash, &rpath);
- long timeout_ms = 4 * 1000;
+ long timeout_ms = 10 * 1000;
if(route_found && rpath->handler == patch_update_collection) {
- timeout_ms = 300 * 1000; // 5 minutes for patching a collection which can take some time
+ timeout_ms = 0; // patching a collection can take a long time
}
long status = HttpClient::patch_response(url, request->body, api_res, res_headers, timeout_ms, true);
@@ -677,7 +678,7 @@ void ReplicationState::refresh_catchup_status(bool log_msg) {
std::string api_res;
std::map<std::string, std::string> res_headers;
- long status_code = HttpClient::get_response(url, api_res, res_headers, {}, 4000, true);
+ long status_code = HttpClient::get_response(url, api_res, res_headers, {}, 5*1000, true);
if(status_code == 200) {
// compare leader's applied log with local applied to see if we are lagging
nlohmann::json leader_status = nlohmann::json::parse(api_res);
@@ -937,7 +938,7 @@ void ReplicationState::do_snapshot(const std::string& nodes) {
std::string url = get_node_url_path(peer_addr, "/health", protocol);
std::string api_res;
std::map<std::string, std::string> res_headers;
- long status_code = HttpClient::get_response(url, api_res, res_headers, {}, 4000, true);
+ long status_code = HttpClient::get_response(url, api_res, res_headers, {}, 5*1000, true);
bool peer_healthy = (status_code == 200);
//LOG(INFO) << "do_snapshot, status_code: " << status_code;