From a684355d80b5ca551dc8398a083b57e870145d94 Mon Sep 17 00:00:00 2001 From: klzgrad Date: Sun, 10 Mar 2024 10:30:35 +0800 Subject: [PATCH] net/socket: Use SO_REUSEPORT for server sockets --- src/net/socket/tcp_socket_posix.cc | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/net/socket/tcp_socket_posix.cc b/src/net/socket/tcp_socket_posix.cc index 7d860d33d3..f2716c9924 100644 --- a/src/net/socket/tcp_socket_posix.cc +++ b/src/net/socket/tcp_socket_posix.cc @@ -404,6 +404,17 @@ int TCPSocketPosix::GetPeerAddress(IPEndPoint* address) const { int TCPSocketPosix::SetDefaultOptionsForServer() { DCHECK(socket_); + +#ifdef SO_REUSEPORT + int reuseport = 1; + int rv = + setsockopt(socket_->socket_fd(), SOL_SOCKET, SO_REUSEPORT, + reinterpret_cast(&reuseport), sizeof(reuseport)); + // Ignore errors that the option does not exist. + if (rv != 0 && errno != ENOPROTOOPT) + return MapSystemError(errno); +#endif // SO_REUSEPORT + return AllowAddressReuse(); }