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