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
C++ algoliadatastoreelasticsearchenterprise-searchfacetingfull-text-searchfuzzy-searchgeosearchin-memoryinstantsearchmerchandisingpineconesearchsearch-enginesemantic-searchsimilarity-searchsite-searchsynonymstypo-tolerancevector-search
Add retries on timeout for http proxy
ozanarmagan525df7d1e2f2
diff --git a/include/http_proxy.h b/include/http_proxy.hindex 4b8315b3..851daa91 100644--- a/include/http_proxy.h+++ b/include/http_proxy.h@@ -36,6 +36,7 @@ class HttpProxy {private:HttpProxy();~HttpProxy() = default;+ http_proxy_res_t call(const std::string& url, const std::string& method, const std::string& body = "", const std::unordered_map<std::string, std::string>& headers = {});// lru cache for http requestsdiff --git a/src/http_proxy.cpp b/src/http_proxy.cppindex b2d2c684..eb562849 100644--- a/src/http_proxy.cpp+++ b/src/http_proxy.cpp@@ -8,39 +8,58 @@ HttpProxy::HttpProxy() : cache(30s){}+http_proxy_res_t HttpProxy::call(const std::string& url, const std::string& method, const std::string& body, const std::unordered_map<std::string, std::string>& headers) {+ HttpClient& client = HttpClient::get_instance();+ http_proxy_res_t res;+ if(method == "GET") {+ res.status_code = client.get_response(url, res.body, res.headers, headers, 20 * 1000);+ } else if(method == "POST") {+ res.status_code = client.post_response(url, body, res.body, res.headers, headers, 20 * 1000);+ } else if(method == "PUT") {+ res.status_code = client.put_response(url, body, res.body, res.headers, 20 * 1000);+ } else if(method == "DELETE") {+ res.status_code = client.delete_response(url, res.body, res.headers, 20 * 1000);+ } else {+ res.status_code = 400;+ nlohmann::json j;+ j["message"] = "Parameter `method` must be one of GET, POST, PUT, DELETE.";+ res.body = j.dump();+ }+ return res;+}++http_proxy_res_t HttpProxy::send(const std::string& url, const std::string& method, const std::string& body, const std::unordered_map<std::string, std::string>& headers) {// check if url is in cacheuint64_t key = StringUtils::hash_wy(url.c_str(), url.size());key = StringUtils::hash_combine(key, StringUtils::hash_wy(method.c_str(), method.size()));key = StringUtils::hash_combine(key, StringUtils::hash_wy(body.c_str(), body.size()));- for (auto& header : headers) {+ for(auto& header : headers){key = StringUtils::hash_combine(key, StringUtils::hash_wy(header.first.c_str(), header.first.size()));key = StringUtils::hash_combine(key, StringUtils::hash_wy(header.second.c_str(), header.second.size()));}- if (cache.contains(key)) {+ if(cache.contains(key)){return cache[key];}- // if not, make http request- HttpClient& client = HttpClient::get_instance();- http_proxy_res_t res;- if(method == "GET") {- res.status_code = client.get_response(url, res.body, res.headers, headers, 30 * 1000);- } else if(method == "POST") {- res.status_code = client.post_response(url, body, res.body, res.headers, headers, 30 * 1000);- } else if(method == "PUT") {- res.status_code = client.put_response(url, body, res.body, res.headers, 30 * 1000);- } else if(method == "DELETE") {- res.status_code = client.delete_response(url, res.body, res.headers, 30 * 1000);- } else {- res.status_code = 400;+ auto res = call(url, method, body, headers);++ if(res.status_code == 500){+ // retry+ res = call(url, method, body, headers);+ }++ if(res.status_code == 500){nlohmann::json j;- j["message"] = "Parameter `method` must be one of GET, POST, PUT, DELETE.";- res.body = j.dump();+ j["message"] = "Server error on remote server. Please try again later.";+ res.body = j.dump();}+- // add to cache- cache.insert(key, res);+ // add to cache+ if(res.status_code != 500){+ cache.insert(key, res);+ }return res;}\ No newline at end of filediff --git a/src/text_embedder_remote.cpp b/src/text_embedder_remote.cppindex 2e12f3b9..bb984e86 100644--- a/src/text_embedder_remote.cpp+++ b/src/text_embedder_remote.cpp@@ -15,9 +15,9 @@ long RemoteEmbedder::call_remote_api(const std::string& method, const std::strinstd::map<std::string, std::string>& headers, const std::unordered_map<std::string, std::string>& req_headers) {if(raft_server == nullptr || raft_server->get_leader_url().empty()) {if(method == "GET") {- return HttpClient::get_instance().get_response(url, res_body, headers, req_headers, 100000, true);+ return HttpClient::get_instance().get_response(url, res_body, headers, req_headers, 45000, true);} else if(method == "POST") {- return HttpClient::get_instance().post_response(url, body, res_body, headers, req_headers, 100000, true);+ return HttpClient::get_instance().post_response(url, body, res_body, headers, req_headers, 45000, true);} else {return 400;}@@ -30,7 +30,7 @@ long RemoteEmbedder::call_remote_api(const std::string& method, const std::strinreq_body["url"] = url;req_body["body"] = body;req_body["headers"] = req_headers;- return HttpClient::get_instance().post_response(leader_url, req_body.dump(), res_body, headers, {}, 10000, true);+ return HttpClient::get_instance().post_response(leader_url, req_body.dump(), res_body, headers, {}, 45000, true);}