fix: set socket buffer options before connect#3368
Open
howzi wants to merge 1 commit into
Open
Conversation
51bfd0e to
308fadf
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes the timing of applying TCP socket buffer size options (SO_SNDBUF/SO_RCVBUF) so they take effect reliably by setting them before connect() on client sockets and before bind()/listen() on server listening sockets (so accepted sockets inherit the sizes).
Changes:
- Refactors socket buffer sizing into
SetSocketBufferOptions(int fd)and applies it beforeconnect()inSocket::Connect. - Extends
butil::tcp_listenwith an optional callback invoked beforebind()/listen()and uses it fromServer::StartInternalto set listener buffer sizes early. - Adds unit tests verifying buffer sizes are set before connect and inherited on accept.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| test/brpc_socket_unittest.cpp | Adds helper routines and tests to validate buffer option timing/inheritance. |
| src/butil/endpoint.h | Adds BeforeListenCallback type + tcp_listen overload taking a callback. |
| src/butil/endpoint.cpp | Implements callback-aware tcp_listen and keeps the original wrapper overload. |
| src/brpc/socket.h | Declares SetSocketBufferOptions(int fd) helper. |
| src/brpc/socket.cpp | Moves buffer sizing into SetSocketBufferOptions and calls it before connect(). |
| src/brpc/server.cpp | Passes SetSocketBufferOptions into tcp_listen so listener fds get sized early. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+1158
to
+1161
| GetSocketBufferValues(reference_fd, expected); | ||
|
|
||
| ASSERT_NE(default_values.recv_buffer, expected->recv_buffer); | ||
| ASSERT_NE(default_values.send_buffer, expected->send_buffer); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What problem does this PR solve?
现有的tcp SNDBUF和RCVBUF设置时机有问题,不仅会导致设置失败,测试中发现还会导致buf固定位非预期的值。
参考:

https://man7.org/linux/man-pages/man7/tcp.7.html