Skip to content

Commit

Permalink
dns stash (#973)
Browse files Browse the repository at this point in the history
  • Loading branch information
taclane authored Sep 15, 2024
1 parent 4d90da1 commit 84f579a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
26 changes: 26 additions & 0 deletions plugins/broadcastify_uploader/broadcastify_uploader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<Source *> sources, std::vector<System *> systems) { return 0; }
int start() { return 0; }
Expand Down
23 changes: 23 additions & 0 deletions plugins/openmhz_uploader/openmhz_uploader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<Source *> sources, std::vector<System *> systems) { return 0; }
int start() { return 0; }
Expand Down
23 changes: 23 additions & 0 deletions plugins/rdioscanner_uploader/rdioscanner_uploader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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; }
Expand Down

0 comments on commit 84f579a

Please sign in to comment.