refactor dns cache
This commit is contained in:
@@ -50,8 +50,6 @@ private:
|
||||
static std::recursive_mutex mtx_;
|
||||
|
||||
private:
|
||||
static auto create_cache() -> CURLSH *;
|
||||
|
||||
static void lock_callback(CURL * /* curl */, curl_lock_data /* data */,
|
||||
curl_lock_access /* access */, void * /* ptr */);
|
||||
|
||||
@@ -59,6 +57,10 @@ private:
|
||||
curl_lock_access /* access */, void * /* ptr */);
|
||||
|
||||
public:
|
||||
static void cleanup();
|
||||
|
||||
static void init();
|
||||
|
||||
static void set_cache(CURL *curl);
|
||||
};
|
||||
} // namespace repertory
|
||||
|
@@ -22,25 +22,25 @@
|
||||
#include "comm/curl/dns_cache.hpp"
|
||||
|
||||
namespace repertory {
|
||||
dns_cache::curl_sh_t dns_cache::cache_{dns_cache::create_cache()};
|
||||
dns_cache::curl_sh_t dns_cache::cache_;
|
||||
|
||||
std::unique_ptr<unique_recur_mutex_lock> dns_cache::lock_;
|
||||
|
||||
std::recursive_mutex dns_cache::mtx_;
|
||||
|
||||
auto dns_cache::create_cache() -> CURLSH * {
|
||||
auto *ret = curl_share_init();
|
||||
if (ret == nullptr) {
|
||||
return ret;
|
||||
void dns_cache::cleanup() { cache_.reset(nullptr); }
|
||||
|
||||
void dns_cache::init() {
|
||||
lock_ = std::make_unique<unique_recur_mutex_lock>(mtx_);
|
||||
auto *cache = curl_share_init();
|
||||
if (cache == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
lock_ = std::make_unique<unique_recur_mutex_lock>(mtx_);
|
||||
|
||||
curl_share_setopt(ret, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
|
||||
curl_share_setopt(ret, CURLSHOPT_LOCKFUNC, lock_callback);
|
||||
curl_share_setopt(ret, CURLSHOPT_UNLOCKFUNC, unlock_callback);
|
||||
|
||||
return ret;
|
||||
curl_share_setopt(cache, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
|
||||
curl_share_setopt(cache, CURLSHOPT_LOCKFUNC, lock_callback);
|
||||
curl_share_setopt(cache, CURLSHOPT_UNLOCKFUNC, unlock_callback);
|
||||
cache_.reset(cache);
|
||||
}
|
||||
|
||||
void dns_cache::lock_callback(CURL * /* curl */, curl_lock_data /* data */,
|
||||
|
@@ -48,6 +48,10 @@
|
||||
#include "utils/path.hpp"
|
||||
#endif // defined(PROJECT_REQUIRE_ALPINE) && !defined (PROJECT_IS_MINGW)
|
||||
|
||||
#if defined(PROJECT_ENABLE_CURL)
|
||||
#include "comm/curl/dns_cache.hpp"
|
||||
#endif // defined(PROJECT_ENABLE_CURL)
|
||||
|
||||
namespace repertory {
|
||||
auto project_initialize() -> bool {
|
||||
#if defined(PROJECT_REQUIRE_ALPINE) && !defined(PROJECT_IS_MINGW)
|
||||
@@ -88,6 +92,8 @@ auto project_initialize() -> bool {
|
||||
if (res != 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
dns_cache::init();
|
||||
}
|
||||
#endif // defined(PROJECT_ENABLE_CURL)
|
||||
|
||||
@@ -96,6 +102,7 @@ auto project_initialize() -> bool {
|
||||
auto res = sqlite3_initialize();
|
||||
if (res != SQLITE_OK) {
|
||||
#if defined(PROJECT_ENABLE_CURL)
|
||||
dns_cache::cleanup();
|
||||
curl_global_cleanup();
|
||||
#endif // defined(PROJECT_ENABLE_CURL)
|
||||
return false;
|
||||
|
Reference in New Issue
Block a user