Skip to content

Commit 1ab08ea

Browse files
committed
Improve shutdown performance
1 parent a5bfddf commit 1ab08ea

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

httplib.h

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8541,25 +8541,33 @@ inline SSL *ssl_new(socket_t sock, SSL_CTX *ctx, std::mutex &ctx_mutex,
85418541
return ssl;
85428542
}
85438543

8544-
inline void ssl_delete(std::mutex &ctx_mutex, SSL *ssl,
8544+
inline void ssl_delete(std::mutex &ctx_mutex, SSL *ssl, socket_t sock,
85458545
bool shutdown_gracefully) {
85468546
// sometimes we may want to skip this to try to avoid SIGPIPE if we know
85478547
// the remote has closed the network connection
85488548
// Note that it is not always possible to avoid SIGPIPE, this is merely a
85498549
// best-efforts.
85508550
if (shutdown_gracefully) {
8551+
#ifdef _WIN32
8552+
SSL_shutdown(ssl);
8553+
#else
85518554
auto is_peer_could_be_closed = false;
85528555
{
8556+
timeval tv;
8557+
tv.tv_sec = 1;
8558+
tv.tv_usec = 0;
8559+
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
8560+
reinterpret_cast<const void *>(&tv), sizeof(tv));
8561+
85538562
char buf[1];
85548563
if (SSL_peek(ssl, buf, 1) == 0 &&
85558564
SSL_get_error(ssl, 0) == SSL_ERROR_ZERO_RETURN) {
85568565
is_peer_could_be_closed = true;
85578566
}
85588567
}
85598568

8560-
if (!is_peer_could_be_closed) {
8561-
SSL_shutdown(ssl);
8562-
}
8569+
if (!is_peer_could_be_closed) { SSL_shutdown(ssl); }
8570+
#endif
85638571
}
85648572

85658573
std::lock_guard<std::mutex> guard(ctx_mutex);
@@ -8839,7 +8847,7 @@ inline bool SSLServer::process_and_close_socket(socket_t sock) {
88398847
// Shutdown gracefully if the result seemed successful, non-gracefully if
88408848
// the connection appeared to be closed.
88418849
const bool shutdown_gracefully = ret;
8842-
detail::ssl_delete(ctx_mutex_, ssl, shutdown_gracefully);
8850+
detail::ssl_delete(ctx_mutex_, ssl, sock, shutdown_gracefully);
88438851
}
88448852

88458853
detail::shutdown_socket(sock);
@@ -9122,7 +9130,8 @@ inline void SSLClient::shutdown_ssl_impl(Socket &socket,
91229130
return;
91239131
}
91249132
if (socket.ssl) {
9125-
detail::ssl_delete(ctx_mutex_, socket.ssl, shutdown_gracefully);
9133+
detail::ssl_delete(ctx_mutex_, socket.ssl, socket.sock,
9134+
shutdown_gracefully);
91269135
socket.ssl = nullptr;
91279136
}
91289137
assert(socket.ssl == nullptr);

0 commit comments

Comments
 (0)