From 84f579ab537787ebf3bca55ac7220e5e3e01eafd Mon Sep 17 00:00:00 2001 From: Nick Date: Sun, 15 Sep 2024 08:38:38 -0400 Subject: [PATCH] dns stash (#973) --- .../broadcastify_uploader.cc | 26 +++++++++++++++++++ plugins/openmhz_uploader/openmhz_uploader.cc | 23 ++++++++++++++++ .../rdioscanner_uploader.cc | 23 ++++++++++++++++ 3 files changed, 72 insertions(+) diff --git a/plugins/broadcastify_uploader/broadcastify_uploader.cc b/plugins/broadcastify_uploader/broadcastify_uploader.cc index 32b4426b1..80a56016f 100644 --- a/plugins/broadcastify_uploader/broadcastify_uploader.cc +++ b/plugins/broadcastify_uploader/broadcastify_uploader.cc @@ -21,10 +21,14 @@ struct Broadcastify_Uploader_Data { bool ssl_verify_disable; }; +boost::mutex curl_share_mutex; + class Broadcastify_Uploader : public Plugin_Api { // float aggr_; // my_plugin_aggregator() : aggr_(0) {} Broadcastify_Uploader_Data data; + CURLSH *curl_share; + long curl_dns_ttl; public: std::string get_api_key(std::string short_name) { @@ -101,6 +105,9 @@ class Broadcastify_Uploader : public Plugin_Api { curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + curl_easy_setopt(curl, CURLOPT_SHARE, curl_share); + curl_easy_setopt(curl, CURLOPT_DNS_CACHE_TIMEOUT, curl_dns_ttl); + /* Perform the request, res will get the return code */ res = curl_easy_perform(curl); @@ -169,6 +176,9 @@ class Broadcastify_Uploader : public Plugin_Api { curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_buffer); + curl_easy_setopt(curl, CURLOPT_SHARE, curl_share); + curl_easy_setopt(curl, CURLOPT_DNS_CACHE_TIMEOUT, curl_dns_ttl); + // broadcastify seems to make a habit out of letting their ssl certs expire if (this->data.ssl_verify_disable) { curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); @@ -356,9 +366,25 @@ class Broadcastify_Uploader : public Plugin_Api { return 1; } + // Initialize shared curl cache to reduce DNS lookups (5 min TTL) + curl_share = curl_share_init(); + curl_share_setopt(curl_share, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS); + curl_share_setopt(curl_share, CURLSHOPT_LOCKFUNC, curl_lock_cb); + curl_share_setopt(curl_share, CURLSHOPT_UNLOCKFUNC, curl_unlock_cb); + curl_dns_ttl = 300; + return 0; } + // curl callbacks + static void curl_lock_cb(CURL *handle, curl_lock_data data, curl_lock_access access, void *userptr) { + curl_share_mutex.lock(); + } + + static void curl_unlock_cb(CURL *handle, curl_lock_data data, curl_lock_access access, void *userptr) { + curl_share_mutex.unlock(); + } + /* int init(Config *config, std::vector sources, std::vector systems) { return 0; } int start() { return 0; } diff --git a/plugins/openmhz_uploader/openmhz_uploader.cc b/plugins/openmhz_uploader/openmhz_uploader.cc index f59dabe67..46b983c77 100644 --- a/plugins/openmhz_uploader/openmhz_uploader.cc +++ b/plugins/openmhz_uploader/openmhz_uploader.cc @@ -21,10 +21,14 @@ struct Openmhz_Uploader_Data { std::string openmhz_server; }; +boost::mutex curl_share_mutex; + class Openmhz_Uploader : public Plugin_Api { // float aggr_; // my_plugin_aggregator() : aggr_(0) {} Openmhz_Uploader_Data data; + CURLSH *curl_share; + long curl_dns_ttl; public: Openmhz_System *get_openmhz_system(std::string short_name) { @@ -205,6 +209,9 @@ class Openmhz_Uploader : public Plugin_Api { curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_buffer); + curl_easy_setopt(curl, CURLOPT_SHARE, curl_share); + curl_easy_setopt(curl, CURLOPT_DNS_CACHE_TIMEOUT, curl_dns_ttl); + curl_multi_add_handle(multi_handle, curl); curl_multi_perform(multi_handle, &still_running); @@ -347,9 +354,25 @@ class Openmhz_Uploader : public Plugin_Api { return 1; } + // Initialize shared curl cache to reduce DNS lookups (5 min TTL) + curl_share = curl_share_init(); + curl_share_setopt(curl_share, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS); + curl_share_setopt(curl_share, CURLSHOPT_LOCKFUNC, curl_lock_cb); + curl_share_setopt(curl_share, CURLSHOPT_UNLOCKFUNC, curl_unlock_cb); + curl_dns_ttl = 300; + return 0; } + // curl callbacks + static void curl_lock_cb(CURL *handle, curl_lock_data data, curl_lock_access access, void *userptr) { + curl_share_mutex.lock(); + } + + static void curl_unlock_cb(CURL *handle, curl_lock_data data, curl_lock_access access, void *userptr) { + curl_share_mutex.unlock(); + } + /* int init(Config *config, std::vector sources, std::vector systems) { return 0; } int start() { return 0; } diff --git a/plugins/rdioscanner_uploader/rdioscanner_uploader.cc b/plugins/rdioscanner_uploader/rdioscanner_uploader.cc index 819975256..356419bb5 100644 --- a/plugins/rdioscanner_uploader/rdioscanner_uploader.cc +++ b/plugins/rdioscanner_uploader/rdioscanner_uploader.cc @@ -25,8 +25,12 @@ struct Rdio_Scanner_Uploader_Data { std::string server; }; +boost::mutex curl_share_mutex; + class Rdio_Scanner_Uploader : public Plugin_Api { Rdio_Scanner_Uploader_Data data; + CURLSH *curl_share; + long curl_dns_ttl; public: Rdio_Scanner_System *get_system(std::string short_name) { @@ -244,6 +248,9 @@ class Rdio_Scanner_Uploader : public Plugin_Api { curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_buffer); + curl_easy_setopt(curl, CURLOPT_SHARE, curl_share); + curl_easy_setopt(curl, CURLOPT_DNS_CACHE_TIMEOUT, curl_dns_ttl); + curl_multi_add_handle(multi_handle, curl); curl_multi_perform(multi_handle, &still_running); @@ -395,9 +402,25 @@ class Rdio_Scanner_Uploader : public Plugin_Api { return 1; } + // Initialize shared curl cache to reduce DNS lookups (5 min TTL) + curl_share = curl_share_init(); + curl_share_setopt(curl_share, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS); + curl_share_setopt(curl_share, CURLSHOPT_LOCKFUNC, curl_lock_cb); + curl_share_setopt(curl_share, CURLSHOPT_UNLOCKFUNC, curl_unlock_cb); + curl_dns_ttl = 300; + return 0; } + // curl callbacks + static void curl_lock_cb(CURL *handle, curl_lock_data data, curl_lock_access access, void *userptr) { + curl_share_mutex.lock(); + } + + static void curl_unlock_cb(CURL *handle, curl_lock_data data, curl_lock_access access, void *userptr) { + curl_share_mutex.unlock(); + } + /* int start() { return 0; } int stop() { return 0; }