mysql_ping_thread decoupled from XmrAccounts

pull/93/merge
moneroexamples 6 years ago
parent 74f47cda25
commit c60f2b245b

@ -1,6 +1,7 @@
#include "src/CmdLineOptions.h"
#include "src/MicroCore.h"
#include "src/YourMoneroRequests.h"
#include "src/ThreadRAII.h"
#include <iostream>
#include <memory>
@ -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 doesnt 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);

@ -612,47 +612,6 @@ MySqlAccounts::disconnect()
}
/**
* A completely different way to tackle this,
* if your program doesnt 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<MySqlConnector> 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<MySqlConnector>
MySqlAccounts::get_connection()
{

@ -189,9 +189,6 @@ public:
bool
get_total_recieved(const uint64_t& account_id, uint64_t& amount);
void
launch_mysql_pinging_thread();
void
disconnect();

@ -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();
}

Loading…
Cancel
Save