add mutex in HttpAppFrameworkImpl to ensure the multiple thread security

This commit is contained in:
an-tao 2018-06-11 13:39:15 +08:00
parent e0fbab7d0e
commit 9ddd79b194
1 changed files with 16 additions and 14 deletions

View File

@ -75,6 +75,7 @@ namespace drogon
std::string controllerName; std::string controllerName;
std::vector<std::string> filtersName; std::vector<std::string> filtersName;
std::shared_ptr<HttpSimpleControllerBase> controller; std::shared_ptr<HttpSimpleControllerBase> controller;
std::mutex _mutex;
}; };
std::unordered_map<std::string,ControllerAndFiltersName>_simpCtrlMap; std::unordered_map<std::string,ControllerAndFiltersName>_simpCtrlMap;
std::mutex _simpCtrlMutex; std::mutex _simpCtrlMutex;
@ -373,27 +374,29 @@ void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequest& req,const std::func
} }
/*find controller*/ /*find simple controller*/
std::string pathLower(req.path()); std::string pathLower(req.path());
std::transform(pathLower.begin(),pathLower.end(),pathLower.begin(),tolower); std::transform(pathLower.begin(),pathLower.end(),pathLower.begin(),tolower);
//fix me!need mutex;
if(_simpCtrlMap.find(pathLower)!=_simpCtrlMap.end()) if(_simpCtrlMap.find(pathLower)!=_simpCtrlMap.end())
{ {
auto &filters=_simpCtrlMap[pathLower].filtersName; auto &filters=_simpCtrlMap[pathLower].filtersName;
if(doFilters(filters,req,callback,needSetJsessionid,session_id)) if(doFilters(filters,req,callback,needSetJsessionid,session_id))
return; return;
auto controller=_simpCtrlMap[pathLower].controller; const std::string &ctrlName = _simpCtrlMap[pathLower].controllerName;
std::string ctrlName = _simpCtrlMap[pathLower].controllerName; std::shared_ptr<HttpSimpleControllerBase> controller;
{
//maybe update controller,so we use lock_guard to protect;
std::lock_guard<std::mutex> guard(_simpCtrlMap[pathLower]._mutex);
controller=_simpCtrlMap[pathLower].controller;
if(!controller) if(!controller)
{ {
auto _object = std::shared_ptr<DrObjectBase>(DrClassMap::newObject(ctrlName)); auto _object = std::shared_ptr<DrObjectBase>(DrClassMap::newObject(ctrlName));
controller = std::dynamic_pointer_cast<HttpSimpleControllerBase>(_object); controller = std::dynamic_pointer_cast<HttpSimpleControllerBase>(_object);
_simpCtrlMap[pathLower].controller=controller; _simpCtrlMap[pathLower].controller=controller;
} }
}
if(controller) { if(controller) {
controller->asyncHandleHttpRequest(req, [=](HttpResponse& resp){ controller->asyncHandleHttpRequest(req, [=](HttpResponse& resp){
@ -408,7 +411,6 @@ void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequest& req,const std::func
} }
} }
//find api controller //find api controller
//fix me!need mutex;
if(_apiRegex.mark_count()>0) if(_apiRegex.mark_count()>0)
{ {
std::smatch result; std::smatch result;