Commit efdfbe5e authored by Adrien Béraud's avatar Adrien Béraud

namedirectory: lock cache during lookup

Change-Id: If92032d68a6429695cb698ca6fc497b1b095f3a5
parent f0506e16
......@@ -105,9 +105,9 @@ size_t getContentLength(restbed::Response& reply)
void NameDirectory::lookupAddress(const std::string& addr, LookupCallback cb)
{
try {
auto cacheRes = nameCache_.find(addr);
if (cacheRes != nameCache_.end()) {
cb(cacheRes->second, Response::found);
std::string cacheResult = nameCache(addr);
if (not cacheResult.empty()) {
cb(cacheResult, Response::found);
return;
}
......@@ -179,9 +179,9 @@ void NameDirectory::lookupName(const std::string& n, LookupCallback cb)
}
toLower(name);
auto cacheRes = addrCache_.find(name);
if (cacheRes != addrCache_.end()) {
cb(cacheRes->second, Response::found);
std::string cacheResult = addrCache(name);
if (not cacheResult.empty()) {
cb(cacheResult, Response::found);
return;
}
......@@ -260,9 +260,9 @@ void NameDirectory::registerName(const std::string& addr, const std::string& n,
}
toLower(name);
auto cacheRes = addrCache_.find(name);
if (cacheRes != addrCache_.end()) {
if (cacheRes->second == addr)
auto cacheResult = addrCache(name);
if (not cacheResult.empty()) {
if (cacheResult == addr)
cb(RegistrationResponse::success);
else
cb(RegistrationResponse::alreadyTaken);
......
......@@ -58,12 +58,24 @@ private:
NameDirectory(NameDirectory&&) = delete;
constexpr static const char* const DEFAULT_SERVER_HOST = "ns.jami.net";
std::mutex lock_ {};
const std::string serverHost_ {DEFAULT_SERVER_HOST};
const std::string cachePath_;
std::map<std::string, std::string> nameCache_ {};
std::map<std::string, std::string> addrCache_ {};
std::mutex lock_ {};
std::string nameCache(const std::string& addr) {
std::lock_guard<std::mutex> l(lock_);
auto cacheRes = nameCache_.find(addr);
return cacheRes != nameCache_.end() ? cacheRes->second : std::string{};
}
std::string addrCache(const std::string& name) {
std::lock_guard<std::mutex> l(lock_);
auto cacheRes = addrCache_.find(name);
return cacheRes != addrCache_.end() ? cacheRes->second : std::string{};
}
bool validateName(const std::string& name) const;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment