Retry a parking backlog on a timer only, and tidy up its tests

The wake a worker sent when it started a job only shortened the wait of a
backlog from at most 10 ms, and needed an atomic flag and a second enqueue
try to be safe. The 10 ms retry, which works with any TaskQueue, now does
it alone, and one submit() decides who may enter the queue. The listen
retries use the same interval, and the park failure is logged in one
place. The zero-timeout test sends its request before the server accepts,
which removes its race with the 10 ms floor.
This commit is contained in:
yhirose committed 2026-11-21 14:57:24 -05:00
1 parent dac2f48003
commit 907ac7c546
2 files changed
+55 -71

No files matched your search

+26 -32
View File
@@ -95,9 +95,11 @@ macos_ssl_trust_settings_result(CFArrayRef trust_settings);
} // namespace impl
#endif
} // namespace tls
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
namespace detail {
bool ssl_free_buffers_is_safe(unsigned long version);
} // namespace detail
#endif
} // namespace httplib
#endif
@@ -34167,10 +34169,14 @@ TEST(PollerFactoryTest, MakePollerWorks) {
namespace parking_test {
// Runs `svr` on a free port and stops it when it goes away.
// Runs `svr` on a free port and stops it when it goes away. `before_listen`
// runs once the port is bound and listening, before the server accepts.
struct RunningServer {
explicit RunningServer(Server &svr) : svr_(svr) {
explicit RunningServer(
Server &svr, const std::function<void(int)> &before_listen = nullptr)
: svr_(svr) {
port = svr_.bind_to_any_port("127.0.0.1");
if (before_listen) { before_listen(port); }
thread = std::thread([this] { svr_.listen_after_bind(); });
svr_.wait_until_ready();
}
@@ -34367,27 +34373,16 @@ TEST(KeepAliveParkingTest, EstablishedConnectionWaitsForAFullQueue) {
// One worker and room for one queued task.
serve_ok(svr, [&] { return new CountingTaskQueue(1, 1, queued, full); });
std::mutex m;
std::condition_variable cv;
auto entered = false;
auto released = false;
Gate gate;
svr.Get("/block", [&](const Request &, Response &res) {
std::unique_lock<std::mutex> lock(m);
entered = true;
cv.notify_all();
cv.wait_for(lock, std::chrono::seconds(3), [&] { return released; });
gate.wait();
res.set_content("ok", "text/plain");
});
auto release = [&] {
std::lock_guard<std::mutex> guard(m);
released = true;
cv.notify_all();
};
RunningServer running(svr);
// Before the server stops, so that a failed run does not wait for the
// handler's timeout.
auto release_on_exit = detail::scope_exit([&] { release(); });
auto open_gate = detail::scope_exit([&] { gate.open(); });
Sockets socks;
auto a = socks.connect(running.port);
@@ -34396,12 +34391,7 @@ TEST(KeepAliveParkingTest, EstablishedConnectionWaitsForAFullQueue) {
// B keeps the only worker busy.
auto b = socks.connect(running.port);
ASSERT_TRUE(send_get(b, "/block"));
ASSERT_TRUE(wait_until(
[&] {
std::lock_guard<std::mutex> guard(m);
return entered;
},
2000));
ASSERT_TRUE(wait_until([&] { return gate.entered() == 1; }, 2000));
// A new connection C fills the queue.
auto c = socks.connect(running.port);
@@ -34413,7 +34403,7 @@ TEST(KeepAliveParkingTest, EstablishedConnectionWaitsForAFullQueue) {
ASSERT_TRUE(send_get(a));
ASSERT_TRUE(wait_until([&] { return full >= 1; }, 2000));
release();
gate.open();
EXPECT_TRUE(receives_ok(b, 2000));
EXPECT_TRUE(receives_ok(c, 2000));
EXPECT_TRUE(receives_ok(a, 2000));
@@ -34465,6 +34455,7 @@ TEST(KeepAliveParkingTest, BacklogIsRetriedOnceTheQueueHasRoom) {
queue->pause_after_ms = 200;
first.open();
EXPECT_TRUE(wait_until([&] { return queued == 5; }, 1000));
queue->pause_after_ms = 0;
second.open();
EXPECT_TRUE(receives_ok(a, 2000));
@@ -34475,16 +34466,17 @@ TEST(KeepAliveParkingTest, ZeroKeepAliveTimeoutServesTheFirstRequest) {
Server svr;
serve_ok(svr, threads(1));
svr.set_keep_alive_timeout(0);
RunningServer running(svr);
Sockets socks;
// The request has to arrive within the 10 ms floor, which a busy machine
// can miss once. Without the floor every attempt fails.
auto served = false;
for (int i = 0; i < 5 && !served; i++) {
served = get_ok(socks.connect(running.port), 1000);
}
EXPECT_TRUE(served);
// The request is on the socket before the server accepts the connection,
// so it does not race the 10 ms floor. Without the floor the connection is
// closed unread.
Sockets socks;
auto sock = INVALID_SOCKET;
RunningServer running(svr, [&](int port) {
sock = socks.connect(port);
send_get(sock);
});
EXPECT_TRUE(receives_ok(sock, 1000));
}
TEST(KeepAliveParkingTest, StopClosesParkedConnectionsPromptly) {
@@ -34548,6 +34540,7 @@ bool tls_get_ok(Client &cli) {
} // namespace parking_test
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
TEST(KeepAliveParkingTest, SslFreeBuffersOnlyWhereTheCveIsFixed) {
struct {
unsigned long version;
@@ -34563,6 +34556,7 @@ TEST(KeepAliveParkingTest, SslFreeBuffersOnlyWhereTheCveIsFixed) {
<< std::hex << c.version;
}
}
#endif
TEST(KeepAliveParkingTest, IdleTlsConnectionsDoNotHoldTheWorkers) {
using namespace parking_test;