From c60f2b245b115731f857069a3ced361aebf1bf77 Mon Sep 17 00:00:00 2001 From: moneroexamples Date: Wed, 11 Jul 2018 13:46:04 +0800 Subject: [PATCH] mysql_ping_thread decoupled from XmrAccounts --- main.cpp | 36 +++++++++++++++++++++++++++++++++ src/MySqlAccounts.cpp | 41 -------------------------------------- src/MySqlAccounts.h | 3 --- src/YourMoneroRequests.cpp | 4 ---- 4 files changed, 36 insertions(+), 48 deletions(-) diff --git a/main.cpp b/main.cpp index c051839..747846a 100755 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,7 @@ #include "src/CmdLineOptions.h" #include "src/MicroCore.h" #include "src/YourMoneroRequests.h" +#include "src/ThreadRAII.h" #include #include @@ -201,6 +202,41 @@ catch(std::exception const& e) return EXIT_FAILURE; } +// at this point we should be connected to the mysql + +// mysql connection will timeout after few hours +// of iddle time. so we have this tiny helper +// thread to ping mysql, thus keeping it alive. +// +// "A completely different way to tackle this, +// if your program doesn’t block forever waiting on I/O while idle, +// is to periodically call Connection::ping(). [12] +// This sends the smallest possible amount of data to the database server, +// which will reset its idle timer and cause it to respond, so ping() returns true. +// If it returns false instead, you know you need to reconnect to the server. +// Periodic pinging is easiest to do if your program uses asynchronous I/O, +// threads, or some kind of event loop to ensure that you can call +// something periodically even while the rest of the program has nothing to do." +// from: https://tangentsoft.net/mysql++/doc/html/userman/tutorial.html#connopts +// +auto conn = mysql_accounts->get_connection(); +xmreg::ThreadRAII mysql_ping_thread( + std::thread([&conn] + { + while (true) + { + std::this_thread::sleep_for(chrono::seconds(7200)); // 2 hours + + if (!conn->ping()) + { + cerr << "Pinging mysql failed. stoping mysql pinging thread. \n"; + break; + } + + cout << "Mysql ping successful. \n" ; + } + }), xmreg::ThreadRAII::DtorAction::detach); + // create REST JSON API services xmreg::YourMoneroRequests open_monero(mysql_accounts, current_bc_status); diff --git a/src/MySqlAccounts.cpp b/src/MySqlAccounts.cpp index dbf7262..5a6fe55 100755 --- a/src/MySqlAccounts.cpp +++ b/src/MySqlAccounts.cpp @@ -612,47 +612,6 @@ MySqlAccounts::disconnect() } -/** - * A completely different way to tackle this, - * if your program doesn’t block forever waiting on I/O while idle, - * is to periodically call Connection::ping(). [12] - * This sends the smallest possible amount of data to the database server, - * which will reset its idle timer and cause it to respond, so ping() returns true. - * If it returns false instead, you know you need to reconnect to the server. - * Periodic pinging is easiest to do if your program uses asynchronous I/O, - * threads, or some kind of event loop to ensure that you can call - * something periodically even while the rest of the program has nothing to do. - * - * from: https://tangentsoft.net/mysql++/doc/html/userman/tutorial.html#connopts - */ -void -MySqlAccounts::launch_mysql_pinging_thread() -{ - // lambda can only capture local variables, so we make - // copy of the connection's shared pointer - shared_ptr conn_ptr = conn; - - std::thread ping_thread ([conn_ptr]() - { - while (true) - { - std::this_thread::sleep_for(chrono::seconds(7200)); // 2 hours - - if (!conn_ptr->ping()) - { - cerr << "Pinging mysql failed. stoping mysql pinging thread. \n"; - break; - } - - cout << "Mysql ping successful. \n" ; - } - }); - - // run this in the background forever - // we dont need to wait for it to finish - ping_thread.detach(); -} - shared_ptr MySqlAccounts::get_connection() { diff --git a/src/MySqlAccounts.h b/src/MySqlAccounts.h index e72b6c8..8eedc48 100755 --- a/src/MySqlAccounts.h +++ b/src/MySqlAccounts.h @@ -189,9 +189,6 @@ public: bool get_total_recieved(const uint64_t& account_id, uint64_t& amount); - void - launch_mysql_pinging_thread(); - void disconnect(); diff --git a/src/YourMoneroRequests.cpp b/src/YourMoneroRequests.cpp index 05f3f6f..55a4ad5 100755 --- a/src/YourMoneroRequests.cpp +++ b/src/YourMoneroRequests.cpp @@ -34,10 +34,6 @@ YourMoneroRequests::YourMoneroRequests( xmr_accounts {_acc}, current_bc_status {_current_bc_status} { - // mysql connection will timeout after few hours - // of iddle time. so we have this tiny helper - // thread to ping mysql, thus keeping it alive - xmr_accounts->launch_mysql_pinging_thread(); }