gydong commented on a change in pull request #694: Health check by rpc call
URL: https://github.com/apache/incubator-brpc/pull/694#discussion_r268414222
##########
File path: src/brpc/socket.cpp
##########
@@ -979,6 +995,89 @@ void HealthCheckTask::OnDestroyingTask() {
delete this;
}
+class HealthCheckChannel : public brpc::Channel {
+public:
+ HealthCheckChannel() {}
+ ~HealthCheckChannel() {}
+
+ int Init(SocketId id, const ChannelOptions* options);
+};
+
+int HealthCheckChannel::Init(SocketId id, const ChannelOptions* options) {
+ brpc::GlobalInitializeOrDie();
+ if (InitChannelOptions(options) != 0) {
+ return -1;
+ }
+ _server_id = id;
+ return 0;
+}
+
+class OnHealthCheckRPCDone : public google::protobuf::Closure {
+public:
+ void Run() {
+ std::unique_ptr<OnHealthCheckRPCDone> self_guard(this);
+ SocketUniquePtr ptr;
+ const int rc = Socket::AddressFailedAsWell(id, &ptr);
+ if (rc < 0) {
+ RPC_VLOG << "SocketId=" << id
+ << " was abandoned during health checking";
+ return;
+ }
+ if (!cntl.Failed()) {
+ ptr->ResetHealthCheckingUsingRPC();
+ return;
+ }
+
+ // Socket::SetFailed() will trigger next round of hc, just
+ // return here.
+ if (cntl.Failed() && ptr->Failed()) {
+ return;
+ }
+ // the left case is cntl.Failed() && !ptr->Failed(),
+ // in which we should retry hc rpc.
+ RPC_VLOG << "Fail to health check using rpc, error="
+ << cntl.ErrorText();
+ bthread_usleep(interval_s * 1000000);
+ cntl.Reset();
+ cntl.http_request().uri() = FLAGS_health_check_path;
+ cntl.set_health_check_call(true);
+ channel.CallMethod(NULL, &cntl, NULL, NULL, self_guard.release());
+ }
+
+ HealthCheckChannel channel;
+ brpc::Controller cntl;
+ SocketId id;
+ int64_t interval_s;
+};
+
+class HealthCheckManager {
+public:
+ static void StartCheck(SocketId id, int64_t check_interval_s) {
+ SocketUniquePtr ptr;
+ const int rc = Socket::AddressFailedAsWell(id, &ptr);
+ if (rc < 0) {
+ RPC_VLOG << "SocketId=" << id
+ << " was abandoned during health checking";
+ return;
+ }
+ OnHealthCheckRPCDone* done = new OnHealthCheckRPCDone;
+ done->id = id;
+ done->interval_s = check_interval_s;
+ brpc::ChannelOptions options;
+ options.protocol = "http";
Review comment:
这里要不要设置一下HTTP method 为 HEAD?以尽可能区别于业务请求。
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]