Add the fast db client enablation option

This commit is contained in:
antao 2019-02-20 13:23:50 +08:00
parent 84ab5d369e
commit 7f21f45a1b
6 changed files with 38 additions and 13 deletions

View File

@ -133,6 +133,8 @@
],
//idle_connection_timeout: Defaults to 60 seconds, the lifetime
//of the connection without read or write
"idle_connection_timeout": 60
"idle_connection_timeout": 60,
//enable_fast_db_client: Defaults to false
"enable_fast_db_client": false
}
}

View File

@ -133,6 +133,8 @@
],
//idle_connection_timeout: Defaults to 60 seconds, the lifetime
//of the connection without read or write
"idle_connection_timeout": 60
"idle_connection_timeout": 60,
//enable_fast_db_client: Defaults to false
"enable_fast_db_client": false
}
}

View File

@ -17,7 +17,7 @@
#include <drogon/config.h>
#if USE_ORM
#include <drogon/orm/DbClient.h>
#define USE_FAST_CLIENT 0
#define USE_FAST_CLIENT 1
#endif
#include <drogon/utils/Utilities.h>
#include <drogon/HttpBinder.h>
@ -187,6 +187,7 @@ class HttpAppFramework : public trantor::NonCopyable
#if USE_ORM
virtual orm::DbClientPtr getDbClient(const std::string &name = "default") = 0;
#if USE_FAST_CLIENT
virtual void enableFastDbClient() = 0;
virtual orm::DbClientPtr getFastDbClient(const std::string &name = "default") = 0;
#endif
virtual void createDbClient(const std::string &dbType,

View File

@ -228,12 +228,20 @@ static void loadApp(const Json::Value &app)
//Kick off idle connections
auto kickOffTimeout = app.get("idle_connection_timeout", 60).asUInt64();
drogon::app().setIdleConnectionTimeout(kickOffTimeout);
#if USE_ORM
#if USE_FAST_CLIENT
//Fast db client
auto fastDbClient = app.get("enable_fast_db_client", false).asBool();
if (fastDbClient)
drogon::app().enableFastDbClient();
#endif
#endif
}
static void loadDbClients(const Json::Value &dbClients)
{
if (!dbClients)
return;
#if USE_ORM
#if USE_ORM
for (auto const &client : dbClients)
{
auto type = client.get("rdbms", "postgresql").asString();

View File

@ -283,15 +283,17 @@ void HttpAppFrameworkImpl::run()
std::vector<std::shared_ptr<EventLoopThread>> loopThreads;
std::vector<trantor::EventLoop *> ioLoops;
for (auto const &listener : _listeners)
#ifdef __linux__
for (size_t i = 0; i < _threadNum; i++)
{
LOG_TRACE << "thread num=" << _threadNum;
#ifdef __linux__
for (size_t i = 0; i < _threadNum; i++)
auto loopThreadPtr = std::make_shared<EventLoopThread>("DrogonIoLoop");
loopThreadPtr->run();
loopThreads.push_back(loopThreadPtr);
ioLoops.push_back(loopThreadPtr->getLoop());
for (auto const &listener : _listeners)
{
auto loopThreadPtr = std::make_shared<EventLoopThread>("DrogonIoLoop");
loopThreadPtr->run();
loopThreads.push_back(loopThreadPtr);
auto serverPtr = std::make_shared<HttpServer>(loopThreadPtr->getLoop(),
InetAddress(std::get<0>(listener), std::get<1>(listener)), "drogon");
if (std::get<2>(listener))
@ -317,9 +319,12 @@ void HttpAppFrameworkImpl::run()
serverPtr->kickoffIdleConnections(_idleConnectionTimeout);
serverPtr->start();
servers.push_back(serverPtr);
ioLoops.push_back(serverPtr->getLoop());
}
}
#else
for (auto const &listener : _listeners)
{
LOG_TRACE << "thread num=" << _threadNum;
auto loopThreadPtr = std::make_shared<EventLoopThread>("DrogonListeningLoop");
loopThreadPtr->run();
loopThreads.push_back(loopThreadPtr);
@ -364,12 +369,14 @@ void HttpAppFrameworkImpl::run()
ioLoops.push_back(serverIoLoop);
}
servers.push_back(serverPtr);
#endif
}
#endif
#if USE_ORM
#if USE_FAST_CLIENT
// Create fast db clients for every io loop
createFastDbClient(ioLoops);
if (_enableFastDbClient)
createFastDbClient(ioLoops);
#endif
#endif
_httpCtrlsRouter.init(ioLoops);

View File

@ -109,6 +109,10 @@ class HttpAppFrameworkImpl : public HttpAppFramework
#if USE_ORM
virtual orm::DbClientPtr getDbClient(const std::string &name = "default") override;
#if USE_FAST_CLIENT
virtual void enableFastDbClient() override
{
_enableFastDbClient = true;
}
virtual orm::DbClientPtr getFastDbClient(const std::string &name = "default") override;
#endif
virtual void createDbClient(const std::string &dbType,
@ -190,6 +194,7 @@ class HttpAppFrameworkImpl : public HttpAppFramework
size_t _logfileSize = 100000000;
bool _useSendfile = true;
bool _useGzip = true;
bool _enableFastDbClient = false;
int _staticFilesCacheTime = 5;
std::unordered_map<std::string, std::weak_ptr<HttpResponse>> _staticFilesCache;
std::mutex _staticFilesCacheMutex;