proxygen
|
#include <ShutdownSocketSet.h>
Classes | |
struct | Free |
Public Member Functions | |
ShutdownSocketSet (int maxFd=1<< 18) | |
void | add (int fd) |
void | remove (int fd) |
int | close (int fd) |
void | shutdown (int fd, bool abortive=false) |
void | shutdownAll (bool abortive=false) |
Private Types | |
enum | State : uint8_t { FREE = 0, IN_USE, IN_SHUTDOWN, SHUT_DOWN, MUST_CLOSE } |
Private Member Functions | |
void | doShutdown (int fd, bool abortive) |
Private Attributes | |
const int | maxFd_ |
std::unique_ptr< std::atomic< uint8_t >[], Free > | data_ |
folly::File | nullFile_ |
Set of sockets that allows immediate, take-no-prisoners abort.
Definition at line 32 of file ShutdownSocketSet.h.
|
private |
Enumerator | |
---|---|
FREE | |
IN_USE | |
IN_SHUTDOWN | |
SHUT_DOWN | |
MUST_CLOSE |
Definition at line 124 of file ShutdownSocketSet.h.
|
explicit |
Create a socket set that can handle file descriptors < maxFd. The default value (256Ki) is high enough for just about all applications, even if you increased the number of file descriptors on your system.
Definition at line 29 of file ShutdownSocketSet.cpp.
void folly::ShutdownSocketSet::add | ( | int | fd | ) |
Add an already open socket to the list of sockets managed by ShutdownSocketSet. You MUST close the socket by calling ShutdownSocketSet::close (which will, as a side effect, also handle EINTR properly) and not by calling close() on the file descriptor.
Definition at line 35 of file ShutdownSocketSet.cpp.
References data_, FREE, IN_USE, maxFd_, and uint8_t.
Referenced by folly::AsyncSocket::connect(), folly::test::Server::Server(), and folly::AsyncServerSocket::setupSocket().
int folly::ShutdownSocketSet::close | ( | int | fd | ) |
Close a socket managed by ShutdownSocketSet. Returns the same return code as close() (and sets errno accordingly).
Definition at line 73 of file ShutdownSocketSet.cpp.
References folly::closeNoInt(), data_, folly::FATAL, FREE, IN_SHUTDOWN, IN_USE, maxFd_, MUST_CLOSE, SHUT_DOWN, and uint8_t.
Referenced by folly::AsyncServerSocket::bind(), folly::test::Server::closeClients(), folly::AsyncSocket::doClose(), folly::test::Server::Server(), and folly::AsyncServerSocket::stopAccepting().
|
private |
Definition at line 143 of file ShutdownSocketSet.cpp.
References folly::dup2NoInt(), folly::File::fd(), nullFile_, folly::netops::setsockopt(), and folly::shutdownNoInt().
Referenced by shutdown().
void folly::ShutdownSocketSet::remove | ( | int | fd | ) |
Remove a socket from the list of sockets managed by ShutdownSocketSet. Note that remove() might block! (which we lamely implement by sleep()-ing) in the extremely rare case that the fd is currently being shutdown().
Definition at line 49 of file ShutdownSocketSet.cpp.
References data_, folly::FATAL, FREE, IN_SHUTDOWN, maxFd_, and uint8_t.
Referenced by folly::AsyncSocket::setShutdownSocketSet(), and folly::AsyncServerSocket::setShutdownSocketSet().
void folly::ShutdownSocketSet::shutdown | ( | int | fd, |
bool | abortive = false |
||
) |
Shut down a socket. If abortive is true, we perform an abortive shutdown (send RST rather than FIN). Note that we might still end up sending FIN due to the rather interesting implementation.
This is async-signal-safe and ignores errors. Obviously, subsequent read() and write() operations to the socket will fail. During normal operation, just call shutdown() on the socket.
Definition at line 102 of file ShutdownSocketSet.cpp.
References folly::closeNoInt(), data_, doShutdown(), FREE, IN_SHUTDOWN, IN_USE, maxFd_, MUST_CLOSE, SHUT_DOWN, and uint8_t.
Referenced by shutdownAll().
void folly::ShutdownSocketSet::shutdownAll | ( | bool | abortive = false | ) |
Immediate shutdown of all connections. This is a hard-hitting hammer; all reads and writes will return errors and no new connections will be accepted.
To be used only in dire situations. We're using it from the failure signal handler to close all connections quickly, even though the server might take multiple seconds to finish crashing.
The optional bool parameter indicates whether to set the active connections in to not linger. The effect of that includes RST packets being immediately sent to clients which will result in errors (and not normal EOF) on the client side. This also causes the local (ip, tcp port number) tuple to be reusable immediately, instead of having to wait the standard amount of time. For full details see the shutdown
method of ShutdownSocketSet
(incl. notes about the abortive
parameter).
This is async-signal-safe and ignores errors.
Definition at line 134 of file ShutdownSocketSet.cpp.
References data_, i, IN_USE, maxFd_, and shutdown().
Referenced by folly::test::runKillTest().
Definition at line 140 of file ShutdownSocketSet.h.
Referenced by add(), close(), remove(), shutdown(), and shutdownAll().
|
private |
Definition at line 139 of file ShutdownSocketSet.h.
Referenced by add(), close(), remove(), shutdown(), and shutdownAll().
|
private |
Definition at line 141 of file ShutdownSocketSet.h.
Referenced by doShutdown().