From 834309db19e803f8466bb59143d71c8628d66d9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Canna=C3=B2?= Date: Fri, 10 Jul 2020 16:08:36 +0200 Subject: [PATCH] Version check run daily and not just at startup --- src/main.cpp | 43 ++++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index f5001ff90..53e272e69 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -171,6 +171,9 @@ static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, voi } +static char * know_latest_version = NULL; +static unsigned int randID = 0; + static char * main_check_latest_version() { CURL *curl_handle; CURLcode res; @@ -191,6 +194,7 @@ static char * main_check_latest_version() { s+= binary_sha1; s += ")" ; } + s += " " + std::to_string(randID); curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, s.c_str()); curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 10); curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT, 10); @@ -215,10 +219,19 @@ static char * main_check_latest_version() { return chunk.memory; } + void * main_check_latest_version_thread(void *arg) { char * latest_version = main_check_latest_version(); if (latest_version) { - proxy_info("Latest ProxySQL version available: %s\n", latest_version); + if ( + (know_latest_version == NULL) // first check + || (strcmp(know_latest_version,latest_version)) // new version detected + ) { + if (know_latest_version) + free(know_latest_version); + know_latest_version = strdup(latest_version); + proxy_info("Latest ProxySQL version available: %s\n", latest_version); + } } free(latest_version); return NULL; @@ -1598,6 +1611,8 @@ int main(int argc, const char * argv[]) { cpu_timer t; ProxySQL_Main_process_global_variables(argc, argv); GloVars.global.start_time=monotonic_time(); // always initialize it + srand(GloVars.global.start_time*pthread_self()); + randID = rand(); #ifdef DEBUG std::cerr << "Main init global variables completed in "; #endif @@ -1779,26 +1794,32 @@ __start_label: #endif } - if (GloVars.global.version_check) { - pthread_attr_t attr; - pthread_attr_init(&attr); - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); - pthread_t thr; - if (pthread_create(&thr, &attr, main_check_latest_version_thread, NULL) !=0 ) { - perror("Thread creation"); - exit(EXIT_FAILURE); - } - } { unsigned int missed_heartbeats = 0; unsigned long long previous_time = monotonic_time(); unsigned int inner_loops = 0; + unsigned long long time_next_version_check = 0; while (glovars.shutdown==0) { usleep(200000); if (disable_watchdog) { continue; } unsigned long long curtime = monotonic_time(); + if (GloVars.global.version_check) { + if (curtime > time_next_version_check) { + pthread_attr_t attr; + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + pthread_t thr; + if (pthread_create(&thr, &attr, main_check_latest_version_thread, NULL) !=0 ) { + perror("Thread creation"); + exit(EXIT_FAILURE); + } + if (time_next_version_check == 0) + time_next_version_check = curtime; + time_next_version_check += 24*3600*(1000*1000); + } + } inner_loops++; if (curtime >= inner_loops*300000 + previous_time ) { // if this happens, it means that this very simple loop is blocked