diff --git a/httplib.h b/httplib.h index 14088ed0..e98f8c71 100644 --- a/httplib.h +++ b/httplib.h @@ -14470,11 +14470,15 @@ struct Server::ParkingState { // The rest belongs to parking_loop(). A parked connection's token is its // address, and `parked` lists them in the order they expire. - void park(Connection conn, Clock::time_point deadline) { + // False when the poller cannot watch it, which closes it: unwatched, it + // would only wait for its deadline. + bool park(Connection conn, Clock::time_point deadline) { auto &c = *conn; c.deadline = deadline; c.parked_at = parked.insert(parked.end(), std::move(conn)); - poller->arm(c.sock, reinterpret_cast(&c)); + if (poller->arm(c.sock, reinterpret_cast(&c))) { return true; } + unpark(reinterpret_cast(&c))->close_idle(); + return false; } Connection unpark(Token token) { @@ -16512,7 +16516,14 @@ inline bool Server::parking_loop(TaskQueue &task_queue) { auto listen_rearm_at = Clock::now(); // `never` while it is armed auto fatal = false; - const auto keep_alive = std::chrono::seconds(keep_alive_timeout_sec_); + const auto check_interval = + std::chrono::duration_cast(std::chrono::microseconds( + CPPHTTPLIB_KEEPALIVE_TIMEOUT_CHECK_INTERVAL_USECOND)); + // Never shorter than the interval keep_alive() first waits, so that a + // request sent right after connecting is served even with a timeout of 0. + const auto keep_alive = + (std::max)(Clock::duration(std::chrono::seconds(keep_alive_timeout_sec_)), + check_interval); // Only an interval the user asked for: Windows' default exists for the sake // of stop(), which wake() already serves here. const auto idle = std::chrono::microseconds( @@ -16523,16 +16534,28 @@ inline bool Server::parking_loop(TaskQueue &task_queue) { auto dispatch = [&](const Connection &conn) { return task_queue.enqueue([this, conn]() { resume_connection(conn); }); }; + // An established connection that finds the queue full waits in the backlog. + // The flag goes up before a second try, so that a worker taking a job in + // between either wakes this loop or leaves room for that try. + auto dispatch_established = [&](const Connection &conn) { + if (dispatch(conn)) { return true; } + st.backlog_waiting = true; + return dispatch(conn); + }; std::vector returned; while (listening && svr_sock_ == listen_sock) { auto now = Clock::now(); if (now >= listen_rearm_at) { - poller.arm(listen_sock, listen_token); - listen_rearm_at = never; + listen_rearm_at = poller.arm(listen_sock, listen_token) + ? never + : now + std::chrono::milliseconds(10); } + // A worker wakes this loop when it takes a job off the queue, but a + // TaskQueue may make room only later, so a backlog is also retried. auto until = (std::min)({st.next_deadline(), listen_rearm_at, next_idle, + st.backlog.empty() ? never : now + check_interval, now + std::chrono::seconds(60)}); // Rounded up, so that the wait does not end just short of the deadline. auto timeout = std::chrono::duration_cast( @@ -16548,7 +16571,9 @@ inline bool Server::parking_loop(TaskQueue &task_queue) { returned.swap(st.returned); } for (auto &conn : returned) { - st.park(std::move(conn), now + keep_alive); + if (!st.park(std::move(conn), now + keep_alive)) { + output_error_log(Error::ResourceExhaustion, nullptr); + } } returned.clear(); @@ -16576,7 +16601,9 @@ inline bool Server::parking_loop(TaskQueue &task_queue) { // Some platforms make it non-blocking like the listening socket. detail::set_nonblocking(sock, false); prepare_accepted_socket(sock); - st.park(new_keep_alive_connection(sock), now + keep_alive); + if (!st.park(new_keep_alive_connection(sock), now + keep_alive)) { + output_error_log(Error::ResourceExhaustion, nullptr); + } if (idle.count() > 0) { next_idle = now + idle; } } continue; @@ -16585,12 +16612,12 @@ inline bool Server::parking_loop(TaskQueue &task_queue) { auto conn = st.unpark(token); if (!conn->opened) { // A new connection is turned away when the queue is full, as - // accept_loop() does. - if (!dispatch(conn)) { + // accept_loop() does, and may not take room the backlog waits for. + if (!st.backlog.empty() || !dispatch(conn)) { conn->close_idle(); output_error_log(Error::ResourceExhaustion, nullptr); } - } else if (!st.backlog.empty() || !dispatch(conn)) { + } else if (!st.backlog.empty() || !dispatch_established(conn)) { // Closing an established connection would lose its request. st.backlog.push_back(std::move(conn)); } @@ -16626,12 +16653,12 @@ Server::new_keep_alive_connection(socket_t sock) { inline void Server::resume_connection(std::shared_ptr conn) { + auto &st = *parking_; + // Taking this job off the queue made room for one that waits in the backlog. + if (st.backlog_waiting) { st.poller->wake(); } + auto &c = *conn; auto ret = c.resume(); - - auto &st = *parking_; - // A backlog waits for a free worker, and this one is about to be free. - if (st.backlog_waiting) { st.poller->wake(); } if (c.turn.park) { c.release_buffers(); // it may stay idle for long if (st.give_back(conn)) { return; } diff --git a/test/test.cc b/test/test.cc index 1a975b18..fa85b233 100644 --- a/test/test.cc +++ b/test/test.cc @@ -34277,18 +34277,50 @@ public: : pool_(threads, threads, max_queued), ok_(ok), full_(full) {} bool enqueue(std::function fn) override { - auto queued = pool_.enqueue(std::move(fn)); + auto queued = pool_.enqueue([this, fn]() { + fn(); + // Holds the worker back from taking the next job. + std::this_thread::sleep_for(std::chrono::milliseconds(pause_after_ms)); + }); (queued ? ok_ : full_)++; return queued; } void shutdown() override { pool_.shutdown(); } + std::atomic pause_after_ms{0}; + private: ThreadPool pool_; std::atomic &ok_; std::atomic &full_; }; +// Holds handlers until it opens, or for 3 seconds at most. +class Gate { +public: + void wait() { + std::unique_lock lock(m_); + entered_++; + cv_.notify_all(); + cv_.wait_for(lock, std::chrono::seconds(3), [&] { return open_; }); + } + void open() { + std::lock_guard guard(m_); + open_ = true; + cv_.notify_all(); + } + int entered() { + std::lock_guard guard(m_); + return entered_; + } + +private: + std::mutex m_; + std::condition_variable cv_; + bool open_ = false; + int entered_ = 0; +}; + } // namespace parking_test TEST(KeepAliveParkingTest, IdleConnectionsDoNotHoldTheWorkers) { @@ -34387,6 +34419,67 @@ TEST(KeepAliveParkingTest, EstablishedConnectionWaitsForAFullQueue) { EXPECT_TRUE(receives_ok(a, 2000)); } +TEST(KeepAliveParkingTest, BacklogIsRetriedOnceTheQueueHasRoom) { + using namespace parking_test; + std::atomic queued{0}, full{0}; + CountingTaskQueue *queue = nullptr; + Server svr; + // Two workers and room for one queued task. + serve_ok(svr, + [&] { return queue = new CountingTaskQueue(2, 1, queued, full); }); + Gate first, second; + svr.Get("/first", [&](const Request &, Response &res) { + first.wait(); + res.set_content("ok", "text/plain"); + }); + svr.Get("/second", [&](const Request &, Response &res) { + second.wait(); + res.set_content("ok", "text/plain"); + }); + + RunningServer running(svr); + auto open_gates = detail::scope_exit([&] { + first.open(); + second.open(); + }); + Sockets socks; + + auto a = socks.connect(running.port); + ASSERT_TRUE(get_ok(a, 2000)); + + // B and D take both workers, and C fills the queue. + ASSERT_TRUE(send_get(socks.connect(running.port), "/first")); + ASSERT_TRUE(send_get(socks.connect(running.port), "/second")); + ASSERT_TRUE(wait_until( + [&] { return first.entered() == 1 && second.entered() == 1; }, 2000)); + ASSERT_TRUE(send_get(socks.connect(running.port), "/second")); + ASSERT_TRUE(wait_until([&] { return queued == 4; }, 2000)); + + // A's next request waits in the backlog. + ASSERT_TRUE(send_get(a)); + ASSERT_TRUE(wait_until([&] { return full >= 1; }, 2000)); + + // B's worker finishes, then takes C off the queue only later, which makes + // room for A. + queue->pause_after_ms = 200; + first.open(); + EXPECT_TRUE(wait_until([&] { return queued == 5; }, 1000)); + + second.open(); + EXPECT_TRUE(receives_ok(a, 2000)); +} + +TEST(KeepAliveParkingTest, ZeroKeepAliveTimeoutServesTheFirstRequest) { + using namespace parking_test; + Server svr; + serve_ok(svr, threads(1)); + svr.set_keep_alive_timeout(0); + RunningServer running(svr); + Sockets socks; + + EXPECT_TRUE(get_ok(socks.connect(running.port), 1000)); +} + TEST(KeepAliveParkingTest, StopClosesParkedConnectionsPromptly) { using namespace parking_test; for (int round = 0; round < 5; round++) {