Modify some code of responses cache (#268)

This commit is contained in:
An Tao 2019-10-03 17:50:55 +08:00 committed by GitHub
parent 323bf0693a
commit d6e5c1959c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 30 additions and 29 deletions

View File

@ -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<DbInfo> _dbInfos;
std::map<std::string, std::unique_ptr<IOThreadStorage<orm::DbClient>>>
std::map<std::string, IOThreadStorage<orm::DbClient>>
_dbFastClientsMap;
};
} // namespace orm

View File

@ -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<IOThreadStorage<HttpResponse, false>>();
// Recreate this with the correct number of threads.
binderInfo->_responseCache = IOThreadStorage<HttpResponse, false>();
});
{
std::lock_guard<std::mutex> guard(_ctrlMutex);
@ -340,7 +340,7 @@ void HttpControllersRouter::doControllerHandler(
const HttpRequestImplPtr &req,
std::function<void(const HttpResponsePtr &)> &&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);
});
}
}

View File

@ -79,7 +79,7 @@ class HttpControllersRouter : public trantor::NonCopyable
std::vector<std::shared_ptr<HttpFilterBase>> _filters;
std::vector<size_t> _parameterPlaces;
std::map<std::string, size_t> _queryParametersPlaces;
std::shared_ptr<IOThreadStorage<HttpResponse, false>> _responseCache;
IOThreadStorage<HttpResponse, false> _responseCache;
bool _isCORS = false;
};
typedef std::shared_ptr<CtrlBinder> CtrlBinderPtr;

View File

@ -61,8 +61,8 @@ void HttpSimpleControllersRouter::registerHttpSimpleController(
auto controller =
std::dynamic_pointer_cast<HttpSimpleControllerBase>(_object);
binder->_controller = controller;
binder->_responseCache =
std::make_shared<IOThreadStorage<HttpResponse, false>>();
// Recreate this with the correct number of threads.
binder->_responseCache = IOThreadStorage<HttpResponse, false>();
});
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);
});
}
}

View File

@ -95,7 +95,7 @@ class HttpSimpleControllersRouter : public trantor::NonCopyable
std::string _controllerName;
std::vector<std::string> _filterNames;
std::vector<std::shared_ptr<HttpFilterBase>> _filters;
std::shared_ptr<IOThreadStorage<HttpResponse, false>> _responseCache;
IOThreadStorage<HttpResponse, false> _responseCache;
bool _isCORS = false;
};

View File

@ -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<orm::DbClient>>(
new IOThreadStorage<orm::DbClient>(
[&](size_t idx) -> std::shared_ptr<orm::DbClient> {
assert(idx == ioloops[idx]->index());
LOG_TRACE
<< "create fast database client for the thread "
<< idx;
return std::shared_ptr<orm::DbClient>(
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<orm::DbClient> {
assert(idx == ioloops[idx]->index());
LOG_TRACE
<< "create fast database client for the thread "
<< idx;
return std::shared_ptr<orm::DbClient>(
new drogon::orm::DbClientLockFree(
dbInfo._connectionInfo,
ioloops[idx],
dbInfo._dbType,
dbInfo._connectionNumber));
})});
}
}
else