Skip to content

Conversation

@wenjiecn
Copy link
Contributor

@wenjiecn wenjiecn commented Dec 25, 2025

What problem does this PR solve?

Issue Number: #3157, #3178

Problem Summary:

What is changed and the side effects?

Changed:
1.在ChannelOptions中添加client_host,让用户设置client的ip或hostname
2.在SocketOptions 中添加butil::EndPoint local_side,由 client_host:0 -> local_side
3.在GetNamingServiceThreadOptions添加butil::EndPoint client_end_point,由 client_host:0 -> client_end_point
4.在ComputeChannelSignature中append "clih=client_host", 进行hash
5.在系统调用socket创建socket后,调用bind绑定ip
Side effects:

  • Performance effects:

  • Breaking backward compatibility:


Check List:

@wwbmmm
Copy link
Contributor

wwbmmm commented Dec 26, 2025

The unit test is broken, please fix.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds support for binding client sockets to a specific IP address or hostname before connecting to a server, addressing issue #3157. The feature allows users to control which local network interface is used for outgoing connections.

  • Adds client_host field to ChannelOptions for user configuration
  • Propagates client endpoint information through SocketOptions, GetNamingServiceThreadOptions, and channel signature computation
  • Implements socket binding in Socket::Connect() to bind the client socket to the specified IP before connecting

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 12 comments.

Show a summary per file
File Description
src/brpc/channel.h Adds client_host string field to ChannelOptions for specifying client IP/hostname
src/brpc/channel.cpp Implements parsing of client_host to EndPoint and includes it in channel signature; handles both InitSingle and Init paths
src/brpc/socket.h Adds local_side field to SocketOptions to store the client endpoint
src/brpc/socket.cpp Implements bind() call in Connect() to bind socket to client IP; initializes local_side from options
src/brpc/socket_map.h Updates function signatures to accept client_end_point parameter
src/brpc/socket_map.cpp Passes client_end_point through to SocketOptions when creating sockets
src/brpc/details/naming_service_thread.h Adds client_end_point field to GetNamingServiceThreadOptions
src/brpc/details/naming_service_thread.cpp Passes client_end_point to SocketMapInsert when adding servers

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@wenjiecn wenjiecn reopened this Dec 26, 2025
@wenjiecn wenjiecn changed the title bind_client_ip support bind client ip Dec 26, 2025
@wenjiecn wenjiecn closed this Dec 27, 2025
@wenjiecn wenjiecn reopened this Dec 27, 2025
@wenjiecn wenjiecn closed this Dec 27, 2025
@wenjiecn wenjiecn reopened this Dec 27, 2025
@chenBright
Copy link
Contributor

Please add some unit tests for this feature.

@wenjiecn
Copy link
Contributor Author

Please add some unit tests for this feature.

@chenBright done

@wwbmmm
Copy link
Contributor

wwbmmm commented Dec 29, 2025

LGTM

@wenjiecn wenjiecn force-pushed the bind_client_ip branch 2 times, most recently from 6cb6396 to c1c6e1a Compare January 5, 2026 03:12
@wenjiecn wenjiecn changed the title support bind client ip support SO_BINDTODEVICE Jan 5, 2026
// We need to do async connect (to manage the timeout by ourselves).
CHECK_EQ(0, butil::make_non_blocking(sockfd));

if (!_device_name.empty()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

不是很确定 #3157 需要的是指定source IP,还是指定网络设备。如果只需要指定source IP, 之前的bind可以满足需求。这里要不要把之前的那个client_host选项一并加进来?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

确实可以加进来,让网络配置更精细化,我有空合并一下

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

是否支持SO_BINDTODEVICE brpc channel 能否支持绑定client ip

4 participants