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

+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