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

Protect access to proxy's timed cache.

Kishore Nallan8482eacae4e3
diff --git a/include/http_proxy.h b/include/http_proxy.h
index dee7c015..0cdd0d93 100644
--- a/include/http_proxy.h
+++ b/include/http_proxy.h
@@ -45,5 +45,6 @@ class HttpProxy {
// lru cache for http requests
+ std::shared_mutex mutex;
LRU::TimedCache<uint64_t, http_proxy_res_t> cache;
};
\ No newline at end of file
diff --git a/src/http_proxy.cpp b/src/http_proxy.cpp
index f4fceba2..fb62838b 100644
--- a/src/http_proxy.cpp
+++ b/src/http_proxy.cpp
@@ -56,10 +56,14 @@ http_proxy_res_t HttpProxy::send(const std::string& url, const std::string& meth
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()));
}
+
+ std::shared_lock slock(mutex);
if(cache.contains(key)){
return cache[key];
}
+ slock.unlock();
+
http_proxy_res_t res;
for(size_t i = 0; i < num_try; i++){
res = call(url, method, req_body, req_headers, timeout_ms);
@@ -80,6 +84,7 @@ http_proxy_res_t HttpProxy::send(const std::string& url, const std::string& meth
// add to cache
if(res.status_code == 200){
+ std::unique_lock ulock(mutex);
cache.insert(key, res);
}