Improve shutdown performance

This commit is contained in:
yhirose 2024-08-06 10:41:53 -04:00
parent a5bfddf17f
commit 1ab08eac4e

View File

@ -8541,15 +8541,24 @@ inline SSL *ssl_new(socket_t sock, SSL_CTX *ctx, std::mutex &ctx_mutex,
return ssl;
}
inline void ssl_delete(std::mutex &ctx_mutex, SSL *ssl,
inline void ssl_delete(std::mutex &ctx_mutex, SSL *ssl, socket_t sock,
bool shutdown_gracefully) {
// sometimes we may want to skip this to try to avoid SIGPIPE if we know
// the remote has closed the network connection
// Note that it is not always possible to avoid SIGPIPE, this is merely a
// best-efforts.
if (shutdown_gracefully) {
#ifdef _WIN32
SSL_shutdown(ssl);
#else
auto is_peer_could_be_closed = false;
{
timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
reinterpret_cast<const void *>(&tv), sizeof(tv));
char buf[1];
if (SSL_peek(ssl, buf, 1) == 0 &&
SSL_get_error(ssl, 0) == SSL_ERROR_ZERO_RETURN) {
@ -8557,9 +8566,8 @@ inline void ssl_delete(std::mutex &ctx_mutex, SSL *ssl,
}
}
if (!is_peer_could_be_closed) {
SSL_shutdown(ssl);
}
if (!is_peer_could_be_closed) { SSL_shutdown(ssl); }
#endif
}
std::lock_guard<std::mutex> guard(ctx_mutex);
@ -8839,7 +8847,7 @@ inline bool SSLServer::process_and_close_socket(socket_t sock) {
// Shutdown gracefully if the result seemed successful, non-gracefully if
// the connection appeared to be closed.
const bool shutdown_gracefully = ret;
detail::ssl_delete(ctx_mutex_, ssl, shutdown_gracefully);
detail::ssl_delete(ctx_mutex_, ssl, sock, shutdown_gracefully);
}
detail::shutdown_socket(sock);
@ -9122,7 +9130,8 @@ inline void SSLClient::shutdown_ssl_impl(Socket &socket,
return;
}
if (socket.ssl) {
detail::ssl_delete(ctx_mutex_, socket.ssl, shutdown_gracefully);
detail::ssl_delete(ctx_mutex_, socket.ssl, socket.sock,
shutdown_gracefully);
socket.ssl = nullptr;
}
assert(socket.ssl == nullptr);