Skip to content

Commit 4aac92d

Browse files
committed
fix UT & review
1 parent 2578b05 commit 4aac92d

File tree

7 files changed

+31
-25
lines changed

7 files changed

+31
-25
lines changed

src/brpc/channel.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,10 @@ int Channel::InitSingle(const butil::EndPoint& server_addr_and_port,
367367
LOG(ERROR) << "Invalid port=" << port;
368368
return -1;
369369
}
370-
butil::EndPoint client_end_point;
370+
butil::EndPoint client_endpoint;
371371
if (!_options.client_host.empty() &&
372-
butil::str2ip(_options.client_host.c_str(), &client_end_point.ip) != 0 &&
373-
butil::hostname2ip(_options.client_host.c_str(), &client_end_point.ip) != 0) {
372+
butil::str2ip(_options.client_host.c_str(), &client_endpoint.ip) != 0 &&
373+
butil::hostname2ip(_options.client_host.c_str(), &client_endpoint.ip) != 0) {
374374
LOG(ERROR) << "Invalid client host=`" << _options.client_host << '\'';
375375
return -1;
376376
}
@@ -382,7 +382,7 @@ int Channel::InitSingle(const butil::EndPoint& server_addr_and_port,
382382
}
383383
if (SocketMapInsert(SocketMapKey(server_addr_and_port, sig),
384384
&_server_id, ssl_ctx, _options.use_rdma,
385-
_options.hc_option, client_end_point) != 0) {
385+
_options.hc_option, client_endpoint) != 0) {
386386
LOG(ERROR) << "Fail to insert into SocketMap";
387387
return -1;
388388
}
@@ -410,10 +410,10 @@ int Channel::Init(const char* ns_url,
410410
_options.mutable_ssl_options()->sni_name = _service_name;
411411
}
412412
}
413-
butil::EndPoint client_end_point;
413+
butil::EndPoint client_endpoint;
414414
if (!_options.client_host.empty() &&
415-
butil::str2ip(_options.client_host.c_str(), &client_end_point.ip) != 0 &&
416-
butil::hostname2ip(_options.client_host.c_str(), &client_end_point.ip) != 0) {
415+
butil::str2ip(_options.client_host.c_str(), &client_endpoint.ip) != 0 &&
416+
butil::hostname2ip(_options.client_host.c_str(), &client_endpoint.ip) != 0) {
417417
LOG(ERROR) << "Invalid client host=`" << _options.client_host << '\'';
418418
return -1;
419419
}
@@ -429,7 +429,7 @@ int Channel::Init(const char* ns_url,
429429
ns_opt.use_rdma = _options.use_rdma;
430430
ns_opt.channel_signature = ComputeChannelSignature(_options);
431431
ns_opt.hc_option = _options.hc_option;
432-
ns_opt.client_end_point = client_end_point;
432+
ns_opt.client_endpoint = client_endpoint;
433433
if (CreateSocketSSLContext(_options, &ns_opt.ssl_ctx) != 0) {
434434
return -1;
435435
}

src/brpc/channel.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ struct ChannelOptions {
149149
// When it is not set, FLAGS_health_check_path and FLAGS_health_check_timeout_ms will take effect.
150150
HealthCheckOption hc_option;
151151

152-
// IP address or host name of the client
152+
// IP address or host name of the client.
153+
// if the client_host is "", the client IP address is determined by the OS.
153154
// Default: ""
154155
std::string client_host;
155156
private:

src/brpc/details/naming_service_thread.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ void NamingServiceThread::Actions::ResetServers(
127127
const SocketMapKey key(_added[i], _owner->_options.channel_signature);
128128
CHECK_EQ(0, SocketMapInsert(key, &tagged_id.id, _owner->_options.ssl_ctx,
129129
_owner->_options.use_rdma, _owner->_options.hc_option,
130-
_owner->_options.client_end_point));
130+
_owner->_options.client_endpoint));
131131
_added_sockets.push_back(tagged_id);
132132
}
133133

src/brpc/details/naming_service_thread.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ struct GetNamingServiceThreadOptions {
5353
HealthCheckOption hc_option;
5454
ChannelSignature channel_signature;
5555
std::shared_ptr<SocketSSLContext> ssl_ctx;
56-
butil::EndPoint client_end_point;
56+
butil::EndPoint client_endpoint;
5757
};
5858

5959
// A dedicated thread to map a name to ServerIds

