fix AsyncServerSocket::bind issue
[folly.git] / folly / io / async / AsyncServerSocket.cpp
1 /*
2  * Copyright 2014 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef __STDC_FORMAT_MACROS
18   #define __STDC_FORMAT_MACROS
19 #endif
20
21 #include <folly/io/async/AsyncServerSocket.h>
22
23 #include <folly/FileUtil.h>
24 #include <folly/SocketAddress.h>
25 #include <folly/io/async/EventBase.h>
26 #include <folly/io/async/NotificationQueue.h>
27
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <netinet/tcp.h>
31 #include <string.h>
32 #include <sys/socket.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35
36 namespace folly {
37
38 const uint32_t AsyncServerSocket::kDefaultMaxAcceptAtOnce;
39 const uint32_t AsyncServerSocket::kDefaultCallbackAcceptAtOnce;
40 const uint32_t AsyncServerSocket::kDefaultMaxMessagesInQueue;
41
42 int setCloseOnExec(int fd, int value) {
43   // Read the current flags
44   int old_flags = fcntl(fd, F_GETFD, 0);
45
46   // If reading the flags failed, return error indication now
47   if (old_flags < 0)
48     return -1;
49
50   // Set just the flag we want to set
51   int new_flags;
52   if (value != 0)
53     new_flags = old_flags | FD_CLOEXEC;
54   else
55     new_flags = old_flags & ~FD_CLOEXEC;
56
57   // Store modified flag word in the descriptor
58   return fcntl(fd, F_SETFD, new_flags);
59 }
60
61 void AsyncServerSocket::RemoteAcceptor::start(
62   EventBase* eventBase, uint32_t maxAtOnce, uint32_t maxInQueue) {
63   setMaxReadAtOnce(maxAtOnce);
64   queue_.setMaxQueueSize(maxInQueue);
65
66   if (!eventBase->runInEventBaseThread([=](){
67         callback_->acceptStarted();
68         this->startConsuming(eventBase, &queue_);
69       })) {
70     throw std::invalid_argument("unable to start waiting on accept "
71                             "notification queue in the specified "
72                             "EventBase thread");
73   }
74 }
75
76 void AsyncServerSocket::RemoteAcceptor::stop(
77   EventBase* eventBase, AcceptCallback* callback) {
78   if (!eventBase->runInEventBaseThread([=](){
79         callback->acceptStopped();
80         delete this;
81       })) {
82     throw std::invalid_argument("unable to start waiting on accept "
83                             "notification queue in the specified "
84                             "EventBase thread");
85   }
86 }
87
88 void AsyncServerSocket::RemoteAcceptor::messageAvailable(
89   QueueMessage&& msg) {
90
91   switch (msg.type) {
92     case MessageType::MSG_NEW_CONN:
93     {
94       callback_->connectionAccepted(msg.fd, msg.address);
95       break;
96     }
97     case MessageType::MSG_ERROR:
98     {
99       std::runtime_error ex(msg.msg);
100       callback_->acceptError(ex);
101       break;
102     }
103     default:
104     {
105       LOG(ERROR) << "invalid accept notification message type "
106                  << int(msg.type);
107       std::runtime_error ex(
108         "received invalid accept notification message type");
109       callback_->acceptError(ex);
110     }
111   }
112 }
113
114 /*
115  * AsyncServerSocket::BackoffTimeout
116  */
117 class AsyncServerSocket::BackoffTimeout : public AsyncTimeout {
118  public:
119   BackoffTimeout(AsyncServerSocket* socket)
120     : AsyncTimeout(socket->getEventBase()),
121       socket_(socket) {}
122
123   virtual void timeoutExpired() noexcept {
124     socket_->backoffTimeoutExpired();
125   }
126
127  private:
128   AsyncServerSocket* socket_;
129 };
130
131 /*
132  * AsyncServerSocket methods
133  */
134
135 AsyncServerSocket::AsyncServerSocket(EventBase* eventBase)
136 :   eventBase_(eventBase),
137     accepting_(false),
138     maxAcceptAtOnce_(kDefaultMaxAcceptAtOnce),
139     maxNumMsgsInQueue_(kDefaultMaxMessagesInQueue),
140     acceptRateAdjustSpeed_(0),
141     acceptRate_(1),
142     lastAccepTimestamp_(std::chrono::steady_clock::now()),
143     numDroppedConnections_(0),
144     callbackIndex_(0),
145     backoffTimeout_(nullptr),
146     callbacks_(),
147     keepAliveEnabled_(true),
148     closeOnExec_(true),
149     shutdownSocketSet_(nullptr) {
150 }
151
152 void AsyncServerSocket::setShutdownSocketSet(ShutdownSocketSet* newSS) {
153   if (shutdownSocketSet_ == newSS) {
154     return;
155   }
156   if (shutdownSocketSet_) {
157     for (auto& h : sockets_) {
158       shutdownSocketSet_->remove(h.socket_);
159     }
160   }
161   shutdownSocketSet_ = newSS;
162   if (shutdownSocketSet_) {
163     for (auto& h : sockets_) {
164       shutdownSocketSet_->add(h.socket_);
165     }
166   }
167 }
168
169 AsyncServerSocket::~AsyncServerSocket() {
170   assert(callbacks_.empty());
171 }
172
173 int AsyncServerSocket::stopAccepting(int shutdownFlags) {
174   int result = 0;
175   for (auto& handler : sockets_) {
176     VLOG(10) << "AsyncServerSocket::stopAccepting " << this <<
177               handler.socket_;
178   }
179   assert(eventBase_ == nullptr || eventBase_->isInEventBaseThread());
180
181   // When destroy is called, unregister and close the socket immediately
182   accepting_ = false;
183
184   for (auto& handler : sockets_) {
185     handler.unregisterHandler();
186     if (shutdownSocketSet_) {
187       shutdownSocketSet_->close(handler.socket_);
188     } else if (shutdownFlags >= 0) {
189       result = shutdownNoInt(handler.socket_, shutdownFlags);
190       pendingCloseSockets_.push_back(handler.socket_);
191     } else {
192       closeNoInt(handler.socket_);
193     }
194   }
195   sockets_.clear();
196
197   // Destroy the backoff timout.  This will cancel it if it is running.
198   delete backoffTimeout_;
199   backoffTimeout_ = nullptr;
200
201   // Close all of the callback queues to notify them that they are being
202   // destroyed.  No one should access the AsyncServerSocket any more once
203   // destroy() is called.  However, clear out callbacks_ before invoking the
204   // accept callbacks just in case.  This will potentially help us detect the
205   // bug if one of the callbacks calls addAcceptCallback() or
206   // removeAcceptCallback().
207   std::vector<CallbackInfo> callbacksCopy;
208   callbacks_.swap(callbacksCopy);
209   for (std::vector<CallbackInfo>::iterator it = callbacksCopy.begin();
210        it != callbacksCopy.end();
211        ++it) {
212     it->consumer->stop(it->eventBase, it->callback);
213   }
214
215   return result;
216 }
217
218 void AsyncServerSocket::destroy() {
219   stopAccepting();
220   for (auto s : pendingCloseSockets_) {
221     closeNoInt(s);
222   }
223   // Then call DelayedDestruction::destroy() to take care of
224   // whether or not we need immediate or delayed destruction
225   DelayedDestruction::destroy();
226 }
227
228 void AsyncServerSocket::attachEventBase(EventBase *eventBase) {
229   assert(eventBase_ == nullptr);
230   assert(eventBase->isInEventBaseThread());
231
232   eventBase_ = eventBase;
233   for (auto& handler : sockets_) {
234     handler.attachEventBase(eventBase);
235   }
236 }
237
238 void AsyncServerSocket::detachEventBase() {
239   assert(eventBase_ != nullptr);
240   assert(eventBase_->isInEventBaseThread());
241   assert(!accepting_);
242
243   eventBase_ = nullptr;
244   for (auto& handler : sockets_) {
245     handler.detachEventBase();
246   }
247 }
248
249 void AsyncServerSocket::useExistingSockets(const std::vector<int>& fds) {
250   assert(eventBase_ == nullptr || eventBase_->isInEventBaseThread());
251
252   if (sockets_.size() > 0) {
253     throw std::invalid_argument(
254                               "cannot call useExistingSocket() on a "
255                               "AsyncServerSocket that already has a socket");
256   }
257
258   for (auto fd: fds) {
259     // Set addressFamily_ from this socket.
260     // Note that the socket may not have been bound yet, but
261     // setFromLocalAddress() will still work and get the correct address family.
262     // We will update addressFamily_ again anyway if bind() is called later.
263     SocketAddress address;
264     address.setFromLocalAddress(fd);
265
266     setupSocket(fd);
267     sockets_.push_back(
268       ServerEventHandler(eventBase_, fd, this, address.getFamily()));
269     sockets_.back().changeHandlerFD(fd);
270   }
271 }
272
273 void AsyncServerSocket::useExistingSocket(int fd) {
274   useExistingSockets({fd});
275 }
276
277 void AsyncServerSocket::bindSocket(
278     int fd,
279     const SocketAddress& address,
280     bool isExistingSocket) {
281   sockaddr_storage addrStorage;
282   address.getAddress(&addrStorage);
283   sockaddr* saddr = reinterpret_cast<sockaddr*>(&addrStorage);
284   if (::bind(fd, saddr, address.getActualSize()) != 0) {
285     if (!isExistingSocket) {
286       closeNoInt(fd);
287     }
288     folly::throwSystemError(errno,
289         "failed to bind to async server socket: " +
290         address.describe());
291   }
292
293   // If we just created this socket, update the EventHandler and set socket_
294   if (!isExistingSocket) {
295     sockets_.push_back(
296       ServerEventHandler(eventBase_, fd, this, address.getFamily()));
297   }
298 }
299
300 void AsyncServerSocket::bind(const SocketAddress& address) {
301   assert(eventBase_ == nullptr || eventBase_->isInEventBaseThread());
302
303   // useExistingSocket() may have been called to initialize socket_ already.
304   // However, in the normal case we need to create a new socket now.
305   // Don't set socket_ yet, so that socket_ will remain uninitialized if an
306   // error occurs.
307   int fd;
308   if (sockets_.size() == 0) {
309     fd = createSocket(address.getFamily());
310   } else if (sockets_.size() == 1) {
311     if (address.getFamily() != sockets_[0].addressFamily_) {
312       throw std::invalid_argument(
313                                 "Attempted to bind address to socket with "
314                                 "different address family");
315     }
316     fd = sockets_[0].socket_;
317   } else {
318     throw std::invalid_argument(
319                               "Attempted to bind to multiple fds");
320   }
321
322   bindSocket(fd, address, !sockets_.empty());
323 }
324
325 void AsyncServerSocket::bind(
326     const std::vector<IPAddress>& ipAddresses,
327     uint16_t port) {
328   if (ipAddresses.empty()) {
329     throw std::invalid_argument("No ip addresses were provided");
330   }
331   if (!sockets_.empty()) {
332     throw std::invalid_argument("Cannot call bind on a AsyncServerSocket "
333                                 "that already has a socket.");
334   }
335
336   for (const IPAddress& ipAddress : ipAddresses) {
337     SocketAddress address(ipAddress.toFullyQualified(), port);
338     int fd = createSocket(address.getFamily());
339
340     bindSocket(fd, address, false);
341   }
342   if (sockets_.size() == 0) {
343     throw std::runtime_error(
344         "did not bind any async server socket for port and addresses");
345   }
346 }
347
348 void AsyncServerSocket::bind(uint16_t port) {
349   struct addrinfo hints, *res, *res0;
350   char sport[sizeof("65536")];
351
352   memset(&hints, 0, sizeof(hints));
353   hints.ai_family = AF_UNSPEC;
354   hints.ai_socktype = SOCK_STREAM;
355   hints.ai_flags = AI_PASSIVE;
356   snprintf(sport, sizeof(sport), "%u", port);
357
358   if (getaddrinfo(nullptr, sport, &hints, &res0)) {
359     throw std::invalid_argument(
360                               "Attempted to bind address to socket with "
361                               "bad getaddrinfo");
362   }
363
364   SCOPE_EXIT { freeaddrinfo(res0); };
365
366   auto setupAddress = [&] (struct addrinfo* res) {
367     int s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
368     // IPv6/IPv4 may not be supported by the kernel
369     if (s < 0 && errno == EAFNOSUPPORT) {
370       return;
371     }
372     CHECK_GE(s, 0);
373
374     try {
375       setupSocket(s);
376     } catch (...) {
377       closeNoInt(s);
378       throw;
379     }
380
381     if (res->ai_family == AF_INET6) {
382       int v6only = 1;
383       CHECK(0 == setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
384                             &v6only, sizeof(v6only)));
385     }
386
387     SocketAddress address;
388     address.setFromLocalAddress(s);
389
390     sockets_.push_back(
391       ServerEventHandler(eventBase_, s, this, address.getFamily()));
392
393     // Bind to the socket
394     if (::bind(s, res->ai_addr, res->ai_addrlen) != 0) {
395       folly::throwSystemError(
396         errno,
397         "failed to bind to async server socket for port");
398     }
399   };
400
401   const int kNumTries = 5;
402   for (int tries = 1; true; tries++) {
403     // Prefer AF_INET6 addresses. RFC 3484 mandates that getaddrinfo
404     // should return IPv6 first and then IPv4 addresses, but glibc's
405     // getaddrinfo(nullptr) with AI_PASSIVE returns:
406     // - 0.0.0.0 (IPv4-only)
407     // - :: (IPv6+IPv4) in this order
408     // See: https://sourceware.org/bugzilla/show_bug.cgi?id=9981
409     for (res = res0; res; res = res->ai_next) {
410       if (res->ai_family == AF_INET6) {
411         setupAddress(res);
412       }
413     }
414
415     // If port == 0, then we should try to bind to the same port on ipv4 and
416     // ipv6.  So if we did bind to ipv6, figure out that port and use it,
417     // except for the last attempt when we just use any port available.
418     if (sockets_.size() == 1 && port == 0) {
419       SocketAddress address;
420       address.setFromLocalAddress(sockets_.back().socket_);
421       snprintf(sport, sizeof(sport), "%u", address.getPort());
422       freeaddrinfo(res0);
423       CHECK_EQ(0, getaddrinfo(nullptr, sport, &hints, &res0));
424     }
425
426     try {
427       for (res = res0; res; res = res->ai_next) {
428         if (res->ai_family != AF_INET6) {
429           setupAddress(res);
430         }
431       }
432     } catch (const std::system_error& e) {
433       // if we can't bind to the same port on ipv4 as ipv6 when using port=0
434       // then we will try again another 2 times before giving up.  We do this
435       // by closing the sockets that were opened, then redoing the whole thing
436       if (port == 0 && !sockets_.empty() && tries != kNumTries) {
437         for (const auto& socket : sockets_) {
438           if (socket.socket_ <= 0) {
439             continue;
440           } else if (shutdownSocketSet_) {
441             shutdownSocketSet_->close(socket.socket_);
442           } else {
443             closeNoInt(socket.socket_);
444           }
445         }
446         sockets_.clear();
447         snprintf(sport, sizeof(sport), "%u", port);
448         freeaddrinfo(res0);
449         CHECK_EQ(0, getaddrinfo(nullptr, sport, &hints, &res0));
450         continue;
451       }
452       throw;
453     }
454
455     break;
456   }
457
458   if (sockets_.size() == 0) {
459     throw std::runtime_error(
460         "did not bind any async server socket for port");
461   }
462 }
463
464 void AsyncServerSocket::listen(int backlog) {
465   assert(eventBase_ == nullptr || eventBase_->isInEventBaseThread());
466
467   // Start listening
468   for (auto& handler : sockets_) {
469     if (::listen(handler.socket_, backlog) == -1) {
470       folly::throwSystemError(errno,
471                                     "failed to listen on async server socket");
472     }
473   }
474 }
475
476 void AsyncServerSocket::getAddress(SocketAddress* addressReturn) const {
477   CHECK(sockets_.size() >= 1);
478   VLOG_IF(2, sockets_.size() > 1)
479     << "Warning: getAddress() called and multiple addresses available ("
480     << sockets_.size() << "). Returning only the first one.";
481
482   addressReturn->setFromLocalAddress(sockets_[0].socket_);
483 }
484
485 std::vector<SocketAddress> AsyncServerSocket::getAddresses()
486     const {
487   CHECK(sockets_.size() >= 1);
488   auto tsaVec = std::vector<SocketAddress>(sockets_.size());
489   auto tsaIter = tsaVec.begin();
490   for (const auto& socket : sockets_) {
491     (tsaIter++)->setFromLocalAddress(socket.socket_);
492   };
493   return tsaVec;
494 }
495
496 void AsyncServerSocket::addAcceptCallback(AcceptCallback *callback,
497                                            EventBase *eventBase,
498                                            uint32_t maxAtOnce) {
499   assert(eventBase_ == nullptr || eventBase_->isInEventBaseThread());
500
501   // If this is the first accept callback and we are supposed to be accepting,
502   // start accepting once the callback is installed.
503   bool runStartAccepting = accepting_ && callbacks_.empty();
504
505   if (!eventBase) {
506     eventBase = eventBase_; // Run in AsyncServerSocket's eventbase
507   }
508
509   callbacks_.push_back(CallbackInfo(callback, eventBase));
510
511   // Start the remote acceptor.
512   //
513   // It would be nice if we could avoid starting the remote acceptor if
514   // eventBase == eventBase_.  However, that would cause issues if
515   // detachEventBase() and attachEventBase() were ever used to change the
516   // primary EventBase for the server socket.  Therefore we require the caller
517   // to specify a nullptr EventBase if they want to ensure that the callback is
518   // always invoked in the primary EventBase, and to be able to invoke that
519   // callback more efficiently without having to use a notification queue.
520   RemoteAcceptor* acceptor = nullptr;
521   try {
522     acceptor = new RemoteAcceptor(callback);
523     acceptor->start(eventBase, maxAtOnce, maxNumMsgsInQueue_);
524   } catch (...) {
525     callbacks_.pop_back();
526     delete acceptor;
527     throw;
528   }
529   callbacks_.back().consumer = acceptor;
530
531   // If this is the first accept callback and we are supposed to be accepting,
532   // start accepting.
533   if (runStartAccepting) {
534     startAccepting();
535   }
536 }
537
538 void AsyncServerSocket::removeAcceptCallback(AcceptCallback *callback,
539                                               EventBase *eventBase) {
540   assert(eventBase_ == nullptr || eventBase_->isInEventBaseThread());
541
542   // Find the matching AcceptCallback.
543   // We just do a simple linear search; we don't expect removeAcceptCallback()
544   // to be called frequently, and we expect there to only be a small number of
545   // callbacks anyway.
546   std::vector<CallbackInfo>::iterator it = callbacks_.begin();
547   uint32_t n = 0;
548   while (true) {
549     if (it == callbacks_.end()) {
550       throw std::runtime_error("AsyncServerSocket::removeAcceptCallback(): "
551                               "accept callback not found");
552     }
553     if (it->callback == callback &&
554         (it->eventBase == eventBase || eventBase == nullptr)) {
555       break;
556     }
557     ++it;
558     ++n;
559   }
560
561   // Remove this callback from callbacks_.
562   //
563   // Do this before invoking the acceptStopped() callback, in case
564   // acceptStopped() invokes one of our methods that examines callbacks_.
565   //
566   // Save a copy of the CallbackInfo first.
567   CallbackInfo info(*it);
568   callbacks_.erase(it);
569   if (n < callbackIndex_) {
570     // We removed an element before callbackIndex_.  Move callbackIndex_ back
571     // one step, since things after n have been shifted back by 1.
572     --callbackIndex_;
573   } else {
574     // We removed something at or after callbackIndex_.
575     // If we removed the last element and callbackIndex_ was pointing at it,
576     // we need to reset callbackIndex_ to 0.
577     if (callbackIndex_ >= callbacks_.size()) {
578       callbackIndex_ = 0;
579     }
580   }
581
582   info.consumer->stop(info.eventBase, info.callback);
583
584   // If we are supposed to be accepting but the last accept callback
585   // was removed, unregister for events until a callback is added.
586   if (accepting_ && callbacks_.empty()) {
587     for (auto& handler : sockets_) {
588       handler.unregisterHandler();
589     }
590   }
591 }
592
593 void AsyncServerSocket::startAccepting() {
594   assert(eventBase_ == nullptr || eventBase_->isInEventBaseThread());
595
596   accepting_ = true;
597   if (callbacks_.empty()) {
598     // We can't actually begin accepting if no callbacks are defined.
599     // Wait until a callback is added to start accepting.
600     return;
601   }
602
603   for (auto& handler : sockets_) {
604     if (!handler.registerHandler(
605           EventHandler::READ | EventHandler::PERSIST)) {
606       throw std::runtime_error("failed to register for accept events");
607     }
608   }
609 }
610
611 void AsyncServerSocket::pauseAccepting() {
612   assert(eventBase_ == nullptr || eventBase_->isInEventBaseThread());
613   accepting_ = false;
614   for (auto& handler : sockets_) {
615    handler. unregisterHandler();
616   }
617
618   // If we were in the accept backoff state, disable the backoff timeout
619   if (backoffTimeout_) {
620     backoffTimeout_->cancelTimeout();
621   }
622 }
623
624 int AsyncServerSocket::createSocket(int family) {
625   int fd = socket(family, SOCK_STREAM, 0);
626   if (fd == -1) {
627     folly::throwSystemError(errno, "error creating async server socket");
628   }
629
630   try {
631     setupSocket(fd);
632   } catch (...) {
633     closeNoInt(fd);
634     throw;
635   }
636   return fd;
637 }
638
639 void AsyncServerSocket::setupSocket(int fd) {
640   // Get the address family
641   SocketAddress address;
642   address.setFromLocalAddress(fd);
643   auto family = address.getFamily();
644
645   // Put the socket in non-blocking mode
646   if (fcntl(fd, F_SETFL, O_NONBLOCK) != 0) {
647     folly::throwSystemError(errno,
648                             "failed to put socket in non-blocking mode");
649   }
650
651   // Set reuseaddr to avoid 2MSL delay on server restart
652   int one = 1;
653   if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) != 0) {
654     // This isn't a fatal error; just log an error message and continue
655     LOG(ERROR) << "failed to set SO_REUSEADDR on async server socket " << errno;
656   }
657
658   // Set reuseport to support multiple accept threads
659   int zero = 0;
660   if (reusePortEnabled_ &&
661       setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(int)) != 0) {
662     LOG(ERROR) << "failed to set SO_REUSEPORT on async server socket "
663                << strerror(errno);
664     folly::throwSystemError(errno,
665                             "failed to bind to async server socket: " +
666                             address.describe());
667   }
668
669   // Set keepalive as desired
670   if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
671                  (keepAliveEnabled_) ? &one : &zero, sizeof(int)) != 0) {
672     LOG(ERROR) << "failed to set SO_KEEPALIVE on async server socket: " <<
673             strerror(errno);
674   }
675
676   // Setup FD_CLOEXEC flag
677   if (closeOnExec_ &&
678       (-1 == folly::setCloseOnExec(fd, closeOnExec_))) {
679     LOG(ERROR) << "failed to set FD_CLOEXEC on async server socket: " <<
680             strerror(errno);
681   }
682
683   // Set TCP nodelay if available, MAC OS X Hack
684   // See http://lists.danga.com/pipermail/memcached/2005-March/001240.html
685 #ifndef TCP_NOPUSH
686   if (family != AF_UNIX) {
687     if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) != 0) {
688       // This isn't a fatal error; just log an error message and continue
689       LOG(ERROR) << "failed to set TCP_NODELAY on async server socket: " <<
690               strerror(errno);
691     }
692   }
693 #endif
694
695   if (shutdownSocketSet_) {
696     shutdownSocketSet_->add(fd);
697   }
698 }
699
700 void AsyncServerSocket::handlerReady(
701   uint16_t events, int fd, sa_family_t addressFamily) noexcept {
702   assert(!callbacks_.empty());
703   DestructorGuard dg(this);
704
705   // Only accept up to maxAcceptAtOnce_ connections at a time,
706   // to avoid starving other I/O handlers using this EventBase.
707   for (uint32_t n = 0; n < maxAcceptAtOnce_; ++n) {
708     SocketAddress address;
709
710     sockaddr_storage addrStorage;
711     socklen_t addrLen = sizeof(addrStorage);
712     sockaddr* saddr = reinterpret_cast<sockaddr*>(&addrStorage);
713
714     // In some cases, accept() doesn't seem to update these correctly.
715     saddr->sa_family = addressFamily;
716     if (addressFamily == AF_UNIX) {
717       addrLen = sizeof(struct sockaddr_un);
718     }
719
720     // Accept a new client socket
721 #ifdef SOCK_NONBLOCK
722     int clientSocket = accept4(fd, saddr, &addrLen, SOCK_NONBLOCK);
723 #else
724     int clientSocket = accept(fd, saddr, &addrLen);
725 #endif
726
727     address.setFromSockaddr(saddr, addrLen);
728
729     std::chrono::time_point<std::chrono::steady_clock> nowMs =
730       std::chrono::steady_clock::now();
731     int64_t timeSinceLastAccept = std::max(
732       int64_t(0),
733       nowMs.time_since_epoch().count() -
734       lastAccepTimestamp_.time_since_epoch().count());
735     lastAccepTimestamp_ = nowMs;
736     if (acceptRate_ < 1) {
737       acceptRate_ *= 1 + acceptRateAdjustSpeed_ * timeSinceLastAccept;
738       if (acceptRate_ >= 1) {
739         acceptRate_ = 1;
740       } else if (rand() > acceptRate_ * RAND_MAX) {
741         ++numDroppedConnections_;
742         if (clientSocket >= 0) {
743           closeNoInt(clientSocket);
744         }
745         continue;
746       }
747     }
748
749     if (clientSocket < 0) {
750       if (errno == EAGAIN) {
751         // No more sockets to accept right now.
752         // Check for this code first, since it's the most common.
753         return;
754       } else if (errno == EMFILE || errno == ENFILE) {
755         // We're out of file descriptors.  Perhaps we're accepting connections
756         // too quickly. Pause accepting briefly to back off and give the server
757         // a chance to recover.
758         LOG(ERROR) << "accept failed: out of file descriptors; entering accept "
759                 "back-off state";
760         enterBackoff();
761
762         // Dispatch the error message
763         dispatchError("accept() failed", errno);
764       } else {
765         dispatchError("accept() failed", errno);
766       }
767       return;
768     }
769
770 #ifndef SOCK_NONBLOCK
771     // Explicitly set the new connection to non-blocking mode
772     if (fcntl(clientSocket, F_SETFL, O_NONBLOCK) != 0) {
773       closeNoInt(clientSocket);
774       dispatchError("failed to set accepted socket to non-blocking mode",
775                     errno);
776       return;
777     }
778 #endif
779
780     // Inform the callback about the new connection
781     dispatchSocket(clientSocket, std::move(address));
782
783     // If we aren't accepting any more, break out of the loop
784     if (!accepting_ || callbacks_.empty()) {
785       break;
786     }
787   }
788 }
789
790 void AsyncServerSocket::dispatchSocket(int socket,
791                                         SocketAddress&& address) {
792   uint32_t startingIndex = callbackIndex_;
793
794   // Short circuit if the callback is in the primary EventBase thread
795
796   CallbackInfo *info = nextCallback();
797   if (info->eventBase == nullptr) {
798     info->callback->connectionAccepted(socket, address);
799     return;
800   }
801
802   // Create a message to send over the notification queue
803   QueueMessage msg;
804   msg.type = MessageType::MSG_NEW_CONN;
805   msg.address = std::move(address);
806   msg.fd = socket;
807
808   // Loop until we find a free queue to write to
809   while (true) {
810     if (info->consumer->getQueue()->tryPutMessageNoThrow(std::move(msg))) {
811       // Success! return.
812       return;
813    }
814
815     // We couldn't add to queue.  Fall through to below
816
817     ++numDroppedConnections_;
818     if (acceptRateAdjustSpeed_ > 0) {
819       // aggressively decrease accept rate when in trouble
820       static const double kAcceptRateDecreaseSpeed = 0.1;
821       acceptRate_ *= 1 - kAcceptRateDecreaseSpeed;
822     }
823
824
825     if (callbackIndex_ == startingIndex) {
826       // The notification queue was full
827       // We can't really do anything at this point other than close the socket.
828       //
829       // This should only happen if a user's service is behaving extremely
830       // badly and none of the EventBase threads are looping fast enough to
831       // process the incoming connections.  If the service is overloaded, it
832       // should use pauseAccepting() to temporarily back off accepting new
833       // connections, before they reach the point where their threads can't
834       // even accept new messages.
835       LOG(ERROR) << "failed to dispatch newly accepted socket:"
836                  << " all accept callback queues are full";
837       closeNoInt(socket);
838       return;
839     }
840
841     info = nextCallback();
842   }
843 }
844
845 void AsyncServerSocket::dispatchError(const char *msgstr, int errnoValue) {
846   uint32_t startingIndex = callbackIndex_;
847   CallbackInfo *info = nextCallback();
848
849   // Create a message to send over the notification queue
850   QueueMessage msg;
851   msg.type = MessageType::MSG_ERROR;
852   msg.err = errnoValue;
853   msg.msg = std::move(msgstr);
854
855   while (true) {
856     // Short circuit if the callback is in the primary EventBase thread
857     if (info->eventBase == nullptr) {
858       std::runtime_error ex(
859         std::string(msgstr) +  folly::to<std::string>(errnoValue));
860       info->callback->acceptError(ex);
861       return;
862     }
863
864     if (info->consumer->getQueue()->tryPutMessageNoThrow(std::move(msg))) {
865       return;
866     }
867     // Fall through and try another callback
868
869     if (callbackIndex_ == startingIndex) {
870       // The notification queues for all of the callbacks were full.
871       // We can't really do anything at this point.
872       LOG(ERROR) << "failed to dispatch accept error: all accept callback "
873         "queues are full: error msg:  " <<
874         msg.msg.c_str() << errnoValue;
875       return;
876     }
877     info = nextCallback();
878   }
879 }
880
881 void AsyncServerSocket::enterBackoff() {
882   // If this is the first time we have entered the backoff state,
883   // allocate backoffTimeout_.
884   if (backoffTimeout_ == nullptr) {
885     try {
886       backoffTimeout_ = new BackoffTimeout(this);
887     } catch (const std::bad_alloc& ex) {
888       // Man, we couldn't even allocate the timer to re-enable accepts.
889       // We must be in pretty bad shape.  Don't pause accepting for now,
890       // since we won't be able to re-enable ourselves later.
891       LOG(ERROR) << "failed to allocate AsyncServerSocket backoff"
892                  << " timer; unable to temporarly pause accepting";
893       return;
894     }
895   }
896
897   // For now, we simply pause accepting for 1 second.
898   //
899   // We could add some smarter backoff calculation here in the future.  (e.g.,
900   // start sleeping for longer if we keep hitting the backoff frequently.)
901   // Typically the user needs to figure out why the server is overloaded and
902   // fix it in some other way, though.  The backoff timer is just a simple
903   // mechanism to try and give the connection processing code a little bit of
904   // breathing room to catch up, and to avoid just spinning and failing to
905   // accept over and over again.
906   const uint32_t timeoutMS = 1000;
907   if (!backoffTimeout_->scheduleTimeout(timeoutMS)) {
908     LOG(ERROR) << "failed to schedule AsyncServerSocket backoff timer;"
909                << "unable to temporarly pause accepting";
910     return;
911   }
912
913   // The backoff timer is scheduled to re-enable accepts.
914   // Go ahead and disable accepts for now.  We leave accepting_ set to true,
915   // since that tracks the desired state requested by the user.
916   for (auto& handler : sockets_) {
917     handler.unregisterHandler();
918   }
919 }
920
921 void AsyncServerSocket::backoffTimeoutExpired() {
922   // accepting_ should still be true.
923   // If pauseAccepting() was called while in the backoff state it will cancel
924   // the backoff timeout.
925   assert(accepting_);
926   // We can't be detached from the EventBase without being paused
927   assert(eventBase_ != nullptr && eventBase_->isInEventBaseThread());
928
929   // If all of the callbacks were removed, we shouldn't re-enable accepts
930   if (callbacks_.empty()) {
931     return;
932   }
933
934   // Register the handler.
935   for (auto& handler : sockets_) {
936     if (!handler.registerHandler(
937           EventHandler::READ | EventHandler::PERSIST)) {
938       // We're hosed.  We could just re-schedule backoffTimeout_ to
939       // re-try again after a little bit.  However, we don't want to
940       // loop retrying forever if we can't re-enable accepts.  Just
941       // abort the entire program in this state; things are really bad
942       // and restarting the entire server is probably the best remedy.
943       LOG(ERROR)
944         << "failed to re-enable AsyncServerSocket accepts after backoff; "
945         << "crashing now";
946       abort();
947     }
948   }
949 }
950
951
952
953 } // folly