asyncudpserversocket reuse port
authorDave Watson <davejwatson@fb.com>
Mon, 30 Mar 2015 17:17:12 +0000 (10:17 -0700)
committerafrind <afrind@fb.com>
Thu, 2 Apr 2015 19:00:25 +0000 (12:00 -0700)
Summary:
AsyncUDPSocket supports reuse port, also make it available in AsyncUDPServerSocket.
Has to happen before bind

Test Plan:
?? it builds?

Mutliple binds already work with reuseaddr, but load balancing doesn't work as well.  See http://lwn.net/Articles/542629/ for details

Reviewed By: naizhi@fb.com

Subscribers: doug, fugalh, folly-diffs@, jsedgwick, yfeldblum, chalfant

FB internal diff: D1948932

Signature: t1:1948932:1427473013:8c114458a0746bb2b6957db33c1834179d860c00

folly/io/async/AsyncUDPServerSocket.h
folly/wangle/bootstrap/ServerSocketFactory.h

index 485f18b03b6127212e108c50630d866f87995443..04682297fcccc4fcaceeb8a86c2cb0c46637b388 100644 (file)
@@ -86,9 +86,14 @@ class AsyncUDPServerSocket : private AsyncUDPSocket::ReadCallback
     CHECK(!socket_);
 
     socket_ = folly::make_unique<AsyncUDPSocket>(evb_);
+    socket_->setReusePort(reusePort_);
     socket_->bind(addy);
   }
 
+  void setReusePort(bool reusePort) {
+    reusePort_ = reusePort;
+  }
+
   folly::SocketAddress address() const {
     CHECK(socket_);
     return socket_->address();
@@ -202,6 +207,8 @@ class AsyncUDPServerSocket : private AsyncUDPSocket::ReadCallback
 
   // Temporary buffer for data
   folly::IOBufQueue buf_;
+
+  bool reusePort_{false};
 };
 
 } // Namespace
index ca99c2eebf4494a788247938b5ca343b65b34c26..1e05ec8b135e06bafc5ca10c7f52d7e2deee453b 100644 (file)
@@ -88,7 +88,7 @@ class AsyncUDPServerSocketFactory : public ServerSocketFactory {
 
     auto socket = std::make_shared<AsyncUDPServerSocket>(
       EventBaseManager::get()->getEventBase());
-    //socket->setReusePortEnabled(reuse);
+    socket->setReusePort(reuse);
     SocketAddress addressr("::1", port);
     socket->bind(addressr);
     socket->listen();