diff --git a/repertory/librepertory/include/comm/curl/dns_cache.hpp b/repertory/librepertory/include/comm/curl/dns_cache.hpp index 659d69e5..7e7b01bd 100644 --- a/repertory/librepertory/include/comm/curl/dns_cache.hpp +++ b/repertory/librepertory/include/comm/curl/dns_cache.hpp @@ -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 diff --git a/repertory/librepertory/src/comm/curl/dns_cache.cpp b/repertory/librepertory/src/comm/curl/dns_cache.cpp index 8e34dcc4..0ac1c3be 100644 --- a/repertory/librepertory/src/comm/curl/dns_cache.cpp +++ b/repertory/librepertory/src/comm/curl/dns_cache.cpp @@ -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 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(mtx_); + auto *cache = curl_share_init(); + if (cache == nullptr) { + return; } - lock_ = std::make_unique(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 */, diff --git a/repertory/librepertory/src/initialize.cpp b/repertory/librepertory/src/initialize.cpp index cc4cddad..543c370b 100644 --- a/repertory/librepertory/src/initialize.cpp +++ b/repertory/librepertory/src/initialize.cpp @@ -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;