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:
yhirose committed 2026-11-21 10:19:26 -05:00
1 parent d730627815
commit b99ccee3db
3 files changed
+160 -39

No files matched your search

+3 -2
View File
@@ -930,10 +930,11 @@ By default, a kept-alive connection holds a worker thread while it waits for its
With it:
- Consecutive requests on a connection may run on different threads.
- When the [max queued requests](#max-queued-requests) limit is reached, only a connection that has not been served yet is closed. An established one waits for room.
- When the [max queued requests](#max-queued-requests) limit is reached, only a connection that has not been served yet (or, with `SSLServer`, has not finished its TLS handshake) is closed. An established one waits for room.
- Each request pays for handing the connection over, so with no more connections than threads and very light handlers, it can serve fewer requests per second.
- A handler, a content provider and a WebSocket still hold their thread while they run.
- `SSLServer` ignores it for now.
- A subclass that overrides the private `process_and_close_socket()` is bypassed.
- `SSLServer` runs the TLS handshake on a worker when the client starts it. Until then the connection waits in the event loop and is closed at the keep-alive timeout, not the read timeout, and `ssl_last_error()` is not set for it.
### Client address behind a reverse proxy
+70 -29
View File
@@ -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,
+87 -8
View File
@@ -76,8 +76,13 @@ ctx_t create_server_context();
void free_context(ctx_t ctx);
bool set_server_cert_file(ctx_t ctx, const char *cert_path,
const char *key_path, const char *password);
ctx_t create_client_context();
void set_verify_client(ctx_t ctx, bool require);
session_t create_session(ctx_t ctx, socket_t sock);
void free_session(session_t session);
bool connect_nonblocking(session_t session, socket_t sock, time_t timeout_sec,
time_t timeout_usec, TlsError *err,
const std::function<bool()> &interrupted);
bool accept_nonblocking(session_t session, socket_t sock, time_t timeout_sec,
time_t timeout_usec, TlsError *err);
ssize_t read(session_t session, void *buf, size_t len, TlsError &err);
@@ -34406,20 +34411,94 @@ TEST(KeepAliveParkingTest, StopClosesParkedConnectionsPromptly) {
}
#ifdef CPPHTTPLIB_SSL_ENABLED
TEST(KeepAliveParkingTest, SSLServerServesAsBefore) {
namespace parking_test {
// Completes a TLS handshake on `sock` and sends nothing more, like a browser
// that has opened a connection ahead of time. Freeing the session sends no
// close_notify, so the connection stays quiet.
bool handshake_only(socket_t sock) {
auto ctx = tls::create_client_context();
tls::set_verify_client(ctx, false);
auto session = tls::create_session(ctx, sock);
auto se = detail::scope_exit([&] {
tls::free_session(session);
tls::free_context(ctx);
});
tls::TlsError err;
return session &&
tls::connect_nonblocking(session, sock, 2, 0, &err, nullptr);
}
Client tls_client(int port) {
Client cli("https://127.0.0.1:" + std::to_string(port));
cli.enable_server_certificate_verification(false);
cli.set_keep_alive(true);
cli.set_connection_timeout(1);
cli.set_read_timeout(1);
return cli;
}
bool tls_get_ok(Client &cli) {
auto res = cli.Get("/");
return res && res->body == "ok";
}
} // namespace parking_test
TEST(KeepAliveParkingTest, IdleTlsConnectionsDoNotHoldTheWorkers) {
using namespace parking_test;
SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
ASSERT_TRUE(svr.is_valid());
serve_ok(svr, threads(1));
svr.set_read_timeout(1); // so that a failing run does not take long
RunningServer running(svr);
Sockets socks;
// One served connection, some that have not started their handshake, and
// one that has finished it and gone quiet. None may keep the only worker.
auto served = tls_client(running.port);
ASSERT_TRUE(tls_get_ok(served));
for (int i = 0; i < 10; i++) {
ASSERT_NE(INVALID_SOCKET, socks.connect(running.port));
}
EXPECT_TRUE(handshake_only(socks.connect(running.port)));
auto other = tls_client(running.port);
EXPECT_TRUE(tls_get_ok(other));
EXPECT_TRUE(tls_get_ok(served));
}
TEST(KeepAliveParkingTest, FailedTlsHandshakeClosesTheConnection) {
using namespace parking_test;
SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
ASSERT_TRUE(svr.is_valid());
serve_ok(svr, threads(1));
RunningServer running(svr);
Sockets socks;
auto plain = socks.connect(running.port);
ASSERT_TRUE(send_get(plain));
EXPECT_TRUE(sees_eof(plain, in_ms(1000)));
EXPECT_NE(0, svr.ssl_last_error());
}
TEST(KeepAliveParkingTest, StopClosesParkedTlsConnections) {
using namespace parking_test;
SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
ASSERT_TRUE(svr.is_valid());
serve_ok(svr, threads(2));
RunningServer running(svr);
SSLClient cli("127.0.0.1", running.port);
cli.enable_server_certificate_verification(false);
cli.set_keep_alive(true);
for (int i = 0; i < 2; i++) {
auto res = cli.Get("/");
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ("ok", res->body);
std::vector<Client> clients;
for (int i = 0; i < 5; i++) {
clients.push_back(tls_client(running.port));
ASSERT_TRUE(tls_get_ok(clients.back()));
}
auto start = std::chrono::steady_clock::now();
svr.stop();
running.thread.join();
EXPECT_LT(std::chrono::steady_clock::now() - start,
std::chrono::milliseconds(500));
}
#endif