From d6e5c1959cb44cb655e81049b3daf2a33a699956 Mon Sep 17 00:00:00 2001 From: An Tao Date: Thu, 3 Oct 2019 17:50:55 +0800 Subject: [PATCH] Modify some code of responses cache (#268) --- lib/src/DbClientManager.h | 4 ++-- lib/src/HttpControllersRouter.cc | 10 ++++----- lib/src/HttpControllersRouter.h | 2 +- lib/src/HttpSimpleControllersRouter.cc | 10 ++++----- lib/src/HttpSimpleControllersRouter.h | 2 +- orm_lib/src/DbClientManager.cc | 31 +++++++++++++------------- 6 files changed, 30 insertions(+), 29 deletions(-) diff --git a/lib/src/DbClientManager.h b/lib/src/DbClientManager.h index 5af81570..1b2d3181 100644 --- a/lib/src/DbClientManager.h +++ b/lib/src/DbClientManager.h @@ -40,7 +40,7 @@ class DbClientManager : public trantor::NonCopyable { auto iter = _dbFastClientsMap.find(name); assert(iter != _dbFastClientsMap.end()); - return iter->second->getThreadData(); + return iter->second.getThreadData(); } void createDbClient(const std::string &dbType, const std::string &host, @@ -64,7 +64,7 @@ class DbClientManager : public trantor::NonCopyable size_t _connectionNumber; }; std::vector _dbInfos; - std::map>> + std::map> _dbFastClientsMap; }; } // namespace orm diff --git a/lib/src/HttpControllersRouter.cc b/lib/src/HttpControllersRouter.cc index dc928c41..83f006f6 100644 --- a/lib/src/HttpControllersRouter.cc +++ b/lib/src/HttpControllersRouter.cc @@ -153,8 +153,8 @@ void HttpControllersRouter::addHttpPath( binderInfo->_parameterPlaces = std::move(places); binderInfo->_queryParametersPlaces = std::move(parametersPlaces); drogon::app().getLoop()->queueInLoop([binderInfo]() { - binderInfo->_responseCache = - std::make_shared>(); + // Recreate this with the correct number of threads. + binderInfo->_responseCache = IOThreadStorage(); }); { std::lock_guard guard(_ctrlMutex); @@ -340,7 +340,7 @@ void HttpControllersRouter::doControllerHandler( const HttpRequestImplPtr &req, std::function &&callback) { - auto &responsePtr = **(ctrlBinderPtr->_responseCache); + auto &responsePtr = *(ctrlBinderPtr->_responseCache); if (responsePtr) { if (responsePtr->expiredTime() == 0 || @@ -405,12 +405,12 @@ void HttpControllersRouter::doControllerHandler( auto loop = req->getLoop(); if (loop->isInLoopThread()) { - ctrlBinderPtr->_responseCache->setThreadData(resp); + ctrlBinderPtr->_responseCache.setThreadData(resp); } else { req->getLoop()->queueInLoop([resp, &ctrlBinderPtr]() { - ctrlBinderPtr->_responseCache->setThreadData(resp); + ctrlBinderPtr->_responseCache.setThreadData(resp); }); } } diff --git a/lib/src/HttpControllersRouter.h b/lib/src/HttpControllersRouter.h index d1c5c237..adb97e4f 100644 --- a/lib/src/HttpControllersRouter.h +++ b/lib/src/HttpControllersRouter.h @@ -79,7 +79,7 @@ class HttpControllersRouter : public trantor::NonCopyable std::vector> _filters; std::vector _parameterPlaces; std::map _queryParametersPlaces; - std::shared_ptr> _responseCache; + IOThreadStorage _responseCache; bool _isCORS = false; }; typedef std::shared_ptr CtrlBinderPtr; diff --git a/lib/src/HttpSimpleControllersRouter.cc b/lib/src/HttpSimpleControllersRouter.cc index 83a94ae4..bb025c5a 100644 --- a/lib/src/HttpSimpleControllersRouter.cc +++ b/lib/src/HttpSimpleControllersRouter.cc @@ -61,8 +61,8 @@ void HttpSimpleControllersRouter::registerHttpSimpleController( auto controller = std::dynamic_pointer_cast(_object); binder->_controller = controller; - binder->_responseCache = - std::make_shared>(); + // Recreate this with the correct number of threads. + binder->_responseCache = IOThreadStorage(); }); if (validMethods.size() > 0) @@ -198,7 +198,7 @@ void HttpSimpleControllersRouter::doControllerHandler( auto &controller = ctrlBinderPtr->_controller; if (controller) { - auto &responsePtr = **(ctrlBinderPtr->_responseCache); + auto &responsePtr = *(ctrlBinderPtr->_responseCache); if (responsePtr) { if (responsePtr->expiredTime() == 0 || @@ -230,12 +230,12 @@ void HttpSimpleControllersRouter::doControllerHandler( if (loop->isInLoopThread()) { - ctrlBinderPtr->_responseCache->setThreadData(resp); + ctrlBinderPtr->_responseCache.setThreadData(resp); } else { loop->queueInLoop([resp, &ctrlBinderPtr]() { - ctrlBinderPtr->_responseCache->setThreadData(resp); + ctrlBinderPtr->_responseCache.setThreadData(resp); }); } } diff --git a/lib/src/HttpSimpleControllersRouter.h b/lib/src/HttpSimpleControllersRouter.h index 4864db95..e192d65e 100644 --- a/lib/src/HttpSimpleControllersRouter.h +++ b/lib/src/HttpSimpleControllersRouter.h @@ -95,7 +95,7 @@ class HttpSimpleControllersRouter : public trantor::NonCopyable std::string _controllerName; std::vector _filterNames; std::vector> _filters; - std::shared_ptr> _responseCache; + IOThreadStorage _responseCache; bool _isCORS = false; }; diff --git a/orm_lib/src/DbClientManager.cc b/orm_lib/src/DbClientManager.cc index 979a9167..424afef9 100644 --- a/orm_lib/src/DbClientManager.cc +++ b/orm_lib/src/DbClientManager.cc @@ -39,21 +39,22 @@ void DbClientManager::createDbClients( if (dbInfo._dbType == drogon::orm::ClientType::PostgreSQL || dbInfo._dbType == drogon::orm::ClientType::Mysql) { - _dbFastClientsMap[dbInfo._name] = std::unique_ptr< - IOThreadStorage>( - new IOThreadStorage( - [&](size_t idx) -> std::shared_ptr { - assert(idx == ioloops[idx]->index()); - LOG_TRACE - << "create fast database client for the thread " - << idx; - return std::shared_ptr( - new drogon::orm::DbClientLockFree( - dbInfo._connectionInfo, - ioloops[idx], - dbInfo._dbType, - dbInfo._connectionNumber)); - })); + _dbFastClientsMap.insert( + {dbInfo._name, + IOThreadStorage< + orm::DbClient>([&](size_t idx) + -> std::shared_ptr { + assert(idx == ioloops[idx]->index()); + LOG_TRACE + << "create fast database client for the thread " + << idx; + return std::shared_ptr( + new drogon::orm::DbClientLockFree( + dbInfo._connectionInfo, + ioloops[idx], + dbInfo._dbType, + dbInfo._connectionNumber)); + })}); } } else