Skip to content

Commit 8bd8610

Browse files
committed
fix UT & review
1 parent 2578b05 commit 8bd8610

4 files changed

Lines changed: 19 additions & 13 deletions

File tree

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/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+
LOG(FATAL) << "Fail to bind 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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ 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) {
96+
const butil::EndPoint& client_endpoint) {
9797
return get_or_new_client_side_socket_map()->Insert(key, id, ssl_ctx, use_rdma, hc_option, client_end_point);
9898
}
9999

@@ -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);

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)