added dns cache
This commit is contained in:
@@ -23,6 +23,7 @@
|
|||||||
#define REPERTORY_INCLUDE_COMM_CURL_CURL_COMM_HPP_
|
#define REPERTORY_INCLUDE_COMM_CURL_CURL_COMM_HPP_
|
||||||
|
|
||||||
#include "app_config.hpp"
|
#include "app_config.hpp"
|
||||||
|
#include "comm/curl/dns_cache.hpp"
|
||||||
#include "comm/curl/multi_request.hpp"
|
#include "comm/curl/multi_request.hpp"
|
||||||
#include "comm/i_http_comm.hpp"
|
#include "comm/i_http_comm.hpp"
|
||||||
#include "events/event_system.hpp"
|
#include "events/event_system.hpp"
|
||||||
@@ -210,6 +211,8 @@ public:
|
|||||||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list);
|
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dns_cache::set_cache(curl);
|
||||||
|
|
||||||
auto url = construct_url(curl, request.get_path(), cfg) + parameters;
|
auto url = construct_url(curl, request.get_path(), cfg) + parameters;
|
||||||
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
||||||
|
|
||||||
|
66
repertory/librepertory/include/comm/curl/dns_cache.hpp
Normal file
66
repertory/librepertory/include/comm/curl/dns_cache.hpp
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
/*
|
||||||
|
Copyright <2018-2025> <scott.e.graves@protonmail.com>
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
*/
|
||||||
|
#ifndef REPERTORY_INCLUDE_COMM_CURL_DNS_CACHE_HPP_
|
||||||
|
#define REPERTORY_INCLUDE_COMM_CURL_DNS_CACHE_HPP_
|
||||||
|
|
||||||
|
namespace repertory {
|
||||||
|
class dns_cache final {
|
||||||
|
private:
|
||||||
|
struct curl_sh_deleter final {
|
||||||
|
void operator()(CURLSH *ptr) {
|
||||||
|
if (ptr != nullptr) {
|
||||||
|
curl_share_cleanup(ptr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
using curl_sh_t = std::unique_ptr<CURLSH, curl_sh_deleter>;
|
||||||
|
|
||||||
|
public:
|
||||||
|
dns_cache() = delete;
|
||||||
|
dns_cache(const dns_cache &) = delete;
|
||||||
|
dns_cache(dns_cache &&) = delete;
|
||||||
|
~dns_cache() = delete;
|
||||||
|
|
||||||
|
auto operator=(const dns_cache &) -> dns_cache & = delete;
|
||||||
|
auto operator=(dns_cache &&) -> dns_cache & = delete;
|
||||||
|
|
||||||
|
private:
|
||||||
|
static curl_sh_t cache_;
|
||||||
|
static std::unique_ptr<unique_recur_mutex_lock> lock_;
|
||||||
|
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 */);
|
||||||
|
|
||||||
|
static void unlock_callback(CURL * /* curl */, curl_lock_data /* data */,
|
||||||
|
curl_lock_access /* access */, void * /* ptr */);
|
||||||
|
|
||||||
|
public:
|
||||||
|
static void set_cache(CURL *curl);
|
||||||
|
};
|
||||||
|
} // namespace repertory
|
||||||
|
|
||||||
|
#endif // REPERTORY_INCLUDE_COMM_CURL_DNS_CACHE_HPP_
|
60
repertory/librepertory/src/comm/curl/dns_cache.cpp
Normal file
60
repertory/librepertory/src/comm/curl/dns_cache.cpp
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
/*
|
||||||
|
Copyright <2018-2025> <scott.e.graves@protonmail.com>
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
*/
|
||||||
|
#include "comm/curl/dns_cache.hpp"
|
||||||
|
|
||||||
|
namespace repertory {
|
||||||
|
dns_cache::curl_sh_t dns_cache::cache_{dns_cache::create_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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
void dns_cache::lock_callback(CURL * /* curl */, curl_lock_data /* data */,
|
||||||
|
curl_lock_access /* access */, void * /* ptr */) {
|
||||||
|
lock_->lock();
|
||||||
|
}
|
||||||
|
|
||||||
|
void dns_cache::set_cache(CURL *curl) {
|
||||||
|
curl_easy_setopt(curl, CURLOPT_SHARE, cache_.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
void dns_cache::unlock_callback(CURL * /* curl */, curl_lock_data /* data */,
|
||||||
|
curl_lock_access /* access */,
|
||||||
|
void * /* ptr */) {
|
||||||
|
lock_->unlock();
|
||||||
|
}
|
||||||
|
} // namespace repertory
|
Reference in New Issue
Block a user