Small fix after remote test

dev
mbandic 6 days ago
parent e23e1f8cea
commit b04cf84eaa
  1. 19
      lib/mysql.hpp
  2. 29
      src/mysql.cpp
  3. 40
      test/test.cpp

@ -103,12 +103,10 @@ class MySQL {
queue<Connection*> connection_pool;
string path, username, password, database;
uint32_t pool_size;
uint32_t connect_trys = 3;
bool run_tloop = true;
future<void> tloop_future;
time_loop_type tloop_type;
time_t last_loop_time = time(nullptr);
function<void(const string&)> on_error;
time_t last_loop_time;
/**
* Open one database server connection
@ -158,6 +156,9 @@ class MySQL {
void _tloop();
public:
function<void(const string&)> on_error;
uint32_t connect_trys = 3;
/**
* 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);
/**
* 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
*/

@ -21,6 +21,8 @@ marcelb::mysql::MySQL::MySQL(const string _path, const string _username, const s
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() {
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 {
Connection *conn = occupy_connection();
Connection *conn = connection_pool.front();
connection_pool.pop();
if (conn->isValid()) {
release_connection(conn);
connection_pool.push(conn);
} else {
if (!conn->isClosed()){
conn->close();
@ -112,21 +119,15 @@ 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) {
on_error = _on_error;
last_loop_time = time(nullptr);
}
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
on_error("The time loop is not executing properly");
if (last_loop_time + (MYSQL_PERIODIC_INTERNAL_TIME*3/1000) < time(nullptr)) {
if (on_error) {
on_error("The time loop is not executing properly");
}
}
unique_lock<mutex> lock(io);
while (connection_pool.empty()) {

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

Loading…
Cancel
Save