Commit 1629fb28 authored by Adrien Béraud's avatar Adrien Béraud Committed by Sébastien Blin
Browse files

namedirectory: schedule cache saving



Change-Id: Iff27c0ca7efedfd54bb5b1763fd2178819128172
Reviewed-by: Sébastien Blin's avatarSébastien Blin <sebastien.blin@savoirfairelinux.com>
parent 40cae3db
......@@ -22,6 +22,8 @@
#include "string_utils.h"
#include "fileutils.h"
#include "base64.h"
#include "scheduled_executor.h"
#include "manager.h"
#include <opendht/thread_pool.h>
#include <opendht/crypto.h>
......@@ -42,6 +44,7 @@ constexpr const char* const QUERY_ADDR {"/addr/"};
constexpr const char* const HTTPS_PROTO {"https://"};
constexpr const char* const CACHE_DIRECTORY {"namecache"};
const std::string HEX_PREFIX = "0x";
constexpr std::chrono::seconds SAVE_INTERVAL {5};
/** Parser for URIs. ( protocol ) ( username ) ( hostname ) */
const std::regex URI_VALIDATOR {"^([a-zA-Z]+:(?://)?)?(?:([a-z0-9-_]{1,64})@)?([a-zA-Z0-9\\-._~%!$&'()*+,;=:\\[\\]]+)"};
......@@ -158,7 +161,7 @@ void NameDirectory::lookupAddress(const std::string& addr, LookupCallback cb)
nameCache_.emplace(addr, name);
}
cb(name, Response::found);
saveCache();
scheduleSave();
} else {
cb("", Response::notFound);
}
......@@ -378,6 +381,19 @@ void NameDirectory::registerName(const std::string& addr, const std::string& n,
});
}
void
NameDirectory::scheduleSave()
{
std::weak_ptr<Task> task = Manager::instance().scheduler().scheduleIn([this]{
dht::ThreadPool::io().run([this] {
saveCache();
});
}, SAVE_INTERVAL);
std::swap(saveTask_, task);
if (auto old = task.lock())
old->cancel();
}
void
NameDirectory::saveCache()
{
......@@ -388,7 +404,7 @@ NameDirectory::saveCache()
std::lock_guard<std::mutex> l(lock_);
msgpack::pack(file, nameCache_);
}
JAMI_DBG("Saved %lu name-address mappings", (long unsigned)nameCache_.size());
JAMI_DBG("Saved %lu name-address mappings to %s", (long unsigned)nameCache_.size(), cachePath_.c_str());
}
void
......
......@@ -34,6 +34,8 @@ struct PublicKey;
namespace jami {
class Task;
class NameDirectory
{
public:
......@@ -80,6 +82,7 @@ private:
std::map<std::string, std::string> nameCache_ {};
std::map<std::string, std::string> addrCache_ {};
std::weak_ptr<Task> saveTask_;
std::shared_ptr<dht::Executor> executor_;
std::string nameCache(const std::string& addr) {
......@@ -96,6 +99,7 @@ private:
bool validateName(const std::string& name) const;
static bool verify(const std::string& name, const dht::crypto::PublicKey& publickey, const std::string& signature);
void scheduleSave();
void saveCache();
void loadCache();
};
......
Supports Markdown
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