mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-11-21 20:01:46 +00:00
Support keep-alive parking in SSLServer
SSLServer::TlsConnection plugs the TLS session into the parked connection. A worker runs the handshake when the event loop sees the ClientHello, so a connection that has not started it, or has gone quiet after it, waits in the event loop instead of holding a worker. The handshake counts as having read what the loop saw, and a connection that has finished it waits for room like a served one when the queue is full. close_notify sent from the event loop does not block. SSLServer::start_session() holds the session setup and handshake that both the blocking path and TlsConnection use, and supports_keep_alive_parking() goes away.
This commit is contained in:
1 parent
d730627815
commit
b99ccee3db
3 files changed
+160
-39
No files matched your search
@@ -2584,7 +2584,6 @@ private:
|
||||
ContentReceiver multipart_receiver) const;
|
||||
|
||||
virtual bool process_and_close_socket(socket_t sock);
|
||||
virtual bool supports_keep_alive_parking() const;
|
||||
// The connection that parking_loop() keeps for a socket it has accepted.
|
||||
virtual std::shared_ptr<KeepAliveConnection>
|
||||
new_keep_alive_connection(socket_t sock);
|
||||
@@ -3548,7 +3547,12 @@ public:
|
||||
|
||||
private:
|
||||
bool process_and_close_socket(socket_t sock) override;
|
||||
bool supports_keep_alive_parking() const override;
|
||||
std::shared_ptr<KeepAliveConnection>
|
||||
new_keep_alive_connection(socket_t sock) override;
|
||||
struct TlsConnection;
|
||||
// Creates a session and runs the handshake. Null on failure, which sets
|
||||
// last_ssl_error_.
|
||||
tls::session_t start_session(socket_t sock);
|
||||
|
||||
tls::ctx_t ctx_ = nullptr;
|
||||
// update_certs_pem() replaces the certificates and CAs that handshakes read
|
||||
@@ -14321,7 +14325,7 @@ struct Server::KeepAliveConnection {
|
||||
}
|
||||
|
||||
// On a worker before the first request, e.g. for a TLS handshake. False
|
||||
// closes the connection.
|
||||
// closes the connection. One that reads from the socket clears turn.woken.
|
||||
virtual bool open() { return true; }
|
||||
// Serves requests while they come: serve_on() with the transport's stream.
|
||||
virtual bool serve() = 0;
|
||||
@@ -15586,9 +15590,8 @@ inline bool Server::listen_internal() {
|
||||
{
|
||||
std::unique_ptr<TaskQueue> task_queue(new_task_queue());
|
||||
|
||||
ret = keep_alive_parking_ && parking_ && supports_keep_alive_parking()
|
||||
? parking_loop(*task_queue)
|
||||
: accept_loop(*task_queue);
|
||||
ret = keep_alive_parking_ && parking_ ? parking_loop(*task_queue)
|
||||
: accept_loop(*task_queue);
|
||||
|
||||
task_queue->shutdown();
|
||||
}
|
||||
@@ -16481,8 +16484,6 @@ inline bool Server::abandon_listen_socket() {
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool Server::supports_keep_alive_parking() const { return true; }
|
||||
|
||||
// Like accept_loop(), but a connection waiting for its next request sits in
|
||||
// the poller instead of keeping a worker. A worker serves a connection while it
|
||||
// has requests to read, then gives it back to this loop.
|
||||
@@ -20149,7 +20150,45 @@ inline bool SSLServer::is_valid() const {
|
||||
return ctx_ != nullptr && Server::is_valid();
|
||||
}
|
||||
|
||||
inline bool SSLServer::process_and_close_socket(socket_t sock) {
|
||||
// A connection that keep-alive parking keeps for SSLServer. The worker that
|
||||
// first resumes it runs the handshake, so one that has not sent its
|
||||
// ClientHello yet waits in the event loop like an idle connection.
|
||||
struct SSLServer::TlsConnection final : Server::KeepAliveConnection {
|
||||
TlsConnection(socket_t s, SSLServer &server)
|
||||
: KeepAliveConnection(s, server), ssl_svr(server) {}
|
||||
~TlsConnection() override { tls::free_session(session); }
|
||||
|
||||
bool open() override {
|
||||
// A handshake would hold up the stop() that is under way.
|
||||
if (ssl_svr.is_stopping()) { return false; }
|
||||
session = ssl_svr.start_session(sock);
|
||||
if (!session) { return false; }
|
||||
// The handshake has read what the event loop saw, so a client that sends
|
||||
// nothing after it is parked instead of being waited for.
|
||||
turn.woken = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
// The session, not the stream, holds what a pipelined request has left.
|
||||
bool serve() override {
|
||||
detail::SSLSocketStream strm(
|
||||
sock, session, ssl_svr.read_timeout_sec_, ssl_svr.read_timeout_usec_,
|
||||
ssl_svr.write_timeout_sec_, ssl_svr.write_timeout_usec_);
|
||||
return serve_on(strm, [this](Request &req) { req.ssl = session; });
|
||||
}
|
||||
|
||||
void shut_down(bool graceful) override { tls::shutdown(session, graceful); }
|
||||
|
||||
SSLServer &ssl_svr;
|
||||
tls::session_t session = nullptr;
|
||||
};
|
||||
|
||||
inline std::shared_ptr<Server::KeepAliveConnection>
|
||||
SSLServer::new_keep_alive_connection(socket_t sock) {
|
||||
return std::make_shared<TlsConnection>(sock, *this);
|
||||
}
|
||||
|
||||
inline tls::session_t SSLServer::start_session(socket_t sock) {
|
||||
using namespace tls;
|
||||
|
||||
session_t session = nullptr;
|
||||
@@ -20162,29 +20201,17 @@ inline bool SSLServer::process_and_close_socket(socket_t sock) {
|
||||
|
||||
if (!session) {
|
||||
last_ssl_error_ = static_cast<int>(get_error());
|
||||
detail::shutdown_socket(sock);
|
||||
detail::close_socket(sock);
|
||||
return false;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Use scope_exit to ensure cleanup on all paths (including exceptions)
|
||||
bool handshake_done = false;
|
||||
bool ret = false;
|
||||
bool websocket_upgraded = false;
|
||||
// Frees the session on all paths but success (including exceptions)
|
||||
auto accepted = false;
|
||||
auto cleanup = detail::scope_exit([&] {
|
||||
if (handshake_done) { shutdown(session, !websocket_upgraded && ret); }
|
||||
free_session(session);
|
||||
if (handshake_done) {
|
||||
detail::drain_and_close_socket(sock);
|
||||
} else {
|
||||
detail::shutdown_socket(sock);
|
||||
detail::close_socket(sock);
|
||||
}
|
||||
if (!accepted) { free_session(session); }
|
||||
});
|
||||
|
||||
// Perform TLS accept handshake with timeout
|
||||
TlsError tls_err;
|
||||
auto accepted = false;
|
||||
{
|
||||
auto handshake_left = detail::scope_exit([&] {
|
||||
std::lock_guard<std::mutex> guard(ctx_mutex_);
|
||||
@@ -20206,10 +20233,27 @@ inline bool SSLServer::process_and_close_socket(socket_t sock) {
|
||||
#else
|
||||
last_ssl_error_ = static_cast<int>(get_error());
|
||||
#endif
|
||||
return nullptr;
|
||||
}
|
||||
return session;
|
||||
}
|
||||
|
||||
inline bool SSLServer::process_and_close_socket(socket_t sock) {
|
||||
auto session = start_session(sock);
|
||||
if (!session) {
|
||||
detail::shutdown_socket(sock);
|
||||
detail::close_socket(sock);
|
||||
return false;
|
||||
}
|
||||
|
||||
handshake_done = true;
|
||||
// Use scope_exit to ensure cleanup on all paths (including exceptions)
|
||||
bool ret = false;
|
||||
bool websocket_upgraded = false;
|
||||
auto cleanup = detail::scope_exit([&] {
|
||||
tls::shutdown(session, !websocket_upgraded && ret);
|
||||
tls::free_session(session);
|
||||
detail::drain_and_close_socket(sock);
|
||||
});
|
||||
|
||||
std::string remote_addr;
|
||||
int remote_port = 0;
|
||||
@@ -20235,9 +20279,6 @@ inline bool SSLServer::process_and_close_socket(socket_t sock) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Not yet: the TLS session would have to live in the parked connection.
|
||||
inline bool SSLServer::supports_keep_alive_parking() const { return false; }
|
||||
|
||||
inline bool SSLServer::update_certs_pem(const char *cert_pem,
|
||||
const char *key_pem,
|
||||
const char *client_ca_pem,
|
||||
|
||||
Reference in new issue
Block a user