2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef __STDC_FORMAT_MACROS
18 #define __STDC_FORMAT_MACROS
21 #include <folly/io/async/AsyncServerSocket.h>
23 #include <folly/FileUtil.h>
24 #include <folly/Portability.h>
25 #include <folly/SocketAddress.h>
26 #include <folly/String.h>
27 #include <folly/detail/SocketFastOpen.h>
28 #include <folly/io/async/EventBase.h>
29 #include <folly/io/async/NotificationQueue.h>
30 #include <folly/portability/Fcntl.h>
31 #include <folly/portability/Sockets.h>
32 #include <folly/portability/Unistd.h>
36 #include <sys/types.h>
38 namespace fsp = folly::portability::sockets;
42 static constexpr bool msgErrQueueSupported =
47 #endif // MSG_ERRQUEUE
49 const uint32_t AsyncServerSocket::kDefaultMaxAcceptAtOnce;
50 const uint32_t AsyncServerSocket::kDefaultCallbackAcceptAtOnce;
51 const uint32_t AsyncServerSocket::kDefaultMaxMessagesInQueue;
53 int setCloseOnExec(int fd, int value) {
54 // Read the current flags
55 int old_flags = fcntl(fd, F_GETFD, 0);
57 // If reading the flags failed, return error indication now
61 // Set just the flag we want to set
64 new_flags = old_flags | FD_CLOEXEC;
66 new_flags = old_flags & ~FD_CLOEXEC;
68 // Store modified flag word in the descriptor
69 return fcntl(fd, F_SETFD, new_flags);
72 void AsyncServerSocket::RemoteAcceptor::start(
73 EventBase* eventBase, uint32_t maxAtOnce, uint32_t maxInQueue) {
74 setMaxReadAtOnce(maxAtOnce);
75 queue_.setMaxQueueSize(maxInQueue);
77 if (!eventBase->runInEventBaseThread([=](){
78 callback_->acceptStarted();
79 this->startConsuming(eventBase, &queue_);
81 throw std::invalid_argument("unable to start waiting on accept "
82 "notification queue in the specified "
87 void AsyncServerSocket::RemoteAcceptor::stop(
88 EventBase* eventBase, AcceptCallback* callback) {
89 if (!eventBase->runInEventBaseThread([=](){
90 callback->acceptStopped();
93 throw std::invalid_argument("unable to start waiting on accept "
94 "notification queue in the specified "
99 void AsyncServerSocket::RemoteAcceptor::messageAvailable(
100 QueueMessage&& msg) noexcept {
102 case MessageType::MSG_NEW_CONN:
104 if (connectionEventCallback_) {
105 connectionEventCallback_->onConnectionDequeuedByAcceptorCallback(
106 msg.fd, msg.address);
108 callback_->connectionAccepted(msg.fd, msg.address);
111 case MessageType::MSG_ERROR:
113 std::runtime_error ex(msg.msg);
114 callback_->acceptError(ex);
119 LOG(ERROR) << "invalid accept notification message type "
121 std::runtime_error ex(
122 "received invalid accept notification message type");
123 callback_->acceptError(ex);
129 * AsyncServerSocket::BackoffTimeout
131 class AsyncServerSocket::BackoffTimeout : public AsyncTimeout {
133 // Disallow copy, move, and default constructors.
134 BackoffTimeout(BackoffTimeout&&) = delete;
135 explicit BackoffTimeout(AsyncServerSocket* socket)
136 : AsyncTimeout(socket->getEventBase()), socket_(socket) {}
138 void timeoutExpired() noexcept override { socket_->backoffTimeoutExpired(); }
141 AsyncServerSocket* socket_;
145 * AsyncServerSocket methods
148 AsyncServerSocket::AsyncServerSocket(EventBase* eventBase)
149 : eventBase_(eventBase),
151 maxAcceptAtOnce_(kDefaultMaxAcceptAtOnce),
152 maxNumMsgsInQueue_(kDefaultMaxMessagesInQueue),
153 acceptRateAdjustSpeed_(0),
155 lastAccepTimestamp_(std::chrono::steady_clock::now()),
156 numDroppedConnections_(0),
158 backoffTimeout_(nullptr),
160 keepAliveEnabled_(true),
162 shutdownSocketSet_(nullptr) {
165 void AsyncServerSocket::setShutdownSocketSet(ShutdownSocketSet* newSS) {
166 if (shutdownSocketSet_ == newSS) {
169 if (shutdownSocketSet_) {
170 for (auto& h : sockets_) {
171 shutdownSocketSet_->remove(h.socket_);
174 shutdownSocketSet_ = newSS;
175 if (shutdownSocketSet_) {
176 for (auto& h : sockets_) {
177 shutdownSocketSet_->add(h.socket_);
182 AsyncServerSocket::~AsyncServerSocket() {
183 assert(callbacks_.empty());
186 int AsyncServerSocket::stopAccepting(int shutdownFlags) {
188 for (auto& handler : sockets_) {
189 VLOG(10) << "AsyncServerSocket::stopAccepting " << this <<
193 eventBase_->dcheckIsInEventBaseThread();
196 // When destroy is called, unregister and close the socket immediately.
199 // Close the sockets in reverse order as they were opened to avoid
200 // the condition where another process concurrently tries to open
201 // the same port, succeed to bind the first socket but fails on the
202 // second because it hasn't been closed yet.
203 for (; !sockets_.empty(); sockets_.pop_back()) {
204 auto& handler = sockets_.back();
205 handler.unregisterHandler();
206 if (shutdownSocketSet_) {
207 shutdownSocketSet_->close(handler.socket_);
208 } else if (shutdownFlags >= 0) {
209 result = shutdownNoInt(handler.socket_, shutdownFlags);
210 pendingCloseSockets_.push_back(handler.socket_);
212 closeNoInt(handler.socket_);
216 // Destroy the backoff timout. This will cancel it if it is running.
217 delete backoffTimeout_;
218 backoffTimeout_ = nullptr;
220 // Close all of the callback queues to notify them that they are being
221 // destroyed. No one should access the AsyncServerSocket any more once
222 // destroy() is called. However, clear out callbacks_ before invoking the
223 // accept callbacks just in case. This will potentially help us detect the
224 // bug if one of the callbacks calls addAcceptCallback() or
225 // removeAcceptCallback().
226 std::vector<CallbackInfo> callbacksCopy;
227 callbacks_.swap(callbacksCopy);
228 for (std::vector<CallbackInfo>::iterator it = callbacksCopy.begin();
229 it != callbacksCopy.end();
231 // consumer may not be set if we are running in primary event base
233 DCHECK(it->eventBase);
234 it->consumer->stop(it->eventBase, it->callback);
236 DCHECK(it->callback);
237 it->callback->acceptStopped();
244 void AsyncServerSocket::destroy() {
246 for (auto s : pendingCloseSockets_) {
249 // Then call DelayedDestruction::destroy() to take care of
250 // whether or not we need immediate or delayed destruction
251 DelayedDestruction::destroy();
254 void AsyncServerSocket::attachEventBase(EventBase *eventBase) {
255 assert(eventBase_ == nullptr);
256 eventBase->dcheckIsInEventBaseThread();
258 eventBase_ = eventBase;
259 for (auto& handler : sockets_) {
260 handler.attachEventBase(eventBase);
264 void AsyncServerSocket::detachEventBase() {
265 assert(eventBase_ != nullptr);
266 eventBase_->dcheckIsInEventBaseThread();
269 eventBase_ = nullptr;
270 for (auto& handler : sockets_) {
271 handler.detachEventBase();
275 void AsyncServerSocket::useExistingSockets(const std::vector<int>& fds) {
277 eventBase_->dcheckIsInEventBaseThread();
280 if (sockets_.size() > 0) {
281 throw std::invalid_argument(
282 "cannot call useExistingSocket() on a "
283 "AsyncServerSocket that already has a socket");
287 // Set addressFamily_ from this socket.
288 // Note that the socket may not have been bound yet, but
289 // setFromLocalAddress() will still work and get the correct address family.
290 // We will update addressFamily_ again anyway if bind() is called later.
291 SocketAddress address;
292 address.setFromLocalAddress(fd);
295 if (noTransparentTls_) {
296 // Ignore return value, errors are ok
297 setsockopt(fd, SOL_SOCKET, SO_NO_TRANSPARENT_TLS, nullptr, 0);
301 setupSocket(fd, address.getFamily());
302 sockets_.emplace_back(eventBase_, fd, this, address.getFamily());
303 sockets_.back().changeHandlerFD(fd);
307 void AsyncServerSocket::useExistingSocket(int fd) {
308 useExistingSockets({fd});
311 void AsyncServerSocket::bindSocket(
313 const SocketAddress& address,
314 bool isExistingSocket) {
315 sockaddr_storage addrStorage;
316 address.getAddress(&addrStorage);
317 sockaddr* saddr = reinterpret_cast<sockaddr*>(&addrStorage);
319 if (fsp::bind(fd, saddr, address.getActualSize()) != 0) {
320 if (!isExistingSocket) {
323 folly::throwSystemError(errno,
324 "failed to bind to async server socket: " +
329 if (noTransparentTls_) {
330 // Ignore return value, errors are ok
331 setsockopt(fd, SOL_SOCKET, SO_NO_TRANSPARENT_TLS, nullptr, 0);
335 // If we just created this socket, update the EventHandler and set socket_
336 if (!isExistingSocket) {
337 sockets_.emplace_back(eventBase_, fd, this, address.getFamily());
341 bool AsyncServerSocket::setZeroCopy(bool enable) {
342 if (msgErrQueueSupported) {
343 int fd = getSocket();
344 int val = enable ? 1 : 0;
345 int ret = setsockopt(fd, SOL_SOCKET, SO_ZEROCOPY, &val, sizeof(val));
353 void AsyncServerSocket::bind(const SocketAddress& address) {
355 eventBase_->dcheckIsInEventBaseThread();
358 // useExistingSocket() may have been called to initialize socket_ already.
359 // However, in the normal case we need to create a new socket now.
360 // Don't set socket_ yet, so that socket_ will remain uninitialized if an
363 if (sockets_.size() == 0) {
364 fd = createSocket(address.getFamily());
365 } else if (sockets_.size() == 1) {
366 if (address.getFamily() != sockets_[0].addressFamily_) {
367 throw std::invalid_argument(
368 "Attempted to bind address to socket with "
369 "different address family");
371 fd = sockets_[0].socket_;
373 throw std::invalid_argument(
374 "Attempted to bind to multiple fds");
377 bindSocket(fd, address, !sockets_.empty());
380 void AsyncServerSocket::bind(
381 const std::vector<IPAddress>& ipAddresses,
383 if (ipAddresses.empty()) {
384 throw std::invalid_argument("No ip addresses were provided");
386 if (!sockets_.empty()) {
387 throw std::invalid_argument("Cannot call bind on a AsyncServerSocket "
388 "that already has a socket.");
391 for (const IPAddress& ipAddress : ipAddresses) {
392 SocketAddress address(ipAddress.toFullyQualified(), port);
393 int fd = createSocket(address.getFamily());
395 bindSocket(fd, address, false);
397 if (sockets_.size() == 0) {
398 throw std::runtime_error(
399 "did not bind any async server socket for port and addresses");
403 void AsyncServerSocket::bind(uint16_t port) {
404 struct addrinfo hints, *res0;
405 char sport[sizeof("65536")];
407 memset(&hints, 0, sizeof(hints));
408 hints.ai_family = AF_UNSPEC;
409 hints.ai_socktype = SOCK_STREAM;
410 hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV;
411 snprintf(sport, sizeof(sport), "%u", port);
413 // On Windows the value we need to pass to bind to all available
414 // addresses is an empty string. Everywhere else, it's nullptr.
415 constexpr const char* kWildcardNode = kIsWindows ? "" : nullptr;
416 if (getaddrinfo(kWildcardNode, sport, &hints, &res0)) {
417 throw std::invalid_argument(
418 "Attempted to bind address to socket with "
422 SCOPE_EXIT { freeaddrinfo(res0); };
424 auto setupAddress = [&] (struct addrinfo* res) {
425 int s = fsp::socket(res->ai_family, res->ai_socktype, res->ai_protocol);
426 // IPv6/IPv4 may not be supported by the kernel
427 if (s < 0 && errno == EAFNOSUPPORT) {
433 setupSocket(s, res->ai_family);
439 if (res->ai_family == AF_INET6) {
441 CHECK(0 == setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
442 &v6only, sizeof(v6only)));
445 // Bind to the socket
446 if (fsp::bind(s, res->ai_addr, socklen_t(res->ai_addrlen)) != 0) {
447 folly::throwSystemError(
449 "failed to bind to async server socket for port ",
450 SocketAddress::getPortFrom(res->ai_addr),
452 SocketAddress::getFamilyNameFrom(res->ai_addr, "<unknown>"));
456 if (noTransparentTls_) {
457 // Ignore return value, errors are ok
458 setsockopt(s, SOL_SOCKET, SO_NO_TRANSPARENT_TLS, nullptr, 0);
462 SocketAddress address;
463 address.setFromLocalAddress(s);
465 sockets_.emplace_back(eventBase_, s, this, address.getFamily());
468 const int kNumTries = 25;
469 for (int tries = 1; true; tries++) {
470 // Prefer AF_INET6 addresses. RFC 3484 mandates that getaddrinfo
471 // should return IPv6 first and then IPv4 addresses, but glibc's
472 // getaddrinfo(nullptr) with AI_PASSIVE returns:
473 // - 0.0.0.0 (IPv4-only)
474 // - :: (IPv6+IPv4) in this order
475 // See: https://sourceware.org/bugzilla/show_bug.cgi?id=9981
476 for (struct addrinfo* res = res0; res; res = res->ai_next) {
477 if (res->ai_family == AF_INET6) {
482 // If port == 0, then we should try to bind to the same port on ipv4 and
483 // ipv6. So if we did bind to ipv6, figure out that port and use it.
484 if (sockets_.size() == 1 && port == 0) {
485 SocketAddress address;
486 address.setFromLocalAddress(sockets_.back().socket_);
487 snprintf(sport, sizeof(sport), "%u", address.getPort());
489 CHECK_EQ(0, getaddrinfo(nullptr, sport, &hints, &res0));
493 for (struct addrinfo* res = res0; res; res = res->ai_next) {
494 if (res->ai_family != AF_INET6) {
498 } catch (const std::system_error&) {
499 // If we can't bind to the same port on ipv4 as ipv6 when using
500 // port=0 then we will retry again before giving up after
501 // kNumTries attempts. We do this by closing the sockets that
502 // were opened, then restarting from scratch.
503 if (port == 0 && !sockets_.empty() && tries != kNumTries) {
504 for (const auto& socket : sockets_) {
505 if (socket.socket_ <= 0) {
507 } else if (shutdownSocketSet_) {
508 shutdownSocketSet_->close(socket.socket_);
510 closeNoInt(socket.socket_);
514 snprintf(sport, sizeof(sport), "%u", port);
516 CHECK_EQ(0, getaddrinfo(nullptr, sport, &hints, &res0));
526 if (sockets_.size() == 0) {
527 throw std::runtime_error(
528 "did not bind any async server socket for port");
532 void AsyncServerSocket::listen(int backlog) {
534 eventBase_->dcheckIsInEventBaseThread();
538 for (auto& handler : sockets_) {
539 if (fsp::listen(handler.socket_, backlog) == -1) {
540 folly::throwSystemError(errno,
541 "failed to listen on async server socket");
546 void AsyncServerSocket::getAddress(SocketAddress* addressReturn) const {
547 CHECK(sockets_.size() >= 1);
548 VLOG_IF(2, sockets_.size() > 1)
549 << "Warning: getAddress() called and multiple addresses available ("
550 << sockets_.size() << "). Returning only the first one.";
552 addressReturn->setFromLocalAddress(sockets_[0].socket_);
555 std::vector<SocketAddress> AsyncServerSocket::getAddresses()
557 CHECK(sockets_.size() >= 1);
558 auto tsaVec = std::vector<SocketAddress>(sockets_.size());
559 auto tsaIter = tsaVec.begin();
560 for (const auto& socket : sockets_) {
561 (tsaIter++)->setFromLocalAddress(socket.socket_);
566 void AsyncServerSocket::addAcceptCallback(AcceptCallback *callback,
567 EventBase *eventBase,
568 uint32_t maxAtOnce) {
570 eventBase_->dcheckIsInEventBaseThread();
573 // If this is the first accept callback and we are supposed to be accepting,
574 // start accepting once the callback is installed.
575 bool runStartAccepting = accepting_ && callbacks_.empty();
577 callbacks_.emplace_back(callback, eventBase);
580 // If this is the first accept callback and we are supposed to be accepting,
582 if (runStartAccepting) {
588 // Run in AsyncServerSocket's eventbase; notify that we are
589 // starting to accept connections
590 callback->acceptStarted();
594 // Start the remote acceptor.
596 // It would be nice if we could avoid starting the remote acceptor if
597 // eventBase == eventBase_. However, that would cause issues if
598 // detachEventBase() and attachEventBase() were ever used to change the
599 // primary EventBase for the server socket. Therefore we require the caller
600 // to specify a nullptr EventBase if they want to ensure that the callback is
601 // always invoked in the primary EventBase, and to be able to invoke that
602 // callback more efficiently without having to use a notification queue.
603 RemoteAcceptor* acceptor = nullptr;
605 acceptor = new RemoteAcceptor(callback, connectionEventCallback_);
606 acceptor->start(eventBase, maxAtOnce, maxNumMsgsInQueue_);
608 callbacks_.pop_back();
612 callbacks_.back().consumer = acceptor;
615 void AsyncServerSocket::removeAcceptCallback(AcceptCallback *callback,
616 EventBase *eventBase) {
618 eventBase_->dcheckIsInEventBaseThread();
621 // Find the matching AcceptCallback.
622 // We just do a simple linear search; we don't expect removeAcceptCallback()
623 // to be called frequently, and we expect there to only be a small number of
625 std::vector<CallbackInfo>::iterator it = callbacks_.begin();
628 if (it == callbacks_.end()) {
629 throw std::runtime_error("AsyncServerSocket::removeAcceptCallback(): "
630 "accept callback not found");
632 if (it->callback == callback &&
633 (it->eventBase == eventBase || eventBase == nullptr)) {
640 // Remove this callback from callbacks_.
642 // Do this before invoking the acceptStopped() callback, in case
643 // acceptStopped() invokes one of our methods that examines callbacks_.
645 // Save a copy of the CallbackInfo first.
646 CallbackInfo info(*it);
647 callbacks_.erase(it);
648 if (n < callbackIndex_) {
649 // We removed an element before callbackIndex_. Move callbackIndex_ back
650 // one step, since things after n have been shifted back by 1.
653 // We removed something at or after callbackIndex_.
654 // If we removed the last element and callbackIndex_ was pointing at it,
655 // we need to reset callbackIndex_ to 0.
656 if (callbackIndex_ >= callbacks_.size()) {
662 // consumer could be nullptr is we run callbacks in primary event
664 DCHECK(info.eventBase);
665 info.consumer->stop(info.eventBase, info.callback);
667 // callback invoked in the primary event base, just call directly
668 DCHECK(info.callback);
669 callback->acceptStopped();
672 // If we are supposed to be accepting but the last accept callback
673 // was removed, unregister for events until a callback is added.
674 if (accepting_ && callbacks_.empty()) {
675 for (auto& handler : sockets_) {
676 handler.unregisterHandler();
681 void AsyncServerSocket::startAccepting() {
683 eventBase_->dcheckIsInEventBaseThread();
687 if (callbacks_.empty()) {
688 // We can't actually begin accepting if no callbacks are defined.
689 // Wait until a callback is added to start accepting.
693 for (auto& handler : sockets_) {
694 if (!handler.registerHandler(
695 EventHandler::READ | EventHandler::PERSIST)) {
696 throw std::runtime_error("failed to register for accept events");
701 void AsyncServerSocket::pauseAccepting() {
703 eventBase_->dcheckIsInEventBaseThread();
706 for (auto& handler : sockets_) {
707 handler. unregisterHandler();
710 // If we were in the accept backoff state, disable the backoff timeout
711 if (backoffTimeout_) {
712 backoffTimeout_->cancelTimeout();
716 int AsyncServerSocket::createSocket(int family) {
717 int fd = fsp::socket(family, SOCK_STREAM, 0);
719 folly::throwSystemError(errno, "error creating async server socket");
723 setupSocket(fd, family);
731 void AsyncServerSocket::setupSocket(int fd, int family) {
732 // Put the socket in non-blocking mode
733 if (fcntl(fd, F_SETFL, O_NONBLOCK) != 0) {
734 folly::throwSystemError(errno,
735 "failed to put socket in non-blocking mode");
738 // Set reuseaddr to avoid 2MSL delay on server restart
740 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) != 0) {
741 // This isn't a fatal error; just log an error message and continue
742 LOG(ERROR) << "failed to set SO_REUSEADDR on async server socket " << errno;
745 // Set reuseport to support multiple accept threads
747 if (reusePortEnabled_ &&
748 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(int)) != 0) {
749 LOG(ERROR) << "failed to set SO_REUSEPORT on async server socket "
752 folly::throwSystemError(errno, "failed to bind to the async server socket");
754 SocketAddress address;
755 address.setFromLocalAddress(fd);
756 folly::throwSystemError(errno,
757 "failed to bind to async server socket: " +
762 // Set keepalive as desired
763 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
764 (keepAliveEnabled_) ? &one : &zero, sizeof(int)) != 0) {
765 LOG(ERROR) << "failed to set SO_KEEPALIVE on async server socket: " <<
769 // Setup FD_CLOEXEC flag
771 (-1 == folly::setCloseOnExec(fd, closeOnExec_))) {
772 LOG(ERROR) << "failed to set FD_CLOEXEC on async server socket: " <<
776 // Set TCP nodelay if available, MAC OS X Hack
777 // See http://lists.danga.com/pipermail/memcached/2005-March/001240.html
779 if (family != AF_UNIX) {
780 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) != 0) {
781 // This isn't a fatal error; just log an error message and continue
782 LOG(ERROR) << "failed to set TCP_NODELAY on async server socket: " <<
789 if (tfo_ && detail::tfo_enable(fd, tfoMaxQueueSize_) != 0) {
790 // This isn't a fatal error; just log an error message and continue
791 LOG(WARNING) << "failed to set TCP_FASTOPEN on async server socket: "
792 << folly::errnoStr(errno);
796 if (shutdownSocketSet_) {
797 shutdownSocketSet_->add(fd);
801 void AsyncServerSocket::handlerReady(uint16_t /* events */,
803 sa_family_t addressFamily) noexcept {
804 assert(!callbacks_.empty());
805 DestructorGuard dg(this);
807 // Only accept up to maxAcceptAtOnce_ connections at a time,
808 // to avoid starving other I/O handlers using this EventBase.
809 for (uint32_t n = 0; n < maxAcceptAtOnce_; ++n) {
810 SocketAddress address;
812 sockaddr_storage addrStorage;
813 socklen_t addrLen = sizeof(addrStorage);
814 sockaddr* saddr = reinterpret_cast<sockaddr*>(&addrStorage);
816 // In some cases, accept() doesn't seem to update these correctly.
817 saddr->sa_family = addressFamily;
818 if (addressFamily == AF_UNIX) {
819 addrLen = sizeof(struct sockaddr_un);
822 // Accept a new client socket
824 int clientSocket = accept4(fd, saddr, &addrLen, SOCK_NONBLOCK);
826 int clientSocket = accept(fd, saddr, &addrLen);
829 address.setFromSockaddr(saddr, addrLen);
831 if (clientSocket >= 0 && connectionEventCallback_) {
832 connectionEventCallback_->onConnectionAccepted(clientSocket, address);
835 std::chrono::time_point<std::chrono::steady_clock> nowMs =
836 std::chrono::steady_clock::now();
837 auto timeSinceLastAccept = std::max<int64_t>(
839 nowMs.time_since_epoch().count() -
840 lastAccepTimestamp_.time_since_epoch().count());
841 lastAccepTimestamp_ = nowMs;
842 if (acceptRate_ < 1) {
843 acceptRate_ *= 1 + acceptRateAdjustSpeed_ * timeSinceLastAccept;
844 if (acceptRate_ >= 1) {
846 } else if (rand() > acceptRate_ * RAND_MAX) {
847 ++numDroppedConnections_;
848 if (clientSocket >= 0) {
849 closeNoInt(clientSocket);
850 if (connectionEventCallback_) {
851 connectionEventCallback_->onConnectionDropped(clientSocket,
859 if (clientSocket < 0) {
860 if (errno == EAGAIN) {
861 // No more sockets to accept right now.
862 // Check for this code first, since it's the most common.
864 } else if (errno == EMFILE || errno == ENFILE) {
865 // We're out of file descriptors. Perhaps we're accepting connections
866 // too quickly. Pause accepting briefly to back off and give the server
867 // a chance to recover.
868 LOG(ERROR) << "accept failed: out of file descriptors; entering accept "
872 // Dispatch the error message
873 dispatchError("accept() failed", errno);
875 dispatchError("accept() failed", errno);
877 if (connectionEventCallback_) {
878 connectionEventCallback_->onConnectionAcceptError(errno);
883 #ifndef SOCK_NONBLOCK
884 // Explicitly set the new connection to non-blocking mode
885 if (fcntl(clientSocket, F_SETFL, O_NONBLOCK) != 0) {
886 closeNoInt(clientSocket);
887 dispatchError("failed to set accepted socket to non-blocking mode",
889 if (connectionEventCallback_) {
890 connectionEventCallback_->onConnectionDropped(clientSocket, address);
896 // Inform the callback about the new connection
897 dispatchSocket(clientSocket, std::move(address));
899 // If we aren't accepting any more, break out of the loop
900 if (!accepting_ || callbacks_.empty()) {
906 void AsyncServerSocket::dispatchSocket(int socket,
907 SocketAddress&& address) {
908 uint32_t startingIndex = callbackIndex_;
910 // Short circuit if the callback is in the primary EventBase thread
912 CallbackInfo *info = nextCallback();
913 if (info->eventBase == nullptr) {
914 info->callback->connectionAccepted(socket, address);
918 const SocketAddress addr(address);
919 // Create a message to send over the notification queue
921 msg.type = MessageType::MSG_NEW_CONN;
922 msg.address = std::move(address);
925 // Loop until we find a free queue to write to
927 if (info->consumer->getQueue()->tryPutMessageNoThrow(std::move(msg))) {
928 if (connectionEventCallback_) {
929 connectionEventCallback_->onConnectionEnqueuedForAcceptorCallback(
937 // We couldn't add to queue. Fall through to below
939 ++numDroppedConnections_;
940 if (acceptRateAdjustSpeed_ > 0) {
941 // aggressively decrease accept rate when in trouble
942 static const double kAcceptRateDecreaseSpeed = 0.1;
943 acceptRate_ *= 1 - kAcceptRateDecreaseSpeed;
947 if (callbackIndex_ == startingIndex) {
948 // The notification queue was full
949 // We can't really do anything at this point other than close the socket.
951 // This should only happen if a user's service is behaving extremely
952 // badly and none of the EventBase threads are looping fast enough to
953 // process the incoming connections. If the service is overloaded, it
954 // should use pauseAccepting() to temporarily back off accepting new
955 // connections, before they reach the point where their threads can't
956 // even accept new messages.
957 LOG(ERROR) << "failed to dispatch newly accepted socket:"
958 << " all accept callback queues are full";
960 if (connectionEventCallback_) {
961 connectionEventCallback_->onConnectionDropped(socket, addr);
966 info = nextCallback();
970 void AsyncServerSocket::dispatchError(const char *msgstr, int errnoValue) {
971 uint32_t startingIndex = callbackIndex_;
972 CallbackInfo *info = nextCallback();
974 // Create a message to send over the notification queue
976 msg.type = MessageType::MSG_ERROR;
977 msg.err = errnoValue;
978 msg.msg = std::move(msgstr);
981 // Short circuit if the callback is in the primary EventBase thread
982 if (info->eventBase == nullptr) {
983 std::runtime_error ex(
984 std::string(msgstr) + folly::to<std::string>(errnoValue));
985 info->callback->acceptError(ex);
989 if (info->consumer->getQueue()->tryPutMessageNoThrow(std::move(msg))) {
992 // Fall through and try another callback
994 if (callbackIndex_ == startingIndex) {
995 // The notification queues for all of the callbacks were full.
996 // We can't really do anything at this point.
997 LOG(ERROR) << "failed to dispatch accept error: all accept callback "
998 "queues are full: error msg: " <<
999 msg.msg.c_str() << errnoValue;
1002 info = nextCallback();
1006 void AsyncServerSocket::enterBackoff() {
1007 // If this is the first time we have entered the backoff state,
1008 // allocate backoffTimeout_.
1009 if (backoffTimeout_ == nullptr) {
1011 backoffTimeout_ = new BackoffTimeout(this);
1012 } catch (const std::bad_alloc&) {
1013 // Man, we couldn't even allocate the timer to re-enable accepts.
1014 // We must be in pretty bad shape. Don't pause accepting for now,
1015 // since we won't be able to re-enable ourselves later.
1016 LOG(ERROR) << "failed to allocate AsyncServerSocket backoff"
1017 << " timer; unable to temporarly pause accepting";
1018 if (connectionEventCallback_) {
1019 connectionEventCallback_->onBackoffError();
1025 // For now, we simply pause accepting for 1 second.
1027 // We could add some smarter backoff calculation here in the future. (e.g.,
1028 // start sleeping for longer if we keep hitting the backoff frequently.)
1029 // Typically the user needs to figure out why the server is overloaded and
1030 // fix it in some other way, though. The backoff timer is just a simple
1031 // mechanism to try and give the connection processing code a little bit of
1032 // breathing room to catch up, and to avoid just spinning and failing to
1033 // accept over and over again.
1034 const uint32_t timeoutMS = 1000;
1035 if (!backoffTimeout_->scheduleTimeout(timeoutMS)) {
1036 LOG(ERROR) << "failed to schedule AsyncServerSocket backoff timer;"
1037 << "unable to temporarly pause accepting";
1038 if (connectionEventCallback_) {
1039 connectionEventCallback_->onBackoffError();
1044 // The backoff timer is scheduled to re-enable accepts.
1045 // Go ahead and disable accepts for now. We leave accepting_ set to true,
1046 // since that tracks the desired state requested by the user.
1047 for (auto& handler : sockets_) {
1048 handler.unregisterHandler();
1050 if (connectionEventCallback_) {
1051 connectionEventCallback_->onBackoffStarted();
1055 void AsyncServerSocket::backoffTimeoutExpired() {
1056 // accepting_ should still be true.
1057 // If pauseAccepting() was called while in the backoff state it will cancel
1058 // the backoff timeout.
1060 // We can't be detached from the EventBase without being paused
1061 assert(eventBase_ != nullptr);
1062 eventBase_->dcheckIsInEventBaseThread();
1064 // If all of the callbacks were removed, we shouldn't re-enable accepts
1065 if (callbacks_.empty()) {
1066 if (connectionEventCallback_) {
1067 connectionEventCallback_->onBackoffEnded();
1072 // Register the handler.
1073 for (auto& handler : sockets_) {
1074 if (!handler.registerHandler(
1075 EventHandler::READ | EventHandler::PERSIST)) {
1076 // We're hosed. We could just re-schedule backoffTimeout_ to
1077 // re-try again after a little bit. However, we don't want to
1078 // loop retrying forever if we can't re-enable accepts. Just
1079 // abort the entire program in this state; things are really bad
1080 // and restarting the entire server is probably the best remedy.
1082 << "failed to re-enable AsyncServerSocket accepts after backoff; "
1087 if (connectionEventCallback_) {
1088 connectionEventCallback_->onBackoffEnded();
1092 } // namespace folly