Skip to content

Commit 96f5d9a

Browse files
author
zhoukangsheng
committed
feat: add health_check_option.h
1 parent 84071ed commit 96f5d9a

5 files changed

Lines changed: 47 additions & 23 deletions

File tree

src/brpc/channel.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#include "brpc/retry_policy.h"
3737
#include "brpc/backup_request_policy.h"
3838
#include "brpc/naming_service_filter.h"
39-
#include "brpc/socket.h"
39+
#include "brpc/health_check_option.h"
4040

4141
namespace brpc {
4242

src/brpc/details/health_check.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ class OnAppHealthCheckDone : public google::protobuf::Closure {
5757
SocketId id;
5858
int64_t interval_s;
5959
int64_t last_check_time_ms;
60-
int32_t health_check_timeout_ms;
61-
std::string health_check_path;
60+
HealthCheckOption hc_option;
6261
};
6362

6463
class HealthCheckManager {
@@ -79,13 +78,12 @@ void HealthCheckManager::StartCheck(SocketId id, int64_t check_interval_s) {
7978
OnAppHealthCheckDone* done = new OnAppHealthCheckDone;
8079
done->id = id;
8180
done->interval_s = check_interval_s;
82-
done->health_check_timeout_ms = ptr->health_check_timeout_ms();
83-
done->health_check_path = ptr->health_check_path();
81+
done->hc_option = ptr->_hc_option;
8482
brpc::ChannelOptions options;
8583
options.protocol = PROTOCOL_HTTP;
8684
options.max_retry = 0;
8785
options.timeout_ms =
88-
std::min((int64_t)(done->health_check_timeout_ms), check_interval_s * 1000);
86+
std::min((int64_t)(done->hc_option.health_check_timeout_ms), check_interval_s * 1000);
8987
if (done->channel.Init(id, &options) != 0) {
9088
LOG(WARNING) << "Fail to init health check channel to SocketId=" << id;
9189
ptr->_ninflight_app_health_check.fetch_sub(
@@ -99,7 +97,7 @@ void HealthCheckManager::StartCheck(SocketId id, int64_t check_interval_s) {
9997
void* HealthCheckManager::AppCheck(void* arg) {
10098
OnAppHealthCheckDone* done = static_cast<OnAppHealthCheckDone*>(arg);
10199
done->cntl.Reset();
102-
done->cntl.http_request().uri() = done->health_check_path;
100+
done->cntl.http_request().uri() = done->hc_option.health_check_path;
103101
ControllerPrivateAccessor(&done->cntl).set_health_check_call();
104102
done->last_check_time_ms = butil::gettimeofday_ms();
105103
done->channel.CallMethod(NULL, &done->cntl, NULL, NULL, done);
@@ -117,14 +115,14 @@ void OnAppHealthCheckDone::Run() {
117115
}
118116
if (!cntl.Failed() || ptr->Failed()) {
119117
LOG_IF(INFO, !cntl.Failed()) << "Succeeded to call "
120-
<< ptr->remote_side() << health_check_path;
118+
<< ptr->remote_side() << hc_option.health_check_path;
121119
// if ptr->Failed(), previous SetFailed would trigger next round
122120
// of hc, just return here.
123121
ptr->_ninflight_app_health_check.fetch_sub(
124122
1, butil::memory_order_relaxed);
125123
return;
126124
}
127-
RPC_VLOG << "Fail to check path=" << health_check_path
125+
RPC_VLOG << "Fail to check path=" << hc_option.health_check_path
128126
<< ", " << cntl.ErrorText();
129127

130128
int64_t sleep_time_ms =

src/brpc/health_check_option.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
#ifndef BRPC_HEALTH_CHECK_OPTION_H
19+
#define BRPC_HEALTH_CHECK_OPTION_H
20+
21+
#include <string>
22+
23+
namespace brpc {
24+
25+
struct HealthCheckOption {
26+
// Http path of health check call
27+
std::string health_check_path;
28+
// The timeout for both establishing the connection and the http call to health_check_path over the connection
29+
int32_t health_check_timeout_ms{500};
30+
};
31+
32+
} // namespace brpc
33+
34+
#endif // BRPC_HEALTH_CHECK_OPTION_H

src/brpc/socket.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -742,8 +742,7 @@ int Socket::OnCreated(const SocketOptions& options) {
742742
reset_parsing_context(options.initial_parsing_context);
743743
_correlation_id = 0;
744744
_health_check_interval_s = options.health_check_interval_s;
745-
_health_check_path = options.hc_option.health_check_path;
746-
_health_check_timeout_ms = options.hc_option.health_check_timeout_ms;
745+
_hc_option = options.hc_option;
747746
_is_hc_related_ref_held = false;
748747
_hc_started.store(false, butil::memory_order_relaxed);
749748
_ninprocess.store(1, butil::memory_order_relaxed);
@@ -2594,7 +2593,7 @@ int Socket::CheckHealth() {
25942593
LOG(INFO) << "Checking " << *this;
25952594
}
25962595
const timespec duetime =
2597-
butil::milliseconds_from_now(_health_check_timeout_ms);
2596+
butil::milliseconds_from_now(_hc_option.health_check_timeout_ms);
25982597
const int connected_fd = Connect(&duetime, NULL, NULL);
25992598
if (connected_fd >= 0) {
26002599
::close(connected_fd);

src/brpc/socket.h

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "brpc/http_method.h"
4242
#include "brpc/event_dispatcher.h"
4343
#include "brpc/versioned_ref_with_id.h"
44+
#include "brpc/health_check_option.h"
4445

4546
namespace brpc {
4647
namespace policy {
@@ -242,13 +243,6 @@ struct SocketKeepaliveOptions {
242243
int keepalive_count{-1};
243244
};
244245

245-
struct HealthCheckOption {
246-
// Http path of health check call
247-
std::string health_check_path;
248-
// The timeout for both establishing the connection and the http call to health_check_path over the connection
249-
int32_t health_check_timeout_ms{500};
250-
};
251-
252246
// TODO: Comment fields
253247
struct SocketOptions {
254248
// If `fd' is non-negative, set `fd' to be non-blocking and take the
@@ -420,9 +414,9 @@ friend void DereferenceSocket(Socket*);
420414
// Initialized by SocketOptions.health_check_interval_s.
421415
int health_check_interval() const { return _health_check_interval_s; }
422416

423-
const std::string& health_check_path() const { return _health_check_path; }
417+
const std::string& health_check_path() const { return _hc_option.health_check_path; }
424418

425-
int32_t health_check_timeout_ms() const {return _health_check_timeout_ms; }
419+
int32_t health_check_timeout_ms() const {return _hc_option.health_check_timeout_ms; }
426420

427421
// True if health checking is enabled.
428422
bool HCEnabled() const {
@@ -992,8 +986,7 @@ friend void DereferenceSocket(Socket*);
992986
int _tcp_user_timeout_ms;
993987

994988
HttpMethod _http_request_method;
995-
std::string _health_check_path;
996-
int32_t _health_check_timeout_ms{500};
989+
HealthCheckOption _hc_option;
997990
};
998991

999992
} // namespace brpc

0 commit comments

Comments
 (0)