Commit 938f2e85 authored by zhujiashun's avatar zhujiashun

health_check_using_rpc: refine code

parent 413db42c
......@@ -68,7 +68,12 @@ public:
class HealthCheckManager {
public:
static void StartCheck(SocketId id, int64_t check_interval_s) {
static void StartCheck(SocketId id, int64_t check_interval_s);
static void* AppCheck(void* arg);
static void RunAppCheck(void* arg);
};
void HealthCheckManager::StartCheck(SocketId id, int64_t check_interval_s) {
SocketUniquePtr ptr;
const int rc = Socket::AddressFailedAsWell(id, &ptr);
if (rc < 0) {
......@@ -93,9 +98,9 @@ public:
return;
}
AppCheck(done);
}
}
static void* AppCheck(void* arg) {
void* HealthCheckManager::AppCheck(void* arg) {
OnAppHealthCheckDone* done = static_cast<OnAppHealthCheckDone*>(arg);
done->cntl.Reset();
done->cntl.http_request().uri() = FLAGS_health_check_path;
......@@ -103,9 +108,9 @@ public:
done->last_check_time_ms = butil::gettimeofday_ms();
done->channel.CallMethod(NULL, &done->cntl, NULL, NULL, done);
return NULL;
}
}
static void RunAppCheck(void* arg) {
void HealthCheckManager::RunAppCheck(void* arg) {
bthread_t th = 0;
int rc = bthread_start_background(
&th, &BTHREAD_ATTR_NORMAL, AppCheck, arg);
......@@ -114,8 +119,7 @@ public:
AppCheck(arg);
return;
}
}
};
}
void OnAppHealthCheckDone::Run() {
std::unique_ptr<OnAppHealthCheckDone> self_guard(this);
......@@ -129,6 +133,8 @@ void OnAppHealthCheckDone::Run() {
if (!cntl.Failed() || ptr->Failed()) {
LOG_IF(INFO, !cntl.Failed()) << "Succeeded to call "
<< ptr->remote_side() << FLAGS_health_check_path;
// if ptr->Failed(), previous SetFailed would trigger next round
// of hc, just return here.
ptr->_ninflight_app_health_check.fetch_sub(
1, butil::memory_order_relaxed);
return;
......@@ -145,6 +151,9 @@ void OnAppHealthCheckDone::Run() {
&timer_id, abstime, HealthCheckManager::RunAppCheck, this);
if (rc != 0) {
LOG(ERROR) << "Fail to add timer for RunAppCheck";
// TODO(zhujiashun): we need to handle the case when timer fails.
// In most situations, the possibility of this case is quite small,
// so currently we just keep sending the hc call.
HealthCheckManager::AppCheck(this);
return;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment