Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/brpc/channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ ChannelSSLOptions* ChannelOptions::mutable_ssl_options() {
static ChannelSignature ComputeChannelSignature(const ChannelOptions& opt) {
if (opt.auth == NULL &&
!opt.has_ssl_options() &&
opt.client_host.empty() &&
opt.connection_group.empty() &&
opt.hc_option.health_check_path.empty()) {
// Returning zeroized result by default is more intuitive for users.
Expand All @@ -94,6 +95,10 @@ static ChannelSignature ComputeChannelSignature(const ChannelOptions& opt) {
buf.append("|conng=");
buf.append(opt.connection_group);
}
if (!opt.client_host.empty()) {
buf.append("|clih=");
buf.append(opt.client_host);
}
if (opt.auth) {
buf.append("|auth=");
buf.append((char*)&opt.auth, sizeof(opt.auth));
Expand Down Expand Up @@ -362,14 +367,22 @@ int Channel::InitSingle(const butil::EndPoint& server_addr_and_port,
LOG(ERROR) << "Invalid port=" << port;
return -1;
}
butil::EndPoint client_end_point;
if (!_options.client_host.empty() &&
butil::str2ip(_options.client_host.c_str(), &client_end_point.ip) != 0 &&
butil::hostname2ip(_options.client_host.c_str(), &client_end_point.ip) != 0) {
LOG(ERROR) << "Invalid client host=`" << _options.client_host << '\'';
return -1;
}
_server_address = server_addr_and_port;
const ChannelSignature sig = ComputeChannelSignature(_options);
std::shared_ptr<SocketSSLContext> ssl_ctx;
if (CreateSocketSSLContext(_options, &ssl_ctx) != 0) {
return -1;
}
if (SocketMapInsert(SocketMapKey(server_addr_and_port, sig),
&_server_id, ssl_ctx, _options.use_rdma, _options.hc_option) != 0) {
&_server_id, ssl_ctx, _options.use_rdma,
_options.hc_option, client_end_point) != 0) {
LOG(ERROR) << "Fail to insert into SocketMap";
return -1;
}
Expand Down Expand Up @@ -397,6 +410,13 @@ int Channel::Init(const char* ns_url,
_options.mutable_ssl_options()->sni_name = _service_name;
}
}
butil::EndPoint client_end_point;
if (!_options.client_host.empty() &&
butil::str2ip(_options.client_host.c_str(), &client_end_point.ip) != 0 &&
butil::hostname2ip(_options.client_host.c_str(), &client_end_point.ip) != 0) {
LOG(ERROR) << "Invalid client host=`" << _options.client_host << '\'';
return -1;
}
Comment thread
wenjiecn marked this conversation as resolved.
Outdated
std::unique_ptr<LoadBalancerWithNaming> lb(new (std::nothrow)
LoadBalancerWithNaming);
if (NULL == lb) {
Expand All @@ -409,6 +429,7 @@ int Channel::Init(const char* ns_url,
ns_opt.use_rdma = _options.use_rdma;
ns_opt.channel_signature = ComputeChannelSignature(_options);
ns_opt.hc_option = _options.hc_option;
ns_opt.client_end_point = client_end_point;
if (CreateSocketSSLContext(_options, &ns_opt.ssl_ctx) != 0) {
return -1;
}
Expand Down
4 changes: 4 additions & 0 deletions src/brpc/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ struct ChannelOptions {
// Its priority is higher than FLAGS_health_check_path and FLAGS_health_check_timeout_ms.
// When it is not set, FLAGS_health_check_path and FLAGS_health_check_timeout_ms will take effect.
HealthCheckOption hc_option;

// IP address or host name of the client
Comment thread
wenjiecn marked this conversation as resolved.
Outdated
// Default: ""
std::string client_host;
private:
// SSLOptions is large and not often used, allocate it on heap to
// prevent ChannelOptions from being bloated in most cases.
Expand Down
3 changes: 2 additions & 1 deletion src/brpc/details/naming_service_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ void NamingServiceThread::Actions::ResetServers(
// to pick those Sockets with the right settings during OnAddedServers
const SocketMapKey key(_added[i], _owner->_options.channel_signature);
CHECK_EQ(0, SocketMapInsert(key, &tagged_id.id, _owner->_options.ssl_ctx,
_owner->_options.use_rdma, _owner->_options.hc_option));
_owner->_options.use_rdma, _owner->_options.hc_option,
_owner->_options.client_end_point));
_added_sockets.push_back(tagged_id);
}

Expand Down
1 change: 1 addition & 0 deletions src/brpc/details/naming_service_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ struct GetNamingServiceThreadOptions {
HealthCheckOption hc_option;
ChannelSignature channel_signature;
std::shared_ptr<SocketSSLContext> ssl_ctx;
butil::EndPoint client_end_point;
};

// A dedicated thread to map a name to ServerIds
Expand Down
11 changes: 9 additions & 2 deletions src/brpc/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ int Socket::OnCreated(const SocketOptions& options) {
_keytable_pool = options.keytable_pool;
_tos = 0;
_remote_side = options.remote_side;
_local_side = butil::EndPoint();
_local_side = options.local_side;
_on_edge_triggered_events = options.on_edge_triggered_events;
_user = options.user;
_conn = options.conn;
Expand Down Expand Up @@ -1283,8 +1283,10 @@ int Socket::Connect(const timespec* abstime,
_ssl_state = SSL_OFF;
}
struct sockaddr_storage serv_addr;
struct sockaddr_storage cli_addr;
socklen_t addr_size = 0;
if (butil::endpoint2sockaddr(remote_side(), &serv_addr, &addr_size) != 0) {
if (butil::endpoint2sockaddr(remote_side(), &serv_addr, &addr_size) != 0 ||
butil::endpoint2sockaddr(local_side(), &cli_addr, &addr_size) != 0) {
Comment thread
wenjiecn marked this conversation as resolved.
Outdated
Comment thread
wenjiecn marked this conversation as resolved.
Outdated
PLOG(ERROR) << "Fail to get sockaddr";
return -1;
}
Expand All @@ -1297,6 +1299,10 @@ int Socket::Connect(const timespec* abstime,
// We need to do async connect (to manage the timeout by ourselves).
CHECK_EQ(0, butil::make_non_blocking(sockfd));

if (::bind(sockfd, (struct sockaddr*)& cli_addr, addr_size) != 0) {
Comment thread
wenjiecn marked this conversation as resolved.
Outdated
Copy link

Copilot AI Dec 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's an extra space in the cast: "(struct sockaddr*)& cli_addr" should be "(struct sockaddr*)&cli_addr" (no space between '&' and 'cli_addr') to be consistent with line 1307 where the same pattern is used correctly.

Suggested change
if (::bind(sockfd, (struct sockaddr*)& cli_addr, addr_size) != 0) {
if (::bind(sockfd, (struct sockaddr*)&cli_addr, addr_size) != 0) {

Copilot uses AI. Check for mistakes.
LOG(FATAL) << "Fail to bind socket, errno=" << strerror(errno);
Comment thread
wenjiecn marked this conversation as resolved.
Outdated
Comment thread
wenjiecn marked this conversation as resolved.
Outdated
return -1;
}
Comment thread
wenjiecn marked this conversation as resolved.
Outdated
const int rc = ::connect(
sockfd, (struct sockaddr*)&serv_addr, addr_size);
if (rc != 0 && errno != EINPROGRESS) {
Expand Down Expand Up @@ -2811,6 +2817,7 @@ int Socket::GetPooledSocket(SocketUniquePtr* pooled_socket) {
if (socket_pool == NULL) {
SocketOptions opt;
opt.remote_side = remote_side();
opt.local_side = local_side();
opt.user = user();
opt.on_edge_triggered_events = _on_edge_triggered_events;
opt.initial_ssl_ctx = _ssl_ctx;
Expand Down
1 change: 1 addition & 0 deletions src/brpc/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ struct SocketOptions {
// user->BeforeRecycle() before recycling.
int fd{-1};
butil::EndPoint remote_side;
butil::EndPoint local_side;
// If `connect_on_create' is true and `fd' is less than 0,
// a client connection will be established to remote_side()
// regarding deadline `connect_abstime' when Socket is being created.
Expand Down
9 changes: 6 additions & 3 deletions src/brpc/socket_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ SocketMap* get_or_new_client_side_socket_map() {
int SocketMapInsert(const SocketMapKey& key, SocketId* id,
const std::shared_ptr<SocketSSLContext>& ssl_ctx,
bool use_rdma,
const HealthCheckOption& hc_option) {
return get_or_new_client_side_socket_map()->Insert(key, id, ssl_ctx, use_rdma, hc_option);
const HealthCheckOption& hc_option,
butil::EndPoint& client_end_point) {
Comment thread
wenjiecn marked this conversation as resolved.
Outdated
return get_or_new_client_side_socket_map()->Insert(key, id, ssl_ctx, use_rdma, hc_option, client_end_point);
}

int SocketMapFind(const SocketMapKey& key, SocketId* id) {
Expand Down Expand Up @@ -229,7 +230,8 @@ void SocketMap::ShowSocketMapInBvarIfNeed() {
int SocketMap::Insert(const SocketMapKey& key, SocketId* id,
const std::shared_ptr<SocketSSLContext>& ssl_ctx,
bool use_rdma,
const HealthCheckOption& hc_option) {
const HealthCheckOption& hc_option,
butil::EndPoint& client_end_point) {
Comment thread
wenjiecn marked this conversation as resolved.
Outdated
ShowSocketMapInBvarIfNeed();

std::unique_lock<butil::Mutex> mu(_mutex);
Expand All @@ -251,6 +253,7 @@ int SocketMap::Insert(const SocketMapKey& key, SocketId* id,
SocketId tmp_id;
SocketOptions opt;
opt.remote_side = key.peer.addr;
opt.local_side = client_end_point;
opt.initial_ssl_ctx = ssl_ctx;
opt.use_rdma = use_rdma;
opt.hc_option = hc_option;
Expand Down
18 changes: 12 additions & 6 deletions src/brpc/socket_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,21 @@ struct SocketMapKeyHasher {
int SocketMapInsert(const SocketMapKey& key, SocketId* id,
const std::shared_ptr<SocketSSLContext>& ssl_ctx,
bool use_rdma,
const HealthCheckOption& hc_option);
const HealthCheckOption& hc_option,
Comment thread
wenjiecn marked this conversation as resolved.
Outdated
butil::EndPoint& client_end_point);
Comment thread
wenjiecn marked this conversation as resolved.
Outdated

inline int SocketMapInsert(const SocketMapKey& key, SocketId* id,
const std::shared_ptr<SocketSSLContext>& ssl_ctx) {
HealthCheckOption hc_option;
return SocketMapInsert(key, id, ssl_ctx, false, hc_option);
butil::EndPoint endpoint;
return SocketMapInsert(key, id, ssl_ctx, false, hc_option, endpoint);
}

inline int SocketMapInsert(const SocketMapKey& key, SocketId* id) {
std::shared_ptr<SocketSSLContext> empty_ptr;
HealthCheckOption hc_option;
return SocketMapInsert(key, id, empty_ptr, false, hc_option);
butil::EndPoint endpoint;
return SocketMapInsert(key, id, empty_ptr, false, hc_option, endpoint);
}

// Find the SocketId associated with `key'.
Expand Down Expand Up @@ -155,17 +158,20 @@ class SocketMap {
int Insert(const SocketMapKey& key, SocketId* id,
const std::shared_ptr<SocketSSLContext>& ssl_ctx,
bool use_rdma,
const HealthCheckOption& hc_option);
const HealthCheckOption& hc_option,
butil::EndPoint& client_end_point);
Comment thread
wenjiecn marked this conversation as resolved.
Outdated

int Insert(const SocketMapKey& key, SocketId* id,
const std::shared_ptr<SocketSSLContext>& ssl_ctx) {
HealthCheckOption hc_option;
return Insert(key, id, ssl_ctx, false, hc_option);
butil::EndPoint endpoint;
return Insert(key, id, ssl_ctx, false, hc_option, endpoint);
}
int Insert(const SocketMapKey& key, SocketId* id) {
std::shared_ptr<SocketSSLContext> empty_ptr;
HealthCheckOption hc_option;
return Insert(key, id, empty_ptr, false, hc_option);
butil::EndPoint endpoint;
return Insert(key, id, empty_ptr, false, hc_option, endpoint);
}

void Remove(const SocketMapKey& key, SocketId expected_id);
Expand Down
Loading