2 * Copyright 2014 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/io/async/EventBase.h>
24 #include <folly/io/async/NotificationQueue.h>
25 #include <folly/SocketAddress.h>
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <netinet/tcp.h>
37 const uint32_t AsyncServerSocket::kDefaultMaxAcceptAtOnce;
38 const uint32_t AsyncServerSocket::kDefaultCallbackAcceptAtOnce;
39 const uint32_t AsyncServerSocket::kDefaultMaxMessagesInQueue;
41 int setCloseOnExec(int fd, int value) {
42 // Read the current flags
43 int old_flags = fcntl(fd, F_GETFD, 0);
45 // If reading the flags failed, return error indication now
49 // Set just the flag we want to set
52 new_flags = old_flags | FD_CLOEXEC;
54 new_flags = old_flags & ~FD_CLOEXEC;
56 // Store modified flag word in the descriptor
57 return fcntl(fd, F_SETFD, new_flags);
60 void AsyncServerSocket::RemoteAcceptor::start(
61 EventBase* eventBase, uint32_t maxAtOnce, uint32_t maxInQueue) {
62 setMaxReadAtOnce(maxAtOnce);
63 queue_.setMaxQueueSize(maxInQueue);
65 if (!eventBase->runInEventBaseThread([=](){
66 callback_->acceptStarted();
67 this->startConsuming(eventBase, &queue_);
69 throw std::invalid_argument("unable to start waiting on accept "
70 "notification queue in the specified "
75 void AsyncServerSocket::RemoteAcceptor::stop(
76 EventBase* eventBase, AcceptCallback* callback) {
77 if (!eventBase->runInEventBaseThread([=](){
78 callback->acceptStopped();
81 throw std::invalid_argument("unable to start waiting on accept "
82 "notification queue in the specified "
87 void AsyncServerSocket::RemoteAcceptor::messageAvailable(
91 case MessageType::MSG_NEW_CONN:
93 callback_->connectionAccepted(msg.fd, msg.address);
96 case MessageType::MSG_ERROR:
98 std::runtime_error ex(msg.msg);
99 callback_->acceptError(ex);
104 LOG(ERROR) << "invalid accept notification message type "
106 std::runtime_error ex(
107 "received invalid accept notification message type");
108 callback_->acceptError(ex);
114 * AsyncServerSocket::BackoffTimeout
116 class AsyncServerSocket::BackoffTimeout : public AsyncTimeout {
118 BackoffTimeout(AsyncServerSocket* socket)
119 : AsyncTimeout(socket->getEventBase()),
122 virtual void timeoutExpired() noexcept {
123 socket_->backoffTimeoutExpired();
127 AsyncServerSocket* socket_;
131 * AsyncServerSocket methods
134 AsyncServerSocket::AsyncServerSocket(EventBase* eventBase)
135 : eventBase_(eventBase),
137 maxAcceptAtOnce_(kDefaultMaxAcceptAtOnce),
138 maxNumMsgsInQueue_(kDefaultMaxMessagesInQueue),
139 acceptRateAdjustSpeed_(0),
141 lastAccepTimestamp_(std::chrono::steady_clock::now()),
142 numDroppedConnections_(0),
144 backoffTimeout_(nullptr),
146 keepAliveEnabled_(true),
148 shutdownSocketSet_(nullptr) {
151 void AsyncServerSocket::setShutdownSocketSet(ShutdownSocketSet* newSS) {
152 if (shutdownSocketSet_ == newSS) {
155 if (shutdownSocketSet_) {
156 for (auto& h : sockets_) {
157 shutdownSocketSet_->remove(h.socket_);
160 shutdownSocketSet_ = newSS;
161 if (shutdownSocketSet_) {
162 for (auto& h : sockets_) {
163 shutdownSocketSet_->add(h.socket_);
168 AsyncServerSocket::~AsyncServerSocket() {
169 assert(callbacks_.empty());
172 int AsyncServerSocket::stopAccepting(int shutdownFlags) {
174 for (auto& handler : sockets_) {
175 VLOG(10) << "AsyncServerSocket::stopAccepting " << this <<
178 assert(eventBase_ == nullptr || eventBase_->isInEventBaseThread());
180 // When destroy is called, unregister and close the socket immediately
183 for (auto& handler : sockets_) {
184 handler.unregisterHandler();
185 if (shutdownSocketSet_) {
186 shutdownSocketSet_->close(handler.socket_);
187 } else if (shutdownFlags >= 0) {
188 result = ::shutdown(handler.socket_, shutdownFlags);
189 pendingCloseSockets_.push_back(handler.socket_);
191 ::close(handler.socket_);
196 // Destroy the backoff timout. This will cancel it if it is running.
197 delete backoffTimeout_;
198 backoffTimeout_ = nullptr;
200 // Close all of the callback queues to notify them that they are being
201 // destroyed. No one should access the AsyncServerSocket any more once
202 // destroy() is called. However, clear out callbacks_ before invoking the
203 // accept callbacks just in case. This will potentially help us detect the
204 // bug if one of the callbacks calls addAcceptCallback() or
205 // removeAcceptCallback().
206 std::vector<CallbackInfo> callbacksCopy;
207 callbacks_.swap(callbacksCopy);
208 for (std::vector<CallbackInfo>::iterator it = callbacksCopy.begin();
209 it != callbacksCopy.end();
211 it->consumer->stop(it->eventBase, it->callback);
217 void AsyncServerSocket::destroy() {
219 for (auto s: pendingCloseSockets_) {
222 // Then call DelayedDestruction::destroy() to take care of
223 // whether or not we need immediate or delayed destruction
224 DelayedDestruction::destroy();
227 void AsyncServerSocket::attachEventBase(EventBase *eventBase) {
228 assert(eventBase_ == nullptr);
229 assert(eventBase->isInEventBaseThread());
231 eventBase_ = eventBase;
232 for (auto& handler : sockets_) {
233 handler.attachEventBase(eventBase);
237 void AsyncServerSocket::detachEventBase() {
238 assert(eventBase_ != nullptr);
239 assert(eventBase_->isInEventBaseThread());
242 eventBase_ = nullptr;
243 for (auto& handler : sockets_) {
244 handler.detachEventBase();
248 void AsyncServerSocket::useExistingSockets(const std::vector<int>& fds) {
249 assert(eventBase_ == nullptr || eventBase_->isInEventBaseThread());
251 if (sockets_.size() > 0) {
252 throw std::invalid_argument(
253 "cannot call useExistingSocket() on a "
254 "AsyncServerSocket that already has a socket");
258 // Set addressFamily_ from this socket.
259 // Note that the socket may not have been bound yet, but
260 // setFromLocalAddress() will still work and get the correct address family.
261 // We will update addressFamily_ again anyway if bind() is called later.
262 SocketAddress address;
263 address.setFromLocalAddress(fd);
267 ServerEventHandler(eventBase_, fd, this, address.getFamily()));
268 sockets_.back().changeHandlerFD(fd);
272 void AsyncServerSocket::useExistingSocket(int fd) {
273 useExistingSockets({fd});
276 void AsyncServerSocket::bindSocket(
278 const SocketAddress& address,
279 bool isExistingSocket) {
280 sockaddr_storage addrStorage;
281 address.getAddress(&addrStorage);
282 sockaddr* saddr = reinterpret_cast<sockaddr*>(&addrStorage);
283 if (::bind(fd, saddr, address.getActualSize()) != 0) {
284 if (!isExistingSocket) {
287 folly::throwSystemError(errno,
288 "failed to bind to async server socket: " +
292 // If we just created this socket, update the EventHandler and set socket_
293 if (!isExistingSocket) {
295 ServerEventHandler(eventBase_, fd, this, address.getFamily()));
299 void AsyncServerSocket::bind(const SocketAddress& address) {
300 assert(eventBase_ == nullptr || eventBase_->isInEventBaseThread());
302 // useExistingSocket() may have been called to initialize socket_ already.
303 // However, in the normal case we need to create a new socket now.
304 // Don't set socket_ yet, so that socket_ will remain uninitialized if an
307 if (sockets_.size() == 0) {
308 fd = createSocket(address.getFamily());
309 } else if (sockets_.size() == 1) {
310 if (address.getFamily() != sockets_[0].addressFamily_) {
311 throw std::invalid_argument(
312 "Attempted to bind address to socket with "
313 "different address family");
315 fd = sockets_[0].socket_;
317 throw std::invalid_argument(
318 "Attempted to bind to multiple fds");
321 bindSocket(fd, address, !sockets_.empty());
324 void AsyncServerSocket::bind(
325 const std::vector<IPAddress>& ipAddresses,
327 if (ipAddresses.empty()) {
328 throw std::invalid_argument("No ip addresses were provided");
330 if (!sockets_.empty()) {
331 throw std::invalid_argument("Cannot call bind on a AsyncServerSocket "
332 "that already has a socket.");
335 for (const IPAddress& ipAddress : ipAddresses) {
336 SocketAddress address(ipAddress.toFullyQualified(), port);
337 int fd = createSocket(address.getFamily());
339 bindSocket(fd, address, false);
341 if (sockets_.size() == 0) {
342 throw std::runtime_error(
343 "did not bind any async server socket for port and addresses");
347 void AsyncServerSocket::bind(uint16_t port) {
348 struct addrinfo hints, *res, *res0;
349 char sport[sizeof("65536")];
351 memset(&hints, 0, sizeof(hints));
352 hints.ai_family = AF_UNSPEC;
353 hints.ai_socktype = SOCK_STREAM;
354 hints.ai_flags = AI_PASSIVE;
355 snprintf(sport, sizeof(sport), "%u", port);
357 if (getaddrinfo(nullptr, sport, &hints, &res0)) {
358 throw std::invalid_argument(
359 "Attempted to bind address to socket with "
363 folly::ScopeGuard guard = folly::makeGuard([&]{
367 auto setupAddress = [&] (struct addrinfo* res) {
368 int s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
369 // IPv6/IPv4 may not be supported by the kernel
370 if (s < 0 && errno == EAFNOSUPPORT) {
382 if (res->ai_family == AF_INET6) {
384 CHECK(0 == setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
385 &v6only, sizeof(v6only)));
388 SocketAddress address;
389 address.setFromLocalAddress(s);
392 ServerEventHandler(eventBase_, s, this, address.getFamily()));
394 // Bind to the socket
395 if (::bind(s, res->ai_addr, res->ai_addrlen) != 0) {
396 folly::throwSystemError(
398 "failed to bind to async server socket for port");
402 // Prefer AF_INET6 addresses. RFC 3484 mandates that getaddrinfo
403 // should return IPv6 first and then IPv4 addresses, but glibc's
404 // getaddrinfo(nullptr) with AI_PASSIVE returns:
405 // - 0.0.0.0 (IPv4-only)
406 // - :: (IPv6+IPv4) in this order
407 // See: https://sourceware.org/bugzilla/show_bug.cgi?id=9981
408 for (res = res0; res; res = res->ai_next) {
409 if (res->ai_family == AF_INET6) {
414 for (res = res0; res; res = res->ai_next) {
415 if (res->ai_family != AF_INET6) {
420 if (sockets_.size() == 0) {
421 throw std::runtime_error(
422 "did not bind any async server socket for port");
426 void AsyncServerSocket::listen(int backlog) {
427 assert(eventBase_ == nullptr || eventBase_->isInEventBaseThread());
430 for (auto& handler : sockets_) {
431 if (::listen(handler.socket_, backlog) == -1) {
432 folly::throwSystemError(errno,
433 "failed to listen on async server socket");
438 void AsyncServerSocket::getAddress(SocketAddress* addressReturn) const {
439 CHECK(sockets_.size() >= 1);
440 VLOG_IF(2, sockets_.size() > 1)
441 << "Warning: getAddress() called and multiple addresses available ("
442 << sockets_.size() << "). Returning only the first one.";
444 addressReturn->setFromLocalAddress(sockets_[0].socket_);
447 std::vector<SocketAddress> AsyncServerSocket::getAddresses()
449 CHECK(sockets_.size() >= 1);
450 auto tsaVec = std::vector<SocketAddress>(sockets_.size());
451 auto tsaIter = tsaVec.begin();
452 for (const auto& socket : sockets_) {
453 (tsaIter++)->setFromLocalAddress(socket.socket_);
458 void AsyncServerSocket::addAcceptCallback(AcceptCallback *callback,
459 EventBase *eventBase,
460 uint32_t maxAtOnce) {
461 assert(eventBase_ == nullptr || eventBase_->isInEventBaseThread());
463 // If this is the first accept callback and we are supposed to be accepting,
464 // start accepting once the callback is installed.
465 bool runStartAccepting = accepting_ && callbacks_.empty();
468 eventBase = eventBase_; // Run in AsyncServerSocket's eventbase
471 callbacks_.push_back(CallbackInfo(callback, eventBase));
473 // Start the remote acceptor.
475 // It would be nice if we could avoid starting the remote acceptor if
476 // eventBase == eventBase_. However, that would cause issues if
477 // detachEventBase() and attachEventBase() were ever used to change the
478 // primary EventBase for the server socket. Therefore we require the caller
479 // to specify a nullptr EventBase if they want to ensure that the callback is
480 // always invoked in the primary EventBase, and to be able to invoke that
481 // callback more efficiently without having to use a notification queue.
482 RemoteAcceptor* acceptor = nullptr;
484 acceptor = new RemoteAcceptor(callback);
485 acceptor->start(eventBase, maxAtOnce, maxNumMsgsInQueue_);
487 callbacks_.pop_back();
491 callbacks_.back().consumer = acceptor;
493 // If this is the first accept callback and we are supposed to be accepting,
495 if (runStartAccepting) {
500 void AsyncServerSocket::removeAcceptCallback(AcceptCallback *callback,
501 EventBase *eventBase) {
502 assert(eventBase_ == nullptr || eventBase_->isInEventBaseThread());
504 // Find the matching AcceptCallback.
505 // We just do a simple linear search; we don't expect removeAcceptCallback()
506 // to be called frequently, and we expect there to only be a small number of
508 std::vector<CallbackInfo>::iterator it = callbacks_.begin();
511 if (it == callbacks_.end()) {
512 throw std::runtime_error("AsyncServerSocket::removeAcceptCallback(): "
513 "accept callback not found");
515 if (it->callback == callback &&
516 (it->eventBase == eventBase || eventBase == nullptr)) {
523 // Remove this callback from callbacks_.
525 // Do this before invoking the acceptStopped() callback, in case
526 // acceptStopped() invokes one of our methods that examines callbacks_.
528 // Save a copy of the CallbackInfo first.
529 CallbackInfo info(*it);
530 callbacks_.erase(it);
531 if (n < callbackIndex_) {
532 // We removed an element before callbackIndex_. Move callbackIndex_ back
533 // one step, since things after n have been shifted back by 1.
536 // We removed something at or after callbackIndex_.
537 // If we removed the last element and callbackIndex_ was pointing at it,
538 // we need to reset callbackIndex_ to 0.
539 if (callbackIndex_ >= callbacks_.size()) {
544 info.consumer->stop(info.eventBase, info.callback);
546 // If we are supposed to be accepting but the last accept callback
547 // was removed, unregister for events until a callback is added.
548 if (accepting_ && callbacks_.empty()) {
549 for (auto& handler : sockets_) {
550 handler.unregisterHandler();
555 void AsyncServerSocket::startAccepting() {
556 assert(eventBase_ == nullptr || eventBase_->isInEventBaseThread());
559 if (callbacks_.empty()) {
560 // We can't actually begin accepting if no callbacks are defined.
561 // Wait until a callback is added to start accepting.
565 for (auto& handler : sockets_) {
566 if (!handler.registerHandler(
567 EventHandler::READ | EventHandler::PERSIST)) {
568 throw std::runtime_error("failed to register for accept events");
573 void AsyncServerSocket::pauseAccepting() {
574 assert(eventBase_ == nullptr || eventBase_->isInEventBaseThread());
576 for (auto& handler : sockets_) {
577 handler. unregisterHandler();
580 // If we were in the accept backoff state, disable the backoff timeout
581 if (backoffTimeout_) {
582 backoffTimeout_->cancelTimeout();
586 int AsyncServerSocket::createSocket(int family) {
587 int fd = socket(family, SOCK_STREAM, 0);
589 folly::throwSystemError(errno, "error creating async server socket");
601 void AsyncServerSocket::setupSocket(int fd) {
602 // Get the address family
603 SocketAddress address;
604 address.setFromLocalAddress(fd);
605 auto family = address.getFamily();
607 // Put the socket in non-blocking mode
608 if (fcntl(fd, F_SETFL, O_NONBLOCK) != 0) {
609 folly::throwSystemError(errno,
610 "failed to put socket in non-blocking mode");
613 // Set reuseaddr to avoid 2MSL delay on server restart
615 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) != 0) {
616 // This isn't a fatal error; just log an error message and continue
617 LOG(ERROR) << "failed to set SO_REUSEADDR on async server socket " << errno;
620 // Set reuseport to support multiple accept threads
622 if (reusePortEnabled_ &&
623 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(int)) != 0) {
624 LOG(ERROR) << "failed to set SO_REUSEPORT on async server socket "
626 folly::throwSystemError(errno,
627 "failed to bind to async server socket: " +
631 // Set keepalive as desired
632 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
633 (keepAliveEnabled_) ? &one : &zero, sizeof(int)) != 0) {
634 LOG(ERROR) << "failed to set SO_KEEPALIVE on async server socket: " <<
638 // Setup FD_CLOEXEC flag
640 (-1 == folly::setCloseOnExec(fd, closeOnExec_))) {
641 LOG(ERROR) << "failed to set FD_CLOEXEC on async server socket: " <<
645 // Set TCP nodelay if available, MAC OS X Hack
646 // See http://lists.danga.com/pipermail/memcached/2005-March/001240.html
648 if (family != AF_UNIX) {
649 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) != 0) {
650 // This isn't a fatal error; just log an error message and continue
651 LOG(ERROR) << "failed to set TCP_NODELAY on async server socket: " <<
657 if (shutdownSocketSet_) {
658 shutdownSocketSet_->add(fd);
662 void AsyncServerSocket::handlerReady(
663 uint16_t events, int fd, sa_family_t addressFamily) noexcept {
664 assert(!callbacks_.empty());
665 DestructorGuard dg(this);
667 // Only accept up to maxAcceptAtOnce_ connections at a time,
668 // to avoid starving other I/O handlers using this EventBase.
669 for (uint32_t n = 0; n < maxAcceptAtOnce_; ++n) {
670 SocketAddress address;
672 sockaddr_storage addrStorage;
673 socklen_t addrLen = sizeof(addrStorage);
674 sockaddr* saddr = reinterpret_cast<sockaddr*>(&addrStorage);
676 // In some cases, accept() doesn't seem to update these correctly.
677 saddr->sa_family = addressFamily;
678 if (addressFamily == AF_UNIX) {
679 addrLen = sizeof(struct sockaddr_un);
682 // Accept a new client socket
684 int clientSocket = accept4(fd, saddr, &addrLen, SOCK_NONBLOCK);
686 int clientSocket = accept(fd, saddr, &addrLen);
689 address.setFromSockaddr(saddr, addrLen);
691 std::chrono::time_point<std::chrono::steady_clock> nowMs =
692 std::chrono::steady_clock::now();
693 int64_t timeSinceLastAccept = std::max(
695 nowMs.time_since_epoch().count() -
696 lastAccepTimestamp_.time_since_epoch().count());
697 lastAccepTimestamp_ = nowMs;
698 if (acceptRate_ < 1) {
699 acceptRate_ *= 1 + acceptRateAdjustSpeed_ * timeSinceLastAccept;
700 if (acceptRate_ >= 1) {
702 } else if (rand() > acceptRate_ * RAND_MAX) {
703 ++numDroppedConnections_;
704 if (clientSocket >= 0) {
705 ::close(clientSocket);
711 if (clientSocket < 0) {
712 if (errno == EAGAIN) {
713 // No more sockets to accept right now.
714 // Check for this code first, since it's the most common.
716 } else if (errno == EMFILE || errno == ENFILE) {
717 // We're out of file descriptors. Perhaps we're accepting connections
718 // too quickly. Pause accepting briefly to back off and give the server
719 // a chance to recover.
720 LOG(ERROR) << "accept failed: out of file descriptors; entering accept "
724 // Dispatch the error message
725 dispatchError("accept() failed", errno);
727 dispatchError("accept() failed", errno);
732 #ifndef SOCK_NONBLOCK
733 // Explicitly set the new connection to non-blocking mode
734 if (fcntl(clientSocket, F_SETFL, O_NONBLOCK) != 0) {
735 ::close(clientSocket);
736 dispatchError("failed to set accepted socket to non-blocking mode",
742 // Inform the callback about the new connection
743 dispatchSocket(clientSocket, std::move(address));
745 // If we aren't accepting any more, break out of the loop
746 if (!accepting_ || callbacks_.empty()) {
752 void AsyncServerSocket::dispatchSocket(int socket,
753 SocketAddress&& address) {
754 uint32_t startingIndex = callbackIndex_;
756 // Short circuit if the callback is in the primary EventBase thread
758 CallbackInfo *info = nextCallback();
759 if (info->eventBase == nullptr) {
760 info->callback->connectionAccepted(socket, address);
764 // Create a message to send over the notification queue
766 msg.type = MessageType::MSG_NEW_CONN;
767 msg.address = std::move(address);
770 // Loop until we find a free queue to write to
772 if (info->consumer->getQueue()->tryPutMessageNoThrow(std::move(msg))) {
777 // We couldn't add to queue. Fall through to below
779 ++numDroppedConnections_;
780 if (acceptRateAdjustSpeed_ > 0) {
781 // aggressively decrease accept rate when in trouble
782 static const double kAcceptRateDecreaseSpeed = 0.1;
783 acceptRate_ *= 1 - kAcceptRateDecreaseSpeed;
787 if (callbackIndex_ == startingIndex) {
788 // The notification queue was full
789 // We can't really do anything at this point other than close the socket.
791 // This should only happen if a user's service is behaving extremely
792 // badly and none of the EventBase threads are looping fast enough to
793 // process the incoming connections. If the service is overloaded, it
794 // should use pauseAccepting() to temporarily back off accepting new
795 // connections, before they reach the point where their threads can't
796 // even accept new messages.
797 LOG(ERROR) << "failed to dispatch newly accepted socket:"
798 << " all accept callback queues are full";
803 info = nextCallback();
807 void AsyncServerSocket::dispatchError(const char *msgstr, int errnoValue) {
808 uint32_t startingIndex = callbackIndex_;
809 CallbackInfo *info = nextCallback();
811 // Create a message to send over the notification queue
813 msg.type = MessageType::MSG_ERROR;
814 msg.err = errnoValue;
815 msg.msg = std::move(msgstr);
818 // Short circuit if the callback is in the primary EventBase thread
819 if (info->eventBase == nullptr) {
820 std::runtime_error ex(
821 std::string(msgstr) + folly::to<std::string>(errnoValue));
822 info->callback->acceptError(ex);
826 if (info->consumer->getQueue()->tryPutMessageNoThrow(std::move(msg))) {
829 // Fall through and try another callback
831 if (callbackIndex_ == startingIndex) {
832 // The notification queues for all of the callbacks were full.
833 // We can't really do anything at this point.
834 LOG(ERROR) << "failed to dispatch accept error: all accept callback "
835 "queues are full: error msg: " <<
836 msg.msg.c_str() << errnoValue;
839 info = nextCallback();
843 void AsyncServerSocket::enterBackoff() {
844 // If this is the first time we have entered the backoff state,
845 // allocate backoffTimeout_.
846 if (backoffTimeout_ == nullptr) {
848 backoffTimeout_ = new BackoffTimeout(this);
849 } catch (const std::bad_alloc& ex) {
850 // Man, we couldn't even allocate the timer to re-enable accepts.
851 // We must be in pretty bad shape. Don't pause accepting for now,
852 // since we won't be able to re-enable ourselves later.
853 LOG(ERROR) << "failed to allocate AsyncServerSocket backoff"
854 << " timer; unable to temporarly pause accepting";
859 // For now, we simply pause accepting for 1 second.
861 // We could add some smarter backoff calculation here in the future. (e.g.,
862 // start sleeping for longer if we keep hitting the backoff frequently.)
863 // Typically the user needs to figure out why the server is overloaded and
864 // fix it in some other way, though. The backoff timer is just a simple
865 // mechanism to try and give the connection processing code a little bit of
866 // breathing room to catch up, and to avoid just spinning and failing to
867 // accept over and over again.
868 const uint32_t timeoutMS = 1000;
869 if (!backoffTimeout_->scheduleTimeout(timeoutMS)) {
870 LOG(ERROR) << "failed to schedule AsyncServerSocket backoff timer;"
871 << "unable to temporarly pause accepting";
875 // The backoff timer is scheduled to re-enable accepts.
876 // Go ahead and disable accepts for now. We leave accepting_ set to true,
877 // since that tracks the desired state requested by the user.
878 for (auto& handler : sockets_) {
879 handler.unregisterHandler();
883 void AsyncServerSocket::backoffTimeoutExpired() {
884 // accepting_ should still be true.
885 // If pauseAccepting() was called while in the backoff state it will cancel
886 // the backoff timeout.
888 // We can't be detached from the EventBase without being paused
889 assert(eventBase_ != nullptr && eventBase_->isInEventBaseThread());
891 // If all of the callbacks were removed, we shouldn't re-enable accepts
892 if (callbacks_.empty()) {
896 // Register the handler.
897 for (auto& handler : sockets_) {
898 if (!handler.registerHandler(
899 EventHandler::READ | EventHandler::PERSIST)) {
900 // We're hosed. We could just re-schedule backoffTimeout_ to
901 // re-try again after a little bit. However, we don't want to
902 // loop retrying forever if we can't re-enable accepts. Just
903 // abort the entire program in this state; things are really bad
904 // and restarting the entire server is probably the best remedy.
906 << "failed to re-enable AsyncServerSocket accepts after backoff; "