From 8a92975fd722eb0a32136a85ba7b4659caa6c9b3 Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Wed, 23 Jul 2025 09:46:29 -0500 Subject: [PATCH] added dns cache --- .../include/comm/curl/curl_comm.hpp | 3 + .../include/comm/curl/dns_cache.hpp | 66 +++++++++++++++++++ .../librepertory/src/comm/curl/dns_cache.cpp | 60 +++++++++++++++++ 3 files changed, 129 insertions(+) create mode 100644 repertory/librepertory/include/comm/curl/dns_cache.hpp create mode 100644 repertory/librepertory/src/comm/curl/dns_cache.cpp diff --git a/repertory/librepertory/include/comm/curl/curl_comm.hpp b/repertory/librepertory/include/comm/curl/curl_comm.hpp index a5100ac7..980a2993 100644 --- a/repertory/librepertory/include/comm/curl/curl_comm.hpp +++ b/repertory/librepertory/include/comm/curl/curl_comm.hpp @@ -23,6 +23,7 @@ #define REPERTORY_INCLUDE_COMM_CURL_CURL_COMM_HPP_ #include "app_config.hpp" +#include "comm/curl/dns_cache.hpp" #include "comm/curl/multi_request.hpp" #include "comm/i_http_comm.hpp" #include "events/event_system.hpp" @@ -210,6 +211,8 @@ public: curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list); } + dns_cache::set_cache(curl); + auto url = construct_url(curl, request.get_path(), cfg) + parameters; curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); diff --git a/repertory/librepertory/include/comm/curl/dns_cache.hpp b/repertory/librepertory/include/comm/curl/dns_cache.hpp new file mode 100644 index 00000000..659d69e5 --- /dev/null +++ b/repertory/librepertory/include/comm/curl/dns_cache.hpp @@ -0,0 +1,66 @@ +/* + Copyright <2018-2025> + + 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; + +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 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_ diff --git a/repertory/librepertory/src/comm/curl/dns_cache.cpp b/repertory/librepertory/src/comm/curl/dns_cache.cpp new file mode 100644 index 00000000..8e34dcc4 --- /dev/null +++ b/repertory/librepertory/src/comm/curl/dns_cache.cpp @@ -0,0 +1,60 @@ +/* + Copyright <2018-2025> + + 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 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(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