net/socket: Use SO_REUSEPORT for server sockets

master
klzgrad 2024-03-10 10:30:35 +08:00
parent 2aae9e3773
commit 3e7036d3cd
1 changed files with 11 additions and 0 deletions

View File

@ -487,6 +487,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<const char*>(&reuseport), sizeof(reuseport));
// Ignore errors that the option does not exist.
if (rv != 0 && errno != ENOPROTOOPT)
return MapSystemError(errno);
#endif // SO_REUSEPORT
return AllowAddressReuse();
}