Skip to content

Commit 6961552

Browse files
Xiangyu Bumeta-codesync[bot]
authored andcommitted
Properly apply pre-bind server socket options
Summary: D107555254 adds support to take the SocketOptionsMap in AsyncServerSocket::bind() call and apply the pre-bind socket options. This diff updates wangle side to use this API. Note that otherwise pre-bind socket options are never properly applied and behave like silent no-ops. Reviewed By: mingtaoy Differential Revision: D107560210 fbshipit-source-id: ee36ef669b7ba9c75f9afa2012582b8da11e3e79
1 parent f0fba9c commit 6961552

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

third-party/wangle/src/wangle/bootstrap/ServerSocketFactory.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class AsyncServerSocketFactory : public ServerSocketFactory {
7171
if (config.enableTCPFastOpen) {
7272
socket->setTFOEnabled(true, config.fastOpenQueueSize);
7373
}
74-
socket->bind(address);
74+
socket->bind(address, config.getSocketOptions());
7575

7676
socket->listen(config.acceptBacklog);
7777
socket->startAccepting();

third-party/wangle/src/wangle/bootstrap/test/BootstrapTest.cpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
#include "wangle/bootstrap/ServerBootstrap.h"
2121
#include "wangle/channel/Handler.h"
2222

23+
#include <folly/io/SocketOptionMap.h>
24+
#include <folly/io/async/AsyncServerSocket.h>
2325
#include <folly/portability/GTest.h>
26+
#include <folly/portability/Sockets.h>
2427
#include <folly/synchronization/Latch.h>
2528
#include <folly/testing/TestUtil.h>
2629
#include <wangle/util/Logging.h>
@@ -85,6 +88,40 @@ class TestAcceptorFactory : public AcceptorFactory {
8588
}
8689
};
8790

91+
namespace {
92+
93+
int getReceiveLowWatermark(const AsyncServerSocket& serverSocket) {
94+
int value = 0;
95+
socklen_t valueLength = sizeof(value);
96+
EXPECT_EQ(
97+
netops::getsockopt(
98+
serverSocket.getNetworkSocket(),
99+
SOL_SOCKET,
100+
SO_RCVLOWAT,
101+
&value,
102+
&valueLength),
103+
0);
104+
return value;
105+
}
106+
107+
} // namespace
108+
109+
TEST(Bootstrap, AsyncServerSocketFactoryAppliesPreBindSocketOptions) {
110+
ServerSocketConfig config;
111+
config.bindAddress = SocketAddress("127.0.0.1", 0);
112+
SocketOptionMap options;
113+
options[{SOL_SOCKET, SO_RCVLOWAT, SocketOptionKey::ApplyPos::PRE_BIND}] = 2;
114+
config.setSocketOptions(options);
115+
116+
AsyncServerSocketFactory factory;
117+
auto socketBase = factory.newSocket(
118+
config.bindAddress, config.acceptBacklog, false, config, nullptr);
119+
auto socket = std::dynamic_pointer_cast<AsyncServerSocket>(socketBase);
120+
ASSERT_NE(socket, nullptr);
121+
122+
EXPECT_EQ(getReceiveLowWatermark(*socket), 2);
123+
}
124+
88125
TEST(Bootstrap, Basic) {
89126
TestServer server;
90127
TestClient client;
@@ -408,7 +445,7 @@ TEST(Bootstrap, UnixServer) {
408445
server.stop();
409446
server.join();
410447

411-
EXPECT_TRUE(std::move(pipelineFuture).get() != nullptr);
448+
EXPECT_NE(std::move(pipelineFuture).get(), nullptr);
412449
EXPECT_EQ(factory->pipelines, 1);
413450
}
414451

0 commit comments

Comments
 (0)