Add methods to get DbClient connection status (#436)
This commit is contained in:
parent
26dca0a910
commit
4423d836f4
|
@ -1013,6 +1013,11 @@ class HttpAppFramework : public trantor::NonCopyable
|
|||
*/
|
||||
virtual orm::DbClientPtr getFastDbClient(
|
||||
const std::string &name = "default") = 0;
|
||||
/**
|
||||
* @brief Check if all database clients in the framework are available
|
||||
* (connect to the database successfully).
|
||||
*/
|
||||
virtual bool areAllDbClientsAvailable() const noexcept = 0;
|
||||
|
||||
/// Create a database client
|
||||
/**
|
||||
|
|
|
@ -52,6 +52,7 @@ class DbClientManager : public trantor::NonCopyable
|
|||
const std::string &filename,
|
||||
const std::string &name,
|
||||
const bool isFast);
|
||||
bool areAllDbClientsAvailable() const noexcept;
|
||||
|
||||
private:
|
||||
std::map<std::string, DbClientPtr> dbClientsMap_;
|
||||
|
|
|
@ -37,6 +37,13 @@ void DbClientManager::createDbClient(const std::string &dbType,
|
|||
const std::string &filename,
|
||||
const std::string &name,
|
||||
const bool isFast)
|
||||
{
|
||||
LOG_FATAL << "No database is supported by drogon, please install the "
|
||||
"database development library first.";
|
||||
abort();
|
||||
}
|
||||
|
||||
bool DbClientManager::areAllDbClientsAvailable() const noexcept
|
||||
{
|
||||
LOG_FATAL << "No database is supported by drogon, please install the "
|
||||
"database development library first.";
|
||||
|
|
|
@ -328,7 +328,7 @@ HttpAppFramework &HttpAppFrameworkImpl::setLogPath(
|
|||
if (access(logPath.c_str(), 0) != 0)
|
||||
#endif
|
||||
{
|
||||
std::cerr << "Log path dose not exist!\n";
|
||||
std::cerr << "Log path does not exist!\n";
|
||||
exit(1);
|
||||
}
|
||||
#ifdef _WIN32
|
||||
|
@ -943,3 +943,8 @@ HttpAppFramework &HttpAppFrameworkImpl::addALocation(
|
|||
filters);
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool HttpAppFrameworkImpl::areAllDbClientsAvailable() const noexcept
|
||||
{
|
||||
return dbClientManagerPtr_->areAllDbClientsAvailable();
|
||||
}
|
|
@ -432,6 +432,8 @@ class HttpAppFrameworkImpl : public HttpAppFramework
|
|||
#endif
|
||||
}
|
||||
|
||||
virtual bool areAllDbClientsAvailable() const noexcept override;
|
||||
|
||||
private:
|
||||
virtual void registerHttpController(
|
||||
const std::string &pathPattern,
|
||||
|
|
|
@ -181,6 +181,13 @@ class DbClient : public trantor::NonCopyable
|
|||
virtual void newTransactionAsync(
|
||||
const std::function<void(const std::shared_ptr<Transaction> &)>
|
||||
&callback) = 0;
|
||||
/**
|
||||
* @brief Check if there is a connection successfully established.
|
||||
*
|
||||
* @return true
|
||||
* @return false
|
||||
*/
|
||||
virtual bool hasAvailableConnections() const noexcept = 0;
|
||||
|
||||
ClientType type() const
|
||||
{
|
||||
|
|
|
@ -440,3 +440,9 @@ DbConnectionPtr DbClientImpl::newConnection(trantor::EventLoop *loop)
|
|||
// std::cout<<"newConn end"<<connPtr<<std::endl;
|
||||
return connPtr;
|
||||
}
|
||||
|
||||
bool DbClientImpl::hasAvailableConnections() const noexcept
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(connectionsMutex_);
|
||||
return (!readyConnections_.empty()) || (!busyConnections_.empty());
|
||||
}
|
|
@ -50,6 +50,7 @@ class DbClientImpl : public DbClient,
|
|||
virtual void newTransactionAsync(
|
||||
const std::function<void(const std::shared_ptr<Transaction> &)>
|
||||
&callback) override;
|
||||
virtual bool hasAvailableConnections() const noexcept override;
|
||||
|
||||
private:
|
||||
size_t connectionsNumber_;
|
||||
|
@ -72,7 +73,7 @@ class DbClientImpl : public DbClient,
|
|||
const DbConnectionPtr &conn,
|
||||
std::function<void(const std::shared_ptr<Transaction> &)> &&callback);
|
||||
|
||||
std::mutex connectionsMutex_;
|
||||
mutable std::mutex connectionsMutex_;
|
||||
std::unordered_set<DbConnectionPtr> connections_;
|
||||
std::unordered_set<DbConnectionPtr> readyConnections_;
|
||||
std::unordered_set<DbConnectionPtr> busyConnections_;
|
||||
|
|
|
@ -434,3 +434,8 @@ DbConnectionPtr DbClientLockFree::newConnection()
|
|||
// std::cout<<"newConn end"<<connPtr<<std::endl;
|
||||
return connPtr;
|
||||
}
|
||||
|
||||
bool DbClientLockFree::hasAvailableConnections() const noexcept
|
||||
{
|
||||
return !connections_.empty();
|
||||
}
|
|
@ -51,6 +51,7 @@ class DbClientLockFree : public DbClient,
|
|||
virtual void newTransactionAsync(
|
||||
const std::function<void(const std::shared_ptr<Transaction> &)>
|
||||
&callback) override;
|
||||
virtual bool hasAvailableConnections() const noexcept override;
|
||||
|
||||
private:
|
||||
std::string connectionInfo_;
|
||||
|
|
|
@ -155,3 +155,22 @@ void DbClientManager::createDbClient(const std::string &dbType,
|
|||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
bool DbClientManager::areAllDbClientsAvailable() const noexcept
|
||||
{
|
||||
for (auto const &pair : dbClientsMap_)
|
||||
{
|
||||
if (!(pair.second)->hasAvailableConnections())
|
||||
return false;
|
||||
}
|
||||
auto loop = trantor::EventLoop::getEventLoopOfCurrentThread();
|
||||
if (loop && loop->index() < app().getThreadNum())
|
||||
{
|
||||
for (auto const &pair : dbFastClientsMap_)
|
||||
{
|
||||
if (!(*(pair.second))->hasAvailableConnections())
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -38,6 +38,10 @@ class TransactionImpl : public Transaction,
|
|||
{
|
||||
commitCallback_ = commitCallback;
|
||||
}
|
||||
virtual bool hasAvailableConnections() const noexcept override
|
||||
{
|
||||
return connectionPtr_->status() == ConnectStatus::Ok;
|
||||
}
|
||||
|
||||
private:
|
||||
DbConnectionPtr connectionPtr_;
|
||||
|
|
|
@ -1937,13 +1937,25 @@ int main(int argc, char *argv[])
|
|||
#if USE_POSTGRESQL
|
||||
auto postgre_client = DbClient::newPgClient(
|
||||
"host=127.0.0.1 port=5432 dbname=postgres user=postgres", 1);
|
||||
while (!postgre_client->hasAvailableConnections())
|
||||
{
|
||||
std::this_thread::sleep_for(1s);
|
||||
}
|
||||
#endif
|
||||
#if USE_MYSQL
|
||||
auto mysql_client =
|
||||
DbClient::newMysqlClient("host=localhost port=3306 user=root", 1);
|
||||
while (!mysql_client->hasAvailableConnections())
|
||||
{
|
||||
std::this_thread::sleep_for(1s);
|
||||
}
|
||||
#endif
|
||||
#if USE_SQLITE3
|
||||
auto sqlite_client = DbClient::newSqlite3Client("filename=:memory:", 1);
|
||||
while (!sqlite_client->hasAvailableConnections())
|
||||
{
|
||||
std::this_thread::sleep_for(1s);
|
||||
}
|
||||
#endif
|
||||
LOG_DEBUG << "start!";
|
||||
std::this_thread::sleep_for(1s);
|
||||
|
|
Loading…
Reference in New Issue