删除过多的线程切换

This commit is contained in:
xiongziliang 2019-05-20 17:34:39 +08:00
parent 7539042d2b
commit f9f487bf61
2 changed files with 70 additions and 98 deletions

View File

@ -128,15 +128,12 @@ static inline void addHttpListener(){
} }
//该api已被消费 //该api已被消费
consumed = true; consumed = true;
AsyncHttpApi api = it->second;
//异步执行该api防止阻塞NoticeCenter
EventPollerPool::Instance().getExecutor()->async([api, parser, invoker]() {
//执行API //执行API
Json::Value val; Json::Value val;
val["code"] = API::Success; val["code"] = API::Success;
HttpSession::KeyValue &headerIn = parser.getValues();
HttpSession::KeyValue headerOut; HttpSession::KeyValue headerOut;
auto allArgs = getAllArgs(parser); auto allArgs = getAllArgs(parser);
HttpSession::KeyValue &headerIn = parser.getValues();
headerOut["Content-Type"] = "application/json; charset=utf-8"; headerOut["Content-Type"] = "application/json; charset=utf-8";
if(api_debug){ if(api_debug){
auto newInvoker = [invoker,parser,allArgs](const string &codeOut, auto newInvoker = [invoker,parser,allArgs](const string &codeOut,
@ -159,7 +156,7 @@ static inline void addHttpListener(){
} }
try { try {
api(headerIn, headerOut, allArgs, val, invoker); it->second(headerIn, headerOut, allArgs, val, invoker);
} catch(ApiRetException &ex){ } catch(ApiRetException &ex){
val["code"] = ex.code(); val["code"] = ex.code();
val["msg"] = ex.what(); val["msg"] = ex.what();
@ -175,7 +172,6 @@ static inline void addHttpListener(){
invoker("200 OK", headerOut, val.toStyledString()); invoker("200 OK", headerOut, val.toStyledString());
} }
}); });
});
} }
template <typename Args,typename First> template <typename Args,typename First>

View File

@ -168,13 +168,11 @@ void installWebHook(){
body["ip"] = sender.get_peer_ip(); body["ip"] = sender.get_peer_ip();
body["port"] = sender.get_peer_port(); body["port"] = sender.get_peer_port();
body["id"] = sender.getIdentifier(); body["id"] = sender.getIdentifier();
EventPollerPool::Instance().getExecutor()->async([body,invoker](){
//执行hook //执行hook
do_http_hook(hook_publish,body,[invoker](const Value &obj,const string &err){ do_http_hook(hook_publish,body,[invoker](const Value &obj,const string &err){
invoker(err); invoker(err);
}); });
}); });
});
NoticeCenter::Instance().addListener(nullptr,Broadcast::kBroadcastMediaPlayed,[](BroadcastMediaPlayedArgs){ NoticeCenter::Instance().addListener(nullptr,Broadcast::kBroadcastMediaPlayed,[](BroadcastMediaPlayedArgs){
if(!hook_enable || args._param_strs == hook_adminparams || hook_play.empty()){ if(!hook_enable || args._param_strs == hook_adminparams || hook_play.empty()){
@ -185,14 +183,11 @@ void installWebHook(){
body["ip"] = sender.get_peer_ip(); body["ip"] = sender.get_peer_ip();
body["port"] = sender.get_peer_port(); body["port"] = sender.get_peer_port();
body["id"] = sender.getIdentifier(); body["id"] = sender.getIdentifier();
//异步执行该hook api防止阻塞NoticeCenter
EventPollerPool::Instance().getExecutor()->async([body,invoker](){
//执行hook //执行hook
do_http_hook(hook_play,body,[invoker](const Value &obj,const string &err){ do_http_hook(hook_play,body,[invoker](const Value &obj,const string &err){
invoker(err); invoker(err);
}); });
}); });
});
NoticeCenter::Instance().addListener(nullptr,Broadcast::kBroadcastFlowReport,[](BroadcastFlowReportArgs){ NoticeCenter::Instance().addListener(nullptr,Broadcast::kBroadcastFlowReport,[](BroadcastFlowReportArgs){
if(!hook_enable || args._param_strs == hook_adminparams || hook_flowreport.empty()){ if(!hook_enable || args._param_strs == hook_adminparams || hook_flowreport.empty()){
@ -204,13 +199,9 @@ void installWebHook(){
body["id"] = sender.getIdentifier(); body["id"] = sender.getIdentifier();
body["totalBytes"] = (Json::UInt64)totalBytes; body["totalBytes"] = (Json::UInt64)totalBytes;
body["duration"] = (Json::UInt64)totalDuration; body["duration"] = (Json::UInt64)totalDuration;
//流量统计事件
EventPollerPool::Instance().getExecutor()->async([body,totalBytes](){
//执行hook //执行hook
do_http_hook(hook_flowreport,body, nullptr); do_http_hook(hook_flowreport,body, nullptr);
}); });
});
static const string unAuthedRealm = "unAuthedRealm"; static const string unAuthedRealm = "unAuthedRealm";
@ -226,8 +217,6 @@ void installWebHook(){
body["ip"] = sender.get_peer_ip(); body["ip"] = sender.get_peer_ip();
body["port"] = sender.get_peer_port(); body["port"] = sender.get_peer_port();
body["id"] = sender.getIdentifier(); body["id"] = sender.getIdentifier();
EventPollerPool::Instance().getExecutor()->async([body,invoker](){
//执行hook //执行hook
do_http_hook(hook_rtsp_realm,body, [invoker](const Value &obj,const string &err){ do_http_hook(hook_rtsp_realm,body, [invoker](const Value &obj,const string &err){
if(!err.empty()){ if(!err.empty()){
@ -238,7 +227,6 @@ void installWebHook(){
invoker(obj["realm"].asString()); invoker(obj["realm"].asString());
}); });
}); });
});
//监听kBroadcastOnRtspAuth事件返回正确的rtsp鉴权用户密码 //监听kBroadcastOnRtspAuth事件返回正确的rtsp鉴权用户密码
NoticeCenter::Instance().addListener(nullptr,Broadcast::kBroadcastOnRtspAuth,[](BroadcastOnRtspAuthArgs){ NoticeCenter::Instance().addListener(nullptr,Broadcast::kBroadcastOnRtspAuth,[](BroadcastOnRtspAuthArgs){
@ -254,8 +242,6 @@ void installWebHook(){
body["user_name"] = user_name; body["user_name"] = user_name;
body["must_no_encrypt"] = must_no_encrypt; body["must_no_encrypt"] = must_no_encrypt;
body["realm"] = realm; body["realm"] = realm;
EventPollerPool::Instance().getExecutor()->async([body,invoker](){
//执行hook //执行hook
do_http_hook(hook_rtsp_auth,body, [invoker](const Value &obj,const string &err){ do_http_hook(hook_rtsp_auth,body, [invoker](const Value &obj,const string &err){
if(!err.empty()){ if(!err.empty()){
@ -266,7 +252,6 @@ void installWebHook(){
invoker(obj["encrypted"].asBool(),obj["passwd"].asString()); invoker(obj["encrypted"].asBool(),obj["passwd"].asString());
}); });
}); });
});
//监听rtsp、rtmp源注册或注销事件 //监听rtsp、rtmp源注册或注销事件
@ -280,12 +265,9 @@ void installWebHook(){
body["vhost"] = vhost; body["vhost"] = vhost;
body["app"] = app; body["app"] = app;
body["stream"] = stream; body["stream"] = stream;
EventPollerPool::Instance().getExecutor()->async([body](){
//执行hook //执行hook
do_http_hook(hook_stream_chaned,body, nullptr); do_http_hook(hook_stream_chaned,body, nullptr);
}); });
});
//监听播放失败(未找到特定的流)事件 //监听播放失败(未找到特定的流)事件
NoticeCenter::Instance().addListener(nullptr,Broadcast::kBroadcastNotFoundStream,[](BroadcastNotFoundStreamArgs){ NoticeCenter::Instance().addListener(nullptr,Broadcast::kBroadcastNotFoundStream,[](BroadcastNotFoundStreamArgs){
@ -296,12 +278,9 @@ void installWebHook(){
body["ip"] = sender.get_peer_ip(); body["ip"] = sender.get_peer_ip();
body["port"] = sender.get_peer_port(); body["port"] = sender.get_peer_port();
body["id"] = sender.getIdentifier(); body["id"] = sender.getIdentifier();
EventPollerPool::Instance().getExecutor()->async([body](){
//执行hook //执行hook
do_http_hook(hook_stream_not_found,body, nullptr); do_http_hook(hook_stream_not_found,body, nullptr);
}); });
});
#ifdef ENABLE_MP4V2 #ifdef ENABLE_MP4V2
//录制mp4文件成功后广播 //录制mp4文件成功后广播
@ -320,12 +299,9 @@ void installWebHook(){
body["app"] = info.strAppName; body["app"] = info.strAppName;
body["stream"] = info.strStreamId; body["stream"] = info.strStreamId;
body["vhost"] = info.strVhost; body["vhost"] = info.strVhost;
EventPollerPool::Instance().getExecutor()->async([body](){
//执行hook //执行hook
do_http_hook(hook_record_mp4,body, nullptr); do_http_hook(hook_record_mp4,body, nullptr);
}); });
});
#endif //ENABLE_MP4V2 #endif //ENABLE_MP4V2
} }