not retry when hook result code is int and !=0

This commit is contained in:
xiongguangjie 2023-04-17 20:01:45 +08:00
parent 5eee86ade3
commit f949c6de2a

View File

@ -94,15 +94,16 @@ static onceToken token([]() {
}//namespace Cluster }//namespace Cluster
static void parse_http_response(const SockException &ex, const Parser &res, static void parse_http_response(const SockException &ex, const Parser &res,
const function<void(const Value &,const string &)> &fun){ const function<void(const Value &,const string &,const bool &)> &fun){
bool should_retry = true;
if (ex) { if (ex) {
auto errStr = StrPrinter << "[network err]:" << ex.what() << endl; auto errStr = StrPrinter << "[network err]:" << ex.what() << endl;
fun(Json::nullValue, errStr); fun(Json::nullValue, errStr,should_retry);
return; return;
} }
if (res.Url() != "200") { if (res.Url() != "200") {
auto errStr = StrPrinter << "[bad http status code]:" << res.Url() << endl; auto errStr = StrPrinter << "[bad http status code]:" << res.Url() << endl;
fun(Json::nullValue, errStr); fun(Json::nullValue, errStr,should_retry);
return; return;
} }
Value result; Value result;
@ -111,20 +112,29 @@ static void parse_http_response(const SockException &ex, const Parser &res,
ss >> result; ss >> result;
} catch (std::exception &ex) { } catch (std::exception &ex) {
auto errStr = StrPrinter << "[parse json failed]:" << ex.what() << endl; auto errStr = StrPrinter << "[parse json failed]:" << ex.what() << endl;
fun(Json::nullValue, errStr); fun(Json::nullValue, errStr,should_retry);
return; return;
} }
if (result["code"].asInt() != 0) { auto code = result["code"];
auto errStr = StrPrinter << "[json code]:" << "code=" << result["code"] << ",msg=" << result["msg"] << endl;
fun(Json::nullValue, errStr); if (!code.isInt64()) {
auto errStr = StrPrinter << "[json code]:" << "code not int :"<<code<< endl;
fun(Json::nullValue, errStr,should_retry);
return; return;
} }
should_retry = false;
if(code.asInt64() != 0){
auto errStr = StrPrinter << "[auth failed]: code:" <<code<<" msg:"<<result["msg"]<<endl;
fun(Json::nullValue, errStr,should_retry);
return;
}
try { try {
fun(result, ""); fun(result, "",should_retry);
} catch (std::exception &ex) { } catch (std::exception &ex) {
auto errStr = StrPrinter << "[do hook invoker failed]:" << ex.what() << endl; auto errStr = StrPrinter << "[do hook invoker failed]:" << ex.what() << endl;
//如果还是抛异常,那么再上抛异常 //如果还是抛异常,那么再上抛异常
fun(Json::nullValue, errStr); fun(Json::nullValue, errStr,should_retry);
} }
} }
@ -173,12 +183,12 @@ void do_http_hook(const string &url, const ArgsType &body, const function<void(c
Ticker ticker; Ticker ticker;
requester->startRequester(url, [url, func, bodyStr, body, requester, ticker, retry](const SockException &ex, const Parser &res) mutable { requester->startRequester(url, [url, func, bodyStr, body, requester, ticker, retry](const SockException &ex, const Parser &res) mutable {
onceToken token(nullptr, [&]() mutable { requester.reset(); }); onceToken token(nullptr, [&]() mutable { requester.reset(); });
parse_http_response(ex, res, [&](const Value &obj, const string &err) { parse_http_response(ex, res, [&](const Value &obj, const string &err,const bool &should_retry) {
if (!err.empty()) { if (!err.empty()) {
// hook失败 // hook失败
WarnL << "hook " << url << " " << ticker.elapsedTime() << "ms,failed" << err << ":" << bodyStr; WarnL << "hook " << url << " " << ticker.elapsedTime() << "ms,failed" << err << ":" << bodyStr;
if (retry-- > 0) { if (retry-- > 0 && should_retry) {
requester->getPoller()->doDelayTask(MAX(retry_delay, 0.0) * 1000, [url, body, func, retry] { requester->getPoller()->doDelayTask(MAX(retry_delay, 0.0) * 1000, [url, body, func, retry] {
do_http_hook(url, body, func, retry); do_http_hook(url, body, func, retry);
return 0; return 0;