proxygen
|
#include <AsyncServerSocket.h>
Classes | |
class | AcceptCallback |
class | BackoffTimeout |
struct | CallbackInfo |
class | ConnectionEventCallback |
struct | QueueMessage |
class | RemoteAcceptor |
struct | ServerEventHandler |
Public Types | |
typedef std::unique_ptr< AsyncServerSocket, Destructor > | UniquePtr |
Public Member Functions | |
AsyncServerSocket (AsyncServerSocket &&)=delete | |
AsyncServerSocket (EventBase *eventBase=nullptr) | |
void | setShutdownSocketSet (const std::weak_ptr< ShutdownSocketSet > &wNewSS) |
void | destroy () override |
void | attachEventBase (EventBase *eventBase) |
void | detachEventBase () |
EventBase * | getEventBase () const override |
void | useExistingSocket (int fd) |
void | useExistingSockets (const std::vector< int > &fds) |
std::vector< int > | getSockets () const |
int | getSocket () const |
bool | setZeroCopy (bool enable) |
virtual void | bind (const SocketAddress &address) |
virtual void | bind (const std::vector< IPAddress > &ipAddresses, uint16_t port) |
virtual void | bind (uint16_t port) |
void | getAddress (SocketAddress *addressReturn) const override |
SocketAddress | getAddress () const |
std::vector< SocketAddress > | getAddresses () const |
virtual void | listen (int backlog) |
virtual void | addAcceptCallback (AcceptCallback *callback, EventBase *eventBase, uint32_t maxAtOnce=kDefaultCallbackAcceptAtOnce) |
void | removeAcceptCallback (AcceptCallback *callback, EventBase *eventBase) |
virtual void | startAccepting () |
void | pauseAccepting () |
int | stopAccepting (int shutdownFlags=-1) |
uint32_t | getMaxAcceptAtOnce () const |
void | setMaxAcceptAtOnce (uint32_t numConns) |
uint32_t | getMaxNumMessagesInQueue () const |
void | setMaxNumMessagesInQueue (uint32_t num) |
double | getAcceptRateAdjustSpeed () const |
void | setAcceptRateAdjustSpeed (double speed) |
void | setTosReflect (bool enable) |
bool | getTosReflect () |
std::size_t | getNumDroppedConnections () const |
int64_t | getNumPendingMessagesInQueue () const |
void | setKeepAliveEnabled (bool enabled) |
bool | getKeepAliveEnabled () const |
void | setReusePortEnabled (bool enabled) |
bool | getReusePortEnabled_ () const |
void | setCloseOnExec (bool closeOnExec) |
bool | getCloseOnExec () const |
void | setTFOEnabled (bool enabled, uint32_t maxTFOQueueSize) |
void | disableTransparentTls () |
bool | getAccepting () const |
void | setConnectionEventCallback (ConnectionEventCallback *const connectionEventCallback) |
ConnectionEventCallback * | getConnectionEventCallback () const |
Public Member Functions inherited from folly::DelayedDestruction | |
bool | getDestroyPending () const |
Public Member Functions inherited from folly::DelayedDestructionBase | |
virtual | ~DelayedDestructionBase ()=default |
Public Member Functions inherited from folly::AsyncSocketBase | |
virtual | ~AsyncSocketBase ()=default |
Static Public Member Functions | |
static std::shared_ptr< AsyncServerSocket > | newSocket (EventBase *evb=nullptr) |
Static Public Attributes | |
static const uint32_t | kDefaultMaxAcceptAtOnce = 30 |
static const uint32_t | kDefaultCallbackAcceptAtOnce = 5 |
static const uint32_t | kDefaultMaxMessagesInQueue = 1024 |
Protected Member Functions | |
~AsyncServerSocket () override | |
Protected Member Functions inherited from folly::DelayedDestruction | |
~DelayedDestruction () override=default | |
DelayedDestruction () | |
Protected Member Functions inherited from folly::DelayedDestructionBase | |
DelayedDestructionBase () | |
uint32_t | getDestructorGuardCount () const |
Private Types | |
enum | MessageType { MessageType::MSG_NEW_CONN = 0, MessageType::MSG_ERROR = 1 } |
Private Member Functions | |
virtual void | handlerReady (uint16_t events, int socket, sa_family_t family) noexcept |
int | createSocket (int family) |
void | setupSocket (int fd, int family) |
void | bindSocket (int fd, const SocketAddress &address, bool isExistingSocket) |
void | dispatchSocket (int socket, SocketAddress &&address) |
void | dispatchError (const char *msg, int errnoValue) |
void | enterBackoff () |
void | backoffTimeoutExpired () |
CallbackInfo * | nextCallback () |
Private Attributes | |
EventBase * | eventBase_ |
std::vector< ServerEventHandler > | sockets_ |
std::vector< int > | pendingCloseSockets_ |
bool | accepting_ |
uint32_t | maxAcceptAtOnce_ |
uint32_t | maxNumMsgsInQueue_ |
double | acceptRateAdjustSpeed_ |
double | acceptRate_ |
std::chrono::time_point< std::chrono::steady_clock > | lastAccepTimestamp_ |
std::size_t | numDroppedConnections_ |
uint32_t | callbackIndex_ |
BackoffTimeout * | backoffTimeout_ |
std::vector< CallbackInfo > | callbacks_ |
bool | keepAliveEnabled_ |
bool | reusePortEnabled_ {false} |
bool | closeOnExec_ |
bool | tfo_ {false} |
bool | noTransparentTls_ {false} |
uint32_t | tfoMaxQueueSize_ {0} |
std::weak_ptr< ShutdownSocketSet > | wShutdownSocketSet_ |
ConnectionEventCallback * | connectionEventCallback_ {nullptr} |
bool | tosReflect_ {false} |
A listening socket that asynchronously informs a callback whenever a new connection has been accepted.
Unlike most async interfaces that always invoke their callback in the same EventBase thread, AsyncServerSocket is unusual in that it can distribute the callbacks across multiple EventBase threads.
This supports a common use case for network servers to distribute incoming connections across a number of EventBase threads. (Servers typically run with one EventBase thread per CPU.)
Despite being able to invoke callbacks in multiple EventBase threads, AsyncServerSocket still has one "primary" EventBase. Operations that modify the AsyncServerSocket state may only be performed from the primary EventBase thread.
Definition at line 65 of file AsyncServerSocket.h.
typedef std::unique_ptr<AsyncServerSocket, Destructor> folly::AsyncServerSocket::UniquePtr |
Definition at line 67 of file AsyncServerSocket.h.
|
strongprivate |
Enumerator | |
---|---|
MSG_NEW_CONN | |
MSG_ERROR |
Definition at line 742 of file AsyncServerSocket.h.
|
delete |
Referenced by newSocket().
Create a new AsyncServerSocket with the specified EventBase.
eventBase | The EventBase to use for driving the asynchronous I/O. If this parameter is nullptr, attachEventBase() must be called before this socket can begin accepting connections. |
Definition at line 162 of file AsyncServerSocket.cpp.
References disableTransparentTls().
|
overrideprotected |
Protected destructor.
Invoke destroy() instead to destroy the AsyncServerSocket.
Definition at line 203 of file AsyncServerSocket.cpp.
References callbacks_.
Referenced by getConnectionEventCallback().
|
virtual |
Add an AcceptCallback.
When a new socket is accepted, one of the AcceptCallbacks will be invoked with the new socket. The AcceptCallbacks are invoked in a round-robin fashion. This allows the accepted sockets to be distributed among a pool of threads, each running its own EventBase object. This is a common model, since most asynchronous-style servers typically run one EventBase thread per CPU.
The EventBase object associated with each AcceptCallback must be running its loop. If the EventBase loop is not running, sockets will still be scheduled for the callback, but the callback cannot actually get invoked until the loop runs.
This method must be invoked from the AsyncServerSocket's primary EventBase thread.
Note that startAccepting() must be called on the AsyncServerSocket to cause it to actually start accepting sockets once callbacks have been installed.
callback | The callback to invoke. |
eventBase | The EventBase to use to invoke the callback. This parameter may be nullptr, in which case the callback will be invoked in the AsyncServerSocket's primary EventBase. |
maxAtOnce | The maximum number of connections to accept in this callback on a single iteration of the event base loop. This only takes effect when eventBase is non-nullptr. When using a nullptr eventBase for the callback, the setMaxAcceptAtOnce() method controls how many connections the main event base will accept at once. |
Definition at line 586 of file AsyncServerSocket.cpp.
References accepting_, folly::AsyncServerSocket::AcceptCallback::acceptStarted(), callbacks_, connectionEventCallback_, folly::EventBase::dcheckIsInEventBaseThread(), eventBase_, maxNumMsgsInQueue_, folly::AsyncServerSocket::RemoteAcceptor::start(), and startAccepting().
Referenced by folly::ZeroCopyTestServer::addCallbackToServerSocket(), getAddress(), wangle::Acceptor::init(), and serverSocketSanityTest().
void folly::AsyncServerSocket::attachEventBase | ( | EventBase * | eventBase | ) |
Attach this AsyncServerSocket to its primary EventBase.
This may only be called if the AsyncServerSocket is not already attached to a EventBase. The AsyncServerSocket must be attached to a EventBase before it can begin accepting connections.
Definition at line 274 of file AsyncServerSocket.cpp.
References folly::EventBase::dcheckIsInEventBaseThread(), eventBase_, handler(), and sockets_.
Referenced by newSocket(), and folly::AsyncServerSocket::ServerEventHandler::operator=().
|
private |
Definition at line 1142 of file AsyncServerSocket.cpp.
References accepting_, callbacks_, connectionEventCallback_, folly::EventBase::dcheckIsInEventBaseThread(), eventBase_, handler(), folly::AsyncServerSocket::ConnectionEventCallback::onBackoffEnded(), folly::EventHandler::PERSIST, folly::EventHandler::READ, and sockets_.
|
virtual |
Bind to the specified address.
This must be called from the primary EventBase thread.
Throws TTransportException on error.
Definition at line 372 of file AsyncServerSocket.cpp.
References bindSocket(), createSocket(), folly::EventBase::dcheckIsInEventBaseThread(), eventBase_, folly::SocketAddress::getFamily(), and sockets_.
Referenced by getSocket().
|
virtual |
Bind to the specified port for the specified addresses.
This must be called from the primary EventBase thread.
Throws TTransportException on error.
Definition at line 398 of file AsyncServerSocket.cpp.
References bindSocket(), createSocket(), and sockets_.
|
virtual |
Bind to the specified port.
This must be called from the primary EventBase thread.
Throws TTransportException on error.
Definition at line 422 of file AsyncServerSocket.cpp.
References folly::netops::bind(), folly::ShutdownSocketSet::close(), folly::closeNoInt(), eventBase_, folly::SocketAddress::getFamily(), folly::SocketAddress::getFamilyNameFrom(), folly::SocketAddress::getPort(), folly::SocketAddress::getPortFrom(), folly::kIsWindows, noTransparentTls_, s, SCOPE_EXIT, folly::SocketAddress::setFromLocalAddress(), folly::netops::setsockopt(), setupSocket(), folly::test::shutdownSocketSet, folly::netops::socket(), sockets_, folly::throwSystemError(), and wShutdownSocketSet_.
|
private |
Definition at line 331 of file AsyncServerSocket.cpp.
References folly::netops::bind(), folly::closeNoInt(), folly::SocketAddress::describe(), eventBase_, folly::SocketAddress::getActualSize(), folly::SocketAddress::getAddress(), folly::SocketAddress::getFamily(), noTransparentTls_, folly::netops::setsockopt(), sockets_, and folly::throwSystemError().
Referenced by bind().
|
private |
Definition at line 738 of file AsyncServerSocket.cpp.
References folly::closeNoInt(), setupSocket(), folly::netops::socket(), and folly::throwSystemError().
Referenced by bind().
|
overridevirtual |
Destroy the socket.
AsyncServerSocket::destroy() must be called to destroy the socket. The normal destructor is private, and should not be invoked directly. This prevents callers from deleting a AsyncServerSocket while it is invoking a callback.
destroy() must be invoked from the socket's primary EventBase thread.
If there are AcceptCallbacks still installed when destroy() is called, acceptStopped() will be called on these callbacks to notify them that accepting has stopped. Accept callbacks being driven by other EventBase threads may continue to receive new accept callbacks for a brief period of time after destroy() returns. They will not receive any more callback invocations once acceptStopped() is invoked.
Reimplemented from folly::DelayedDestruction.
Definition at line 264 of file AsyncServerSocket.cpp.
References folly::closeNoInt(), folly::DelayedDestruction::destroy(), pendingCloseSockets_, s, and stopAccepting().
Referenced by newSocket(), and wangle::AsyncServerSocketFactory::ThreadSafeDestructor::operator()().
void folly::AsyncServerSocket::detachEventBase | ( | ) |
Detach the AsyncServerSocket from its primary EventBase.
detachEventBase() may only be called if the AsyncServerSocket is not currently accepting connections.
Definition at line 284 of file AsyncServerSocket.cpp.
References accepting_, folly::EventBase::dcheckIsInEventBaseThread(), eventBase_, handler(), and sockets_.
Referenced by newSocket(), and folly::AsyncServerSocket::ServerEventHandler::operator=().
|
inline |
Do not attempt the transparent TLS handshake
Definition at line 707 of file AsyncServerSocket.h.
References noTransparentTls_.
Referenced by AsyncServerSocket().
|
private |
Definition at line 1056 of file AsyncServerSocket.cpp.
References folly::AsyncServerSocket::AcceptCallback::acceptError(), folly::AsyncServerSocket::CallbackInfo::callback, callbackIndex_, folly::AsyncServerSocket::CallbackInfo::consumer, folly::AsyncServerSocket::QueueMessage::err, errnoValue, folly::AsyncServerSocket::CallbackInfo::eventBase, folly::AsyncServerSocket::RemoteAcceptor::getQueue(), deadlock::info(), folly::gen::move, folly::AsyncServerSocket::QueueMessage::msg, MSG_ERROR, nextCallback(), string, folly::AsyncServerSocket::QueueMessage::type, and uint32_t.
Referenced by handlerReady().
|
private |
Definition at line 995 of file AsyncServerSocket.cpp.
References acceptRate_, acceptRateAdjustSpeed_, addr, folly::AsyncServerSocket::QueueMessage::address, folly::AsyncServerSocket::CallbackInfo::callback, callbackIndex_, folly::closeNoInt(), folly::AsyncServerSocket::AcceptCallback::connectionAccepted(), connectionEventCallback_, folly::AsyncServerSocket::CallbackInfo::consumer, folly::AsyncServerSocket::CallbackInfo::eventBase, folly::AsyncServerSocket::QueueMessage::fd, folly::AsyncServerSocket::RemoteAcceptor::getQueue(), deadlock::info(), folly::gen::move, MSG_NEW_CONN, nextCallback(), numDroppedConnections_, folly::AsyncServerSocket::ConnectionEventCallback::onConnectionDropped(), folly::AsyncServerSocket::ConnectionEventCallback::onConnectionEnqueuedForAcceptorCallback(), folly::netops::socket(), folly::AsyncServerSocket::QueueMessage::type, and uint32_t.
Referenced by handlerReady().
|
private |
Definition at line 1093 of file AsyncServerSocket.cpp.
References backoffTimeout_, connectionEventCallback_, handler(), folly::AsyncServerSocket::ConnectionEventCallback::onBackoffError(), folly::AsyncServerSocket::ConnectionEventCallback::onBackoffStarted(), folly::AsyncTimeout::scheduleTimeout(), sockets_, and uint32_t.
Referenced by handlerReady().
|
inline |
Get whether or not the socket is accepting new connections
Definition at line 714 of file AsyncServerSocket.h.
References accepting_.
|
inline |
Get the speed of adjusting connection accept rate.
Definition at line 569 of file AsyncServerSocket.h.
References acceptRateAdjustSpeed_.
|
overridevirtual |
Get the local address to which the socket is bound.
Throws TTransportException on error.
Implements folly::AsyncSocketBase.
Definition at line 567 of file AsyncServerSocket.cpp.
References folly::SocketAddress::setFromLocalAddress(), socket_, and sockets_.
Referenced by serverSocketSanityTest().
|
inline |
Get the local address to which the socket is bound.
Throws TTransportException on error.
Definition at line 368 of file AsyncServerSocket.h.
References addAcceptCallback(), getAddresses(), listen(), pauseAccepting(), removeAcceptCallback(), startAccepting(), stopAccepting(), and uint32_t.
Referenced by getSocket().
std::vector< SocketAddress > folly::AsyncServerSocket::getAddresses | ( | ) | const |
Get all the local addresses to which the socket is bound.
Throws TTransportException on error.
Definition at line 576 of file AsyncServerSocket.cpp.
References folly::netops::socket(), and sockets_.
Referenced by getAddress().
|
inline |
Get whether or not FD_CLOEXEC is enabled on the server socket.
Definition at line 692 of file AsyncServerSocket.h.
References closeOnExec_.
|
inline |
Get the ConnectionEventCallback
Definition at line 729 of file AsyncServerSocket.h.
References connectionEventCallback_, and ~AsyncServerSocket().
|
inlineoverridevirtual |
Get the EventBase used by this socket.
Implements folly::AsyncSocketBase.
Definition at line 273 of file AsyncServerSocket.h.
References eventBase_, useExistingSocket(), and useExistingSockets().
Referenced by wangle::AsyncServerSocketFactory::ThreadSafeDestructor::operator()(), and serverSocketSanityTest().
|
inline |
Get whether or not SO_KEEPALIVE is enabled on the server socket.
Definition at line 647 of file AsyncServerSocket.h.
References keepAliveEnabled_.
|
inline |
Get the maximum number of connections that will be accepted each time around the event loop.
Definition at line 525 of file AsyncServerSocket.h.
References maxAcceptAtOnce_.
|
inline |
Get the maximum number of unprocessed messages which a NotificationQueue can hold.
Definition at line 551 of file AsyncServerSocket.h.
References maxNumMsgsInQueue_.
|
inline |
Get the number of connections dropped by the AsyncServerSocket
Definition at line 592 of file AsyncServerSocket.h.
References numDroppedConnections_.
|
inline |
Get the current number of unprocessed messages in NotificationQueue.
This method must be invoked from the AsyncServerSocket's primary EventBase thread. Use EventBase::runInEventBaseThread() to schedule the operation in the correct EventBase if your code is not in the server socket's primary EventBase.
Definition at line 604 of file AsyncServerSocket.h.
References callbacks_, folly::EventBase::dcheckIsInEventBaseThread(), eventBase_, and int64_t.
|
inline |
Get whether or not SO_REUSEPORT is enabled on the server socket.
Definition at line 677 of file AsyncServerSocket.h.
References reusePortEnabled_.
|
inline |
Backwards compatible getSocket, warns if > 1 socket
Definition at line 312 of file AsyncServerSocket.h.
References bind(), getAddress(), setZeroCopy(), sockets_, and uint16_t.
Referenced by setZeroCopy().
|
inline |
Return the underlying file descriptor
Definition at line 301 of file AsyncServerSocket.h.
References handler(), and sockets_.
Referenced by proxygen::HTTPServer::getListenSocket(), and wangle::Acceptor::init().
|
inline |
|
privatevirtualnoexcept |
Definition at line 855 of file AsyncServerSocket.cpp.
References folly::netops::accept(), accepting_, acceptRate_, acceptRateAdjustSpeed_, folly::Endian::big(), buffer(), callbacks_, folly::closeNoInt(), connectionEventCallback_, dispatchError(), dispatchSocket(), enterBackoff(), folly::netops::getsockopt(), folly::kIsLinux, lastAccepTimestamp_, maxAcceptAtOnce_, folly::gen::move, now(), numDroppedConnections_, folly::AsyncServerSocket::ConnectionEventCallback::onConnectionAccepted(), folly::AsyncServerSocket::ConnectionEventCallback::onConnectionAcceptError(), folly::AsyncServerSocket::ConnectionEventCallback::onConnectionDropped(), folly::SocketAddress::setFromSockaddr(), folly::netops::setsockopt(), TCP_SAVED_SYN, tosReflect_, and uint32_t.
|
virtual |
Begin listening for connections.
This calls listen() with the specified backlog.
Once listen() is invoked the socket will actually be open so that remote clients may establish connections. (Clients that attempt to connect before listen() is called will receive a connection refused error.)
At least one callback must be set and startAccepting() must be called to actually begin notifying the accept callbacks of newly accepted connections. The backlog parameter controls how many connections the kernel will accept and buffer internally while the accept callbacks are paused (or if accepting is enabled but the callbacks cannot keep up).
bind() must be called before calling listen(). listen() must be called from the primary EventBase thread.
Throws TTransportException on error.
Definition at line 554 of file AsyncServerSocket.cpp.
References folly::EventBase::dcheckIsInEventBaseThread(), eventBase_, handler(), folly::netops::listen(), sockets_, and folly::throwSystemError().
Referenced by getAddress().
|
inlinestatic |
Helper function to create a shared_ptr<AsyncServerSocket>.
This passes in the correct destructor object, since AsyncServerSocket's destructor is protected and cannot be invoked directly.
Definition at line 226 of file AsyncServerSocket.h.
References AsyncServerSocket(), attachEventBase(), destroy(), detachEventBase(), and setShutdownSocketSet().
Referenced by folly::TestSSLServer::init(), folly::ScopedBoundPort::ScopedBoundPort(), folly::TEST(), and TEST().
|
inlineprivate |
Definition at line 814 of file AsyncServerSocket.h.
References callbackIndex_, callbacks_, and deadlock::info().
Referenced by dispatchError(), and dispatchSocket().
void folly::AsyncServerSocket::pauseAccepting | ( | ) |
Pause accepting connections.
startAccepting() may be called to resume accepting.
This method may only be called from the primary EventBase thread. If there are AcceptCallbacks being driven by other EventBase threads they may continue to receive callbacks for a short period of time after pauseAccepting() returns.
Unlike removeAcceptCallback() or destroy(), acceptStopped() will not be called on the AcceptCallback objects simply due to a temporary pause. If the server socket is later destroyed while paused, acceptStopped() will be called all of the installed AcceptCallbacks.
Definition at line 723 of file AsyncServerSocket.cpp.
References accepting_, backoffTimeout_, folly::AsyncTimeout::cancelTimeout(), folly::EventBase::dcheckIsInEventBaseThread(), eventBase_, handler(), and sockets_.
Referenced by getAddress().
void folly::AsyncServerSocket::removeAcceptCallback | ( | AcceptCallback * | callback, |
EventBase * | eventBase | ||
) |
Remove an AcceptCallback.
This allows a single AcceptCallback to be removed from the round-robin pool.
This method must be invoked from the AsyncServerSocket's primary EventBase thread. Use EventBase::runInEventBaseThread() to schedule the operation in the correct EventBase if your code is not in the server socket's primary EventBase.
Given that the accept callback is being driven by a different EventBase, the AcceptCallback may continue to be invoked for a short period of time after removeAcceptCallback() returns in this thread. Once the other EventBase thread receives the notification to stop, it will call acceptStopped() on the callback to inform it that it is fully stopped and will not receive any new sockets.
If the last accept callback is removed while the socket is accepting, the socket will implicitly pause accepting. If a callback is later added, it will resume accepting immediately, without requiring startAccepting() to be invoked.
callback | The callback to uninstall. |
eventBase | The EventBase associated with this callback. This must be the same EventBase that was used when the callback was installed with addAcceptCallback(). |
Definition at line 636 of file AsyncServerSocket.cpp.
References accepting_, folly::AsyncServerSocket::AcceptCallback::acceptStopped(), folly::AsyncServerSocket::CallbackInfo::callback, callbackIndex_, callbacks_, folly::AsyncServerSocket::CallbackInfo::consumer, folly::EventBase::dcheckIsInEventBaseThread(), folly::AsyncServerSocket::CallbackInfo::eventBase, eventBase_, handler(), deadlock::info(), sockets_, folly::AsyncServerSocket::RemoteAcceptor::stop(), and uint32_t.
Referenced by getAddress(), and serverSocketSanityTest().
|
inline |
Set the speed of adjusting connection accept rate.
Definition at line 576 of file AsyncServerSocket.h.
References acceptRateAdjustSpeed_, and setTosReflect().
|
inline |
Set whether or not the socket should close during exec() (FD_CLOEXEC). By default, this is enabled
Definition at line 685 of file AsyncServerSocket.h.
References closeOnExec_.
|
inline |
Set the ConnectionEventCallback
Definition at line 721 of file AsyncServerSocket.h.
References connectionEventCallback_.
|
inline |
Set whether or not SO_KEEPALIVE should be enabled on the server socket (and thus on all subsequently-accepted connections). By default, keepalive is enabled.
Note that TCP keepalive usually only kicks in after the connection has been idle for several hours. Applications should almost always have their own, shorter idle timeout.
Definition at line 626 of file AsyncServerSocket.h.
References folly::errnoStr(), handler(), keepAliveEnabled_, folly::netops::setsockopt(), sockets_, and val.
|
inline |
Set the maximum number of connections that will be accepted each time around the event loop.
This provides a very coarse-grained way of controlling how fast the AsyncServerSocket will accept connections. If you find that when your server is overloaded AsyncServerSocket accepts connections more quickly than your code can process them, you can try lowering this number so that fewer connections will be accepted each event loop iteration.
For more explicit control over the accept rate, you can also use pauseAccepting() to temporarily pause accepting when your server is overloaded, and then use startAccepting() later to resume accepting.
Definition at line 543 of file AsyncServerSocket.h.
References maxAcceptAtOnce_.
|
inline |
Set the maximum number of unprocessed messages in NotificationQueue. No new message will be sent to that NotificationQueue if there are more than such number of unprocessed messages in that queue.
Only works if called before addAcceptCallback.
Definition at line 562 of file AsyncServerSocket.h.
References maxNumMsgsInQueue_.
|
inline |
Set whether or not SO_REUSEPORT should be enabled on the server socket, allowing multiple binds to the same port
Definition at line 655 of file AsyncServerSocket.h.
References handler(), reusePortEnabled_, folly::netops::setsockopt(), SO_REUSEPORT, sockets_, folly::throwSystemError(), and val.
void folly::AsyncServerSocket::setShutdownSocketSet | ( | const std::weak_ptr< ShutdownSocketSet > & | wNewSS | ) |
Definition at line 179 of file AsyncServerSocket.cpp.
References h, folly::ShutdownSocketSet::remove(), folly::test::shutdownSocketSet, sockets_, and wShutdownSocketSet_.
Referenced by newSocket().
|
inline |
Tries to enable TFO if the machine supports it.
Definition at line 699 of file AsyncServerSocket.h.
References tfo_, and tfoMaxQueueSize_.
void folly::AsyncServerSocket::setTosReflect | ( | bool | enable | ) |
Enable/Disable TOS reflection for the server socket
Enable/Disable TOS reflection for the server socket If enabled, the 'accepted' connections will reflect the TOS derived from the client's connect request
Definition at line 758 of file AsyncServerSocket.cpp.
References handler(), folly::kIsLinux, folly::netops::setsockopt(), sockets_, TCP_SAVE_SYN, folly::throwSystemError(), tosReflect_, and val.
Referenced by setAcceptRateAdjustSpeed().
|
private |
Definition at line 782 of file AsyncServerSocket.cpp.
References folly::ShutdownSocketSet::add(), closeOnExec_, folly::SocketAddress::describe(), folly::errnoStr(), keepAliveEnabled_, reusePortEnabled_, folly::setCloseOnExec(), folly::SocketAddress::setFromLocalAddress(), folly::netops::setsockopt(), folly::test::shutdownSocketSet, SO_REUSEPORT, tfo_, folly::detail::tfo_enable(), tfoMaxQueueSize_, folly::throwSystemError(), folly::WARNING, and wShutdownSocketSet_.
Referenced by bind(), createSocket(), and useExistingSockets().
bool folly::AsyncServerSocket::setZeroCopy | ( | bool | enable | ) |
Definition at line 360 of file AsyncServerSocket.cpp.
References getSocket(), folly::netops::setsockopt(), SO_ZEROCOPY, and val.
Referenced by getSocket().
|
virtual |
Begin accepting connctions on this socket.
bind() and listen() must be called before calling startAccepting().
When a AsyncServerSocket is initially created, it will not begin accepting connections until at least one callback has been added and startAccepting() has been called. startAccepting() can also be used to resume accepting connections after a call to pauseAccepting().
If startAccepting() is called when there are no accept callbacks installed, the socket will not actually begin accepting until an accept callback is added.
This method may only be called from the primary EventBase thread.
Definition at line 704 of file AsyncServerSocket.cpp.
References accepting_, callbacks_, folly::EventBase::dcheckIsInEventBaseThread(), eventBase_, handler(), folly::EventHandler::PERSIST, folly::EventHandler::READ, and sockets_.
Referenced by addAcceptCallback(), getAddress(), and serverSocketSanityTest().
int folly::AsyncServerSocket::stopAccepting | ( | int | shutdownFlags = -1 | ) |
Shutdown the listen socket and notify all callbacks that accept has stopped, but don't close the socket. This invokes shutdown(2) with the supplied argument. Passing -1 will close the socket now. Otherwise, the close will be delayed until this object is destroyed.
Only use this if you have reason to pass special flags to shutdown. Otherwise just destroy the socket.
This method has no effect when a ShutdownSocketSet option is used.
Returns the result of shutdown on sockets_[n-1]
Definition at line 207 of file AsyncServerSocket.cpp.
References accepting_, backoffTimeout_, callbacks_, folly::ShutdownSocketSet::close(), folly::closeNoInt(), folly::EventBase::dcheckIsInEventBaseThread(), eventBase_, handler(), pendingCloseSockets_, folly::shutdownNoInt(), folly::test::shutdownSocketSet, sockets_, and wShutdownSocketSet_.
Referenced by destroy(), and getAddress().
void folly::AsyncServerSocket::useExistingSocket | ( | int | fd | ) |
Create a AsyncServerSocket from an existing socket file descriptor.
useExistingSocket() will cause the AsyncServerSocket to take ownership of the specified file descriptor, and use it to listen for new connections. The AsyncServerSocket will close the file descriptor when it is destroyed.
useExistingSocket() must be called before bind() or listen().
The supplied file descriptor will automatically be put into non-blocking mode. The caller may have already directly called bind() and possibly listen on the file descriptor. If so the caller should skip calling the corresponding AsyncServerSocket::bind() and listen() methods.
On error a TTransportException will be thrown and the caller will retain ownership of the file descriptor.
Definition at line 327 of file AsyncServerSocket.cpp.
References useExistingSockets().
Referenced by getEventBase().
void folly::AsyncServerSocket::useExistingSockets | ( | const std::vector< int > & | fds | ) |
Definition at line 295 of file AsyncServerSocket.cpp.
References folly::EventBase::dcheckIsInEventBaseThread(), eventBase_, folly::SocketAddress::getFamily(), noTransparentTls_, folly::SocketAddress::setFromLocalAddress(), folly::netops::setsockopt(), setupSocket(), and sockets_.
Referenced by getEventBase(), and useExistingSocket().
|
private |
Definition at line 872 of file AsyncServerSocket.h.
Referenced by addAcceptCallback(), backoffTimeoutExpired(), detachEventBase(), getAccepting(), handlerReady(), pauseAccepting(), removeAcceptCallback(), startAccepting(), and stopAccepting().
|
private |
Definition at line 876 of file AsyncServerSocket.h.
Referenced by dispatchSocket(), and handlerReady().
|
private |
Definition at line 875 of file AsyncServerSocket.h.
Referenced by dispatchSocket(), getAcceptRateAdjustSpeed(), handlerReady(), and setAcceptRateAdjustSpeed().
|
private |
Definition at line 880 of file AsyncServerSocket.h.
Referenced by enterBackoff(), pauseAccepting(), and stopAccepting().
|
private |
Definition at line 879 of file AsyncServerSocket.h.
Referenced by dispatchError(), dispatchSocket(), nextCallback(), and removeAcceptCallback().
|
private |
Definition at line 881 of file AsyncServerSocket.h.
Referenced by addAcceptCallback(), backoffTimeoutExpired(), getNumPendingMessagesInQueue(), handlerReady(), nextCallback(), removeAcceptCallback(), startAccepting(), stopAccepting(), and ~AsyncServerSocket().
|
private |
Definition at line 884 of file AsyncServerSocket.h.
Referenced by getCloseOnExec(), setCloseOnExec(), and setupSocket().
|
private |
Definition at line 889 of file AsyncServerSocket.h.
Referenced by addAcceptCallback(), backoffTimeoutExpired(), dispatchSocket(), enterBackoff(), getConnectionEventCallback(), handlerReady(), and setConnectionEventCallback().
|
private |
Definition at line 869 of file AsyncServerSocket.h.
Referenced by addAcceptCallback(), attachEventBase(), backoffTimeoutExpired(), bind(), bindSocket(), detachEventBase(), getEventBase(), getNumPendingMessagesInQueue(), listen(), folly::AsyncServerSocket::ServerEventHandler::operator=(), pauseAccepting(), removeAcceptCallback(), startAccepting(), stopAccepting(), and useExistingSockets().
|
static |
Definition at line 208 of file AsyncServerSocket.h.
|
static |
Definition at line 207 of file AsyncServerSocket.h.
|
static |
Definition at line 209 of file AsyncServerSocket.h.
|
private |
Definition at line 882 of file AsyncServerSocket.h.
Referenced by getKeepAliveEnabled(), setKeepAliveEnabled(), and setupSocket().
|
private |
Definition at line 877 of file AsyncServerSocket.h.
Referenced by handlerReady().
|
private |
Definition at line 873 of file AsyncServerSocket.h.
Referenced by getMaxAcceptAtOnce(), handlerReady(), and setMaxAcceptAtOnce().
|
private |
Definition at line 874 of file AsyncServerSocket.h.
Referenced by addAcceptCallback(), getMaxNumMessagesInQueue(), and setMaxNumMessagesInQueue().
|
private |
Definition at line 886 of file AsyncServerSocket.h.
Referenced by bind(), bindSocket(), disableTransparentTls(), and useExistingSockets().
|
private |
Definition at line 878 of file AsyncServerSocket.h.
Referenced by dispatchSocket(), getNumDroppedConnections(), and handlerReady().
|
private |
Definition at line 871 of file AsyncServerSocket.h.
Referenced by destroy(), and stopAccepting().
|
private |
Definition at line 883 of file AsyncServerSocket.h.
Referenced by getReusePortEnabled_(), setReusePortEnabled(), and setupSocket().
|
private |
Definition at line 870 of file AsyncServerSocket.h.
Referenced by attachEventBase(), backoffTimeoutExpired(), bind(), bindSocket(), detachEventBase(), enterBackoff(), getAddress(), getAddresses(), getSocket(), getSockets(), listen(), pauseAccepting(), removeAcceptCallback(), setKeepAliveEnabled(), setReusePortEnabled(), setShutdownSocketSet(), setTosReflect(), startAccepting(), stopAccepting(), and useExistingSockets().
|
private |
Definition at line 885 of file AsyncServerSocket.h.
Referenced by setTFOEnabled(), and setupSocket().
|
private |
Definition at line 887 of file AsyncServerSocket.h.
Referenced by setTFOEnabled(), and setupSocket().
|
private |
Definition at line 890 of file AsyncServerSocket.h.
Referenced by getTosReflect(), handlerReady(), and setTosReflect().
|
private |
Definition at line 888 of file AsyncServerSocket.h.
Referenced by bind(), setShutdownSocketSet(), setupSocket(), and stopAccepting().