From 272eca1249955f05a88a8b815c124b03cc19efe7 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Sat, 18 Jun 2022 14:09:38 +0800 Subject: [PATCH] =?UTF-8?q?hook=E9=87=8D=E8=AF=95=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E5=BB=B6=E6=97=B6=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/config.ini | 6 +++-- server/WebHook.cpp | 64 ++++++++++++++++++++++++---------------------- 2 files changed, 37 insertions(+), 33 deletions(-) diff --git a/conf/config.ini b/conf/config.ini index 0067e0f9..e836a124 100644 --- a/conf/config.ini +++ b/conf/config.ini @@ -157,8 +157,10 @@ on_server_keepalive=https://127.0.0.1/index/hook/on_server_keepalive timeoutSec=10 #keepalive hook触发间隔,单位秒,float类型 alive_interval=10.0 -#hook通知重试次数 -retry=2 +#hook通知失败重试次数,正整数。为0不重试,1时重试一次,以此类推 +retry=1 +#hook通知失败重试延时,单位秒,float型 +retry_delay=3.0 [cluster] #设置源站拉流url模板, 格式跟printf类似,第一个%s指定app,第二个%s指定stream_id, diff --git a/server/WebHook.cpp b/server/WebHook.cpp index 5a5a2926..711ae4fb 100755 --- a/server/WebHook.cpp +++ b/server/WebHook.cpp @@ -1,4 +1,4 @@ -/* +/* * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved. * * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit). @@ -48,6 +48,7 @@ const string kOnServerKeepalive = HOOK_FIELD"on_server_keepalive"; const string kAdminParams = HOOK_FIELD"admin_params"; const string kAliveInterval = HOOK_FIELD"alive_interval"; const string kRetry = HOOK_FIELD"retry"; +const string kRetryDelay = HOOK_FIELD"retry_delay"; onceToken token([](){ mINI::Instance()[kEnable] = false; @@ -69,8 +70,8 @@ onceToken token([](){ mINI::Instance()[kOnServerKeepalive] = ""; mINI::Instance()[kAdminParams] = "secret=035c73f7-bb6b-4889-a715-d9eb2d1925cc"; mINI::Instance()[kAliveInterval] = 30.0; - mINI::Instance()[kRetry] = 0; - + mINI::Instance()[kRetry] = 1; + mINI::Instance()[kRetryDelay] = 3.0; },nullptr); }//namespace Hook @@ -88,8 +89,6 @@ static onceToken token([]() { }//namespace Cluster -void do_http_process(HttpRequester::Ptr &requester,const string &url, const string& bodyStr, const function &func,int retry,float timeout_sec); - static void parse_http_response(const SockException &ex, const Parser &res, const function &fun){ if (ex) { @@ -152,10 +151,10 @@ string getVhost(const HttpArgs &value) { return val != value.end() ? val->second : ""; } -void do_http_hook(const string &url,const ArgsType &body,const function &func){ +void do_http_hook(const string &url, const ArgsType &body, const function &func, uint32_t retry) { GET_CONFIG(string, mediaServerId, General::kMediaServerId); GET_CONFIG(float, hook_timeoutSec, Hook::kTimeoutSec); - GET_CONFIG(int, hook_retry, Hook::kRetry); + GET_CONFIG(float, retry_delay, Hook::kRetryDelay); const_cast(body)["mediaServerId"] = mediaServerId; HttpRequester::Ptr requester(new HttpRequester); @@ -167,35 +166,38 @@ void do_http_hook(const string &url,const ArgsType &body,const functionaddHeader("X-VHOST", vhost); } - - do_http_process(requester,url,bodyStr,func,hook_retry,hook_timeoutSec); -} + Ticker ticker; + requester->startRequester(url, [url, func, bodyStr, body, requester, ticker, retry](const SockException &ex, const Parser &res) mutable { + onceToken token(nullptr, [&]() mutable { requester.reset(); }); + parse_http_response(ex, res, [&](const Value &obj, const string &err) { + if (!err.empty()) { + // hook失败 + WarnL << "hook " << url << " " << ticker.elapsedTime() << "ms,failed" << err << ":" << bodyStr; + + if (retry-- > 0) { + requester->getPoller()->doDelayTask(MAX(retry_delay, 0.0) * 1000, [url, body, func, retry] { + do_http_hook(url, body, func, retry); + return 0; + }); + //重试不需要触发回调 + return; + } + + } else if (ticker.elapsedTime() > 500) { + //hook成功,但是hook响应超过500ms,打印警告日志 + DebugL << "hook " << url << " " << ticker.elapsedTime() << "ms,success:" << bodyStr; + } -void do_http_process(HttpRequester::Ptr &requester,const string &url, const string& bodyStr, const function &func,int retry,float timeout_sec){ - - std::shared_ptr pTicker(new Ticker); - requester->startRequester(url, [url, func, bodyStr, requester, pTicker,retry,timeout_sec](const SockException &ex, - const Parser &res) mutable{ - onceToken token(nullptr, [&]() mutable{ - requester.reset(); - }); - parse_http_response(ex, res, [&](const Value &obj, const string &err) { if (func) { func(obj, err); } - if (!err.empty()) { - WarnL << "hook " << url << " " << pTicker->elapsedTime() << "ms,failed" << err << ":" << bodyStr; - } else if (pTicker->elapsedTime() > 500) { - DebugL << "hook " << url << " " << pTicker->elapsedTime() << "ms,success:" << bodyStr; - } - //尾部递归重试 - if (!err.empty() && retry-- > 0) { - //WarnL << "----------------hook retry------------------ " << retry ; - HttpRequester::Ptr requester(new HttpRequester); - do_http_process(requester,url,bodyStr,func,retry,timeout_sec); - } }); - }, timeout_sec); + }, hook_timeoutSec); +} + +void do_http_hook(const string &url, const ArgsType &body, const function &func) { + GET_CONFIG(uint32_t, hook_retry, Hook::kRetry); + do_http_hook(url, body, func, hook_retry); } static ArgsType make_json(const MediaInfo &args){