Small fix after remote test

dev
mbandic 2 months ago
parent e23e1f8cea
commit b04cf84eaa
  1. 19
      lib/mysql.hpp
  2. 27
      src/mysql.cpp
  3. 38
      test/test.cpp

@ -103,12 +103,10 @@ class MySQL {
queue<Connection*> connection_pool; queue<Connection*> connection_pool;
string path, username, password, database; string path, username, password, database;
uint32_t pool_size; uint32_t pool_size;
uint32_t connect_trys = 3;
bool run_tloop = true; bool run_tloop = true;
future<void> tloop_future; future<void> tloop_future;
time_loop_type tloop_type; time_loop_type tloop_type;
time_t last_loop_time = time(nullptr); time_t last_loop_time;
function<void(const string&)> on_error;
/** /**
* Open one database server connection * Open one database server connection
@ -158,6 +156,9 @@ class MySQL {
void _tloop(); void _tloop();
public: public:
function<void(const string&)> on_error;
uint32_t connect_trys = 3;
/** /**
* MySQL constructor, * MySQL constructor,
@ -167,18 +168,6 @@ public:
*/ */
MySQL(const string _path, const string _username, const string _password, const string _db, const uint32_t _available = 1, const time_loop_type _engine_type = time_loop_type::internal); MySQL(const string _path, const string _username, const string _password, const string _db, const uint32_t _available = 1, const time_loop_type _engine_type = time_loop_type::internal);
/**
* Define the maximum number of attempts to
* reconnect to the server
*/
void set_connect_trys(const uint32_t _trys);
/**
* Set callback on error
*/
void set_on_error(function<void(const string&)> _on_error);
/** /**
* Execute the SQL statement * Execute the SQL statement
*/ */

@ -21,6 +21,8 @@ marcelb::mysql::MySQL::MySQL(const string _path, const string _username, const s
return; return;
}); });
} }
// set on initialization to avoid the error
last_loop_time = time(nullptr);
} }
@ -94,11 +96,16 @@ bool marcelb::mysql::MySQL::disconnect_connection(Connection* connection) {
} }
void marcelb::mysql::MySQL::_tloop() { void marcelb::mysql::MySQL::_tloop() {
for (size_t i = 0; i < pool_size && run_tloop; i++) { if (!run_tloop) {
return;
}
lock_guard<mutex> lock(io);
for (size_t i=0; i<connection_pool.size(); i++) {
try { try {
Connection *conn = occupy_connection(); Connection *conn = connection_pool.front();
connection_pool.pop();
if (conn->isValid()) { if (conn->isValid()) {
release_connection(conn); connection_pool.push(conn);
} else { } else {
if (!conn->isClosed()){ if (!conn->isClosed()){
conn->close(); conn->close();
@ -112,22 +119,16 @@ void marcelb::mysql::MySQL::_tloop() {
} }
} }
} }
last_loop_time = time(nullptr);
}
void marcelb::mysql::MySQL::set_connect_trys(const uint32_t _trys) {
// lock_guard<mutex> lock(io);
connect_trys = _trys;
}
void marcelb::mysql::MySQL::set_on_error(function<void(const string&)> _on_error) { last_loop_time = time(nullptr);
on_error = _on_error;
} }
Connection* marcelb::mysql::MySQL::occupy_connection() { Connection* marcelb::mysql::MySQL::occupy_connection() {
if (last_loop_time + (MYSQL_PERIODIC_INTERNAL_TIME/1000)*3 < time(nullptr) && on_error) { // ako je zadnje vrijeme + 3 intervala manje od trenutnog vremena emitiraj grešku if (last_loop_time + (MYSQL_PERIODIC_INTERNAL_TIME*3/1000) < time(nullptr)) {
if (on_error) {
on_error("The time loop is not executing properly"); on_error("The time loop is not executing properly");
} }
}
unique_lock<mutex> lock(io); unique_lock<mutex> lock(io);
while (connection_pool.empty()) { while (connection_pool.empty()) {
condition.wait(lock); condition.wait(lock);

@ -13,19 +13,20 @@ using namespace marcelb::asynco;
int main() { int main() {
try { try {
MySQL mydb("tcp://192.168.2.10:3306", "dinio", "H€r5elfInd1aH@nds", "dinio", 5, time_loop_type::internal); // MySQL mydb("tcp://192.168.2.10:3306", "dinio", "H€r5elfInd1aH@nds", "dinio", 5, time_loop_type::internal);
// MySQL mydb("tcp://bitelex.ddns.net:3306", "dinio", "H€r5elfInd1aH@nds", "dinio", 10, time_loop_type::external); MySQL mydb("tcp://bitelex.ddns.net:3306", "dinio", "H€r5elfInd1aH@nds", "dinio", 5, time_loop_type::external);
// MySQL mydb("tcp://bitelex.ddns.net:3306", "dinio", "H€r5elfInd1aH@nds", "dinio", 5); // MySQL mydb("tcp://bitelex.ddns.net:3306", "dinio", "H€r5elfInd1aH@nds", "dinio", 5);
mydb.set_on_error( [](const string& error) { mydb.on_error = [](const string& error) {
cout << error << endl; cout << error << endl;
}); };
periodic mysql_tloop ( [&mydb] () { periodic mysql_tloop ( [&mydb] () {
cout << "loop---------------------------" << endl;
mydb.tloop(); mydb.tloop();
}, MYSQL_PERIODIC_INTERNAL_TIME); }, MYSQL_PERIODIC_INTERNAL_TIME);
while (true) {
sleep(5); sleep(5);
auto start = high_resolution_clock::now(); auto start = high_resolution_clock::now();
@ -44,8 +45,8 @@ int main() {
cout << column_name << endl; cout << column_name << endl;
} }
} catch (const string err) { } catch (const SQLException error) {
cout << err << endl; cout << error.what() << endl;
} }
}); });
@ -63,8 +64,8 @@ int main() {
cout << column_name << endl; cout << column_name << endl;
} }
} catch (const string err) { } catch (const SQLException error) {
cout << err << endl; cout << error.what() << endl;
} }
}); });
@ -82,8 +83,8 @@ int main() {
cout << column_name << endl; cout << column_name << endl;
} }
} catch (const string err) { } catch (const SQLException error) {
cout << err << endl; cout << error.what() << endl;
} }
}); });
@ -101,8 +102,8 @@ int main() {
cout << column_name << endl; cout << column_name << endl;
} }
} catch (const string err) { } catch (const SQLException error) {
cout << err << endl; cout << error.what() << endl;
} }
}); });
@ -120,8 +121,8 @@ int main() {
cout << column_name << endl; cout << column_name << endl;
} }
} catch (const string err) { } catch (const SQLException error) {
cout << err << endl; cout << error.what() << endl;
} }
}); });
@ -139,8 +140,8 @@ int main() {
cout << column_name << endl; cout << column_name << endl;
} }
} catch (const string err) { } catch (const SQLException error) {
cout << err << endl; cout << error.what() << endl;
} }
}); });
@ -156,8 +157,9 @@ int main() {
auto duration = duration_cast<microseconds>(end - start); auto duration = duration_cast<microseconds>(end - start);
cout << "-------------Izvršilo se za: " << (double)(duration.count() / 1000.0) << " ms"<< endl; cout << "-------------Izvršilo se za: " << (double)(duration.count() / 1000.0) << " ms"<< endl;
sleep(100);
}
sleep(100);
} catch (const SQLException error) { } catch (const SQLException error) {
cout << error.what() << endl; cout << error.what() << endl;

Loading…
Cancel
Save