Skip to content

Commit

Permalink
refactor reconnect_all and change db_con_pool to deque
Browse files Browse the repository at this point in the history
  • Loading branch information
dr3mro committed Jan 1, 2025
1 parent 74ce0d2 commit 94fb6c8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 26 deletions.
34 changes: 30 additions & 4 deletions src/database/databaseconnectionpool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ DatabaseConnectionPool::DatabaseConnectionPool() : configurator_(Store::getObjec
auto conn = future.get();
if (conn != nullptr)
{
databaseConnections.push(conn);
databaseConnections.push_back(conn);
Message::InitMessage(fmt::format("Connection {}/{} created successfully.", i + 1, config.max_conn));
connectionEstablished = true;
break;
Expand All @@ -86,7 +86,7 @@ DatabaseConnectionPool::DatabaseConnectionPool() : configurator_(Store::getObjec
{
while (!databaseConnections.empty())
{
databaseConnections.pop();
databaseConnections.pop_front();
}

Message::ErrorMessage(fmt::format("Failed to establish connection {} after {} attempts.", i + 1, MAX_RETRIES));
Expand Down Expand Up @@ -114,7 +114,7 @@ std::shared_ptr<Database> DatabaseConnectionPool::get_connection()

std::shared_ptr<Database> db_ptr = std::move(databaseConnections.front());

databaseConnections.pop();
databaseConnections.pop_front();

return db_ptr;
}
Expand All @@ -124,7 +124,33 @@ void DatabaseConnectionPool::return_connection(std::shared_ptr<Database>&& db_pt
if (db_ptr != nullptr)
{
std::lock_guard<std::mutex> lock(mutex);
databaseConnections.push(std::move(db_ptr));
databaseConnections.push_back(std::move(db_ptr));
cv.notify_one();
}
}

void DatabaseConnectionPool::reconnect_all()
{
std::lock_guard<std::mutex> lock(mutex);
auto connection = databaseConnections.begin();
while (connection != databaseConnections.end())
{
try
{
if ((*connection)->reconnect())
{
Message::InfoMessage(fmt::format("Database connection id: {} link is re-established", static_cast<void*>(connection->get())));
}
else
{
Message::ErrorMessage(fmt::format("Failed to reconnect to database {}", static_cast<void*>(connection->get())));
}
}
catch (const std::exception& e)
{
Message::CriticalMessage(fmt::format("Reconnect exception: {}", e.what()));
}

connection++;
}
}
5 changes: 3 additions & 2 deletions src/database/databaseconnectionpool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

#include <condition_variable>
#include <cstdint>
#include <deque>
#include <memory>
#include <mutex>
#include <queue>

class Database;
class Configurator;
Expand All @@ -26,10 +26,11 @@ class DatabaseConnectionPool

std::shared_ptr<Database> get_connection();
void return_connection(std::shared_ptr<Database> &&db_ptr);
void reconnect_all();

private:
std::shared_ptr<Database> createDatabaseConnection(const auto &config_);
std::queue<std::shared_ptr<Database>> databaseConnections;
std::deque<std::shared_ptr<Database>> databaseConnections;
std::mutex mutex;
std::condition_variable cv;
static constexpr std::uint16_t TIMEOUT = 2;
Expand Down
19 changes: 1 addition & 18 deletions src/database/watchdog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ WatchDog::WatchDog() : databaseConnectionPool(Store::getObject<DatabaseConnectio
if (db_ptr->reconnect())
{
Message::InfoMessage(fmt::format("Database connection id: {} link is re-established", static_cast<void *>(db_ptr.get())));
reconnect_all();
databaseConnectionPool->reconnect_all();
}
else
{
Expand Down Expand Up @@ -80,20 +80,3 @@ WatchDog::~WatchDog()
monitor_thread.join();
}
}

void WatchDog::reconnect_all()
{
auto db_ptr = databaseConnectionPool->get_connection();

while (!db_ptr->check_connection())
{
if (db_ptr->reconnect())
{
Message::InfoMessage(fmt::format("Database connection id: {} link is re-established", static_cast<void *>(db_ptr.get())));
}
databaseConnectionPool->return_connection(std::move(db_ptr));
db_ptr = databaseConnectionPool->get_connection();
};

databaseConnectionPool->return_connection(std::move(db_ptr));
}
2 changes: 0 additions & 2 deletions src/database/watchdog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ class WatchDog
virtual ~WatchDog();

private:
void reconnect_all();

std::atomic<bool> should_monitor{true};
std::shared_ptr<DatabaseConnectionPool> databaseConnectionPool;
std::chrono::seconds check_interval{1};
Expand Down

0 comments on commit 94fb6c8

Please sign in to comment.