From 7f21f45a1b4d165b07d61bb0dee2f6e5bdfeedb8 Mon Sep 17 00:00:00 2001 From: antao Date: Wed, 20 Feb 2019 13:23:50 +0800 Subject: [PATCH] Add the fast db client enablation option --- config.example.json | 4 +++- drogon_ctl/templates/config.csp | 4 +++- lib/inc/drogon/HttpAppFramework.h | 3 ++- lib/src/ConfigLoader.cc | 10 +++++++++- lib/src/HttpAppFrameworkImpl.cc | 25 ++++++++++++++++--------- lib/src/HttpAppFrameworkImpl.h | 5 +++++ 6 files changed, 38 insertions(+), 13 deletions(-) diff --git a/config.example.json b/config.example.json index a995f018..dabe35e4 100644 --- a/config.example.json +++ b/config.example.json @@ -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 } } diff --git a/drogon_ctl/templates/config.csp b/drogon_ctl/templates/config.csp index a995f018..dabe35e4 100644 --- a/drogon_ctl/templates/config.csp +++ b/drogon_ctl/templates/config.csp @@ -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 } } diff --git a/lib/inc/drogon/HttpAppFramework.h b/lib/inc/drogon/HttpAppFramework.h index 9319eacc..ac1cc9a8 100755 --- a/lib/inc/drogon/HttpAppFramework.h +++ b/lib/inc/drogon/HttpAppFramework.h @@ -17,7 +17,7 @@ #include #if USE_ORM #include -#define USE_FAST_CLIENT 0 +#define USE_FAST_CLIENT 1 #endif #include #include @@ -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, diff --git a/lib/src/ConfigLoader.cc b/lib/src/ConfigLoader.cc index f380e693..cab78222 100644 --- a/lib/src/ConfigLoader.cc +++ b/lib/src/ConfigLoader.cc @@ -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(); diff --git a/lib/src/HttpAppFrameworkImpl.cc b/lib/src/HttpAppFrameworkImpl.cc index 8902a84b..1cc85134 100755 --- a/lib/src/HttpAppFrameworkImpl.cc +++ b/lib/src/HttpAppFrameworkImpl.cc @@ -283,15 +283,17 @@ void HttpAppFrameworkImpl::run() std::vector> loopThreads; std::vector 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("DrogonIoLoop"); + loopThreadPtr->run(); + loopThreads.push_back(loopThreadPtr); + ioLoops.push_back(loopThreadPtr->getLoop()); + for (auto const &listener : _listeners) { - auto loopThreadPtr = std::make_shared("DrogonIoLoop"); - loopThreadPtr->run(); - loopThreads.push_back(loopThreadPtr); auto serverPtr = std::make_shared(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("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); diff --git a/lib/src/HttpAppFrameworkImpl.h b/lib/src/HttpAppFrameworkImpl.h index 654cb4ca..f79a365c 100644 --- a/lib/src/HttpAppFrameworkImpl.h +++ b/lib/src/HttpAppFrameworkImpl.h @@ -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> _staticFilesCache; std::mutex _staticFilesCacheMutex;