src/brpc/socket.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,10 +1283,8 @@ int Socket::Connect(const timespec* abstime,
12831283
_ssl_state = SSL_OFF;
12841284
}
12851285
struct sockaddr_storage serv_addr;
1286-
struct sockaddr_storage cli_addr;
12871286
socklen_t addr_size = 0;
1288-
if (butil::endpoint2sockaddr(remote_side(), &serv_addr, &addr_size) != 0 ||
1289-
butil::endpoint2sockaddr(local_side(), &cli_addr, &addr_size) != 0) {
1287+
if (butil::endpoint2sockaddr(remote_side(), &serv_addr, &addr_size) != 0) {
12901288
PLOG(ERROR) << "Fail to get sockaddr";
12911289
return -1;
12921290
}
@@ -1298,10 +1296,16 @@ int Socket::Connect(const timespec* abstime,
12981296
CHECK_EQ(0, butil::make_close_on_exec(sockfd));
12991297
// We need to do async connect (to manage the timeout by ourselves).
13001298
CHECK_EQ(0, butil::make_non_blocking(sockfd));
1301-
1302-
if (::bind(sockfd, (struct sockaddr*)& cli_addr, addr_size) != 0) {
1303-
LOG(FATAL) << "Fail to bind socket, errno=" << strerror(errno);
1304-
return -1;
1299+
if (local_side().ip != butil::IP_ANY) {
1300+
struct sockaddr_storage cli_addr;
1301+
if (butil::endpoint2sockaddr(local_side(), &cli_addr, &addr_size) != 0) {
1302+
PLOG(ERROR) << "Fail to get client sockaddr";
1303+
return -1;
1304+
}
1305+
if (::bind(sockfd, (struct sockaddr*)&cli_addr, addr_size) != 0) {
1306+
PLOG(ERROR) << "Fail to bind client socket, errno=" << strerror(errno);
1307+
return -1;
1308+
}
13051309
}
13061310
const int rc = ::connect(
13071311
sockfd, (struct sockaddr*)&serv_addr, addr_size);
@@ -2817,7 +2821,7 @@ int Socket::GetPooledSocket(SocketUniquePtr* pooled_socket) {
28172821
if (socket_pool == NULL) {
28182822
SocketOptions opt;
28192823
opt.remote_side = remote_side();
2820-
opt.local_side = local_side();
2824+
opt.local_side = butil::EndPoint(local_side().ip, 0);
28212825
opt.user = user();
28222826
opt.on_edge_triggered_events = _on_edge_triggered_events;
28232827
opt.initial_ssl_ctx = _ssl_ctx;
@@ -2919,6 +2923,7 @@ int Socket::GetShortSocket(SocketUniquePtr* short_socket) {
29192923
SocketId id;
29202924
SocketOptions opt;
29212925
opt.remote_side = remote_side();
2926+
opt.local_side = butil::EndPoint(local_side().ip, 0);
29222927
opt.user = user();
29232928
opt.on_edge_triggered_events = _on_edge_triggered_events;
29242929
opt.initial_ssl_ctx = _ssl_ctx;

src/brpc/socket_map.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ int SocketMapInsert(const SocketMapKey& key, SocketId* id,
9393
const std::shared_ptr<SocketSSLContext>& ssl_ctx,
9494
bool use_rdma,
9595
const HealthCheckOption& hc_option,
96-
butil::EndPoint& client_end_point) {
97-
return get_or_new_client_side_socket_map()->Insert(key, id, ssl_ctx, use_rdma, hc_option, client_end_point);
96+
const butil::EndPoint& client_endpoint) {
97+
return get_or_new_client_side_socket_map()->Insert(key, id, ssl_ctx, use_rdma, hc_option, client_endpoint);
9898
}
9999

100100
int SocketMapFind(const SocketMapKey& key, SocketId* id) {
@@ -231,7 +231,7 @@ int SocketMap::Insert(const SocketMapKey& key, SocketId* id,
231231
const std::shared_ptr<SocketSSLContext>& ssl_ctx,
232232
bool use_rdma,
233233
const HealthCheckOption& hc_option,
234-
butil::EndPoint& client_end_point) {
234+
const butil::EndPoint& client_endpoint) {
235235
ShowSocketMapInBvarIfNeed();
236236

237237
std::unique_lock<butil::Mutex> mu(_mutex);
@@ -253,7 +253,7 @@ int SocketMap::Insert(const SocketMapKey& key, SocketId* id,
253253
SocketId tmp_id;
254254
SocketOptions opt;
255255
opt.remote_side = key.peer.addr;
256-
opt.local_side = client_end_point;
256+
opt.local_side = client_endpoint;
257257
opt.initial_ssl_ctx = ssl_ctx;
258258
opt.use_rdma = use_rdma;
259259
opt.hc_option = hc_option;

src/brpc/socket_map.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ int SocketMapInsert(const SocketMapKey& key, SocketId* id,
8383
const std::shared_ptr<SocketSSLContext>& ssl_ctx,
8484
bool use_rdma,
8585
const HealthCheckOption& hc_option,
86-
butil::EndPoint& client_end_point);
86+
const butil::EndPoint& client_endpoint);
8787

8888
inline int SocketMapInsert(const SocketMapKey& key, SocketId* id,
8989
const std::shared_ptr<SocketSSLContext>& ssl_ctx) {
@@ -159,7 +159,7 @@ class SocketMap {
159159
const std::shared_ptr<SocketSSLContext>& ssl_ctx,
160160
bool use_rdma,
161161
const HealthCheckOption& hc_option,
162-
butil::EndPoint& client_end_point);
162+
const butil::EndPoint& client_endpoint);
163163

164164
int Insert(const SocketMapKey& key, SocketId* id,
165165
const std::shared_ptr<SocketSSLContext>& ssl_ctx) {

0 commit comments

Comments
 (0)