Add retries on timeout for http proxy

This commit is contained in:
ozanarmagan 2023-07-04 11:29:49 +03:00
parent 1588cc050a
commit 525df7d1e2
3 changed files with 43 additions and 23 deletions

View File

@ -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 requests

View File

@ -8,39 +8,58 @@ HttpProxy::HttpProxy() : cache(30s){
}
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 cache
uint64_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) {
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)) {
return cache[key];
}
// if not, make http request
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, 30 * 1000);
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, 30 * 1000);
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, 30 * 1000);
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, 30 * 1000);
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;
}
// add to cache
cache.insert(key, 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 cache
uint64_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){
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)){
return cache[key];
}
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"] = "Server error on remote server. Please try again later.";
res.body = j.dump();
}
// add to cache
if(res.status_code != 500){
cache.insert(key, res);
}
return res;
}

View File

@ -15,9 +15,9 @@ long RemoteEmbedder::call_remote_api(const std::string& method, const std::strin
std::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::strin
req_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);
}