mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-11-21 20:01:46 +00:00
Fix keep-alive parking with a zero timeout and a waiting backlog
- A keep-alive timeout of 0 closed every new connection before its first request. The wait now has the same 10 ms floor as keep_alive(). - An established connection in the backlog could wait for an unrelated event although the queue had room. A worker woke the loop when it finished a job, before ThreadPool made room by taking the next one, and the loop announced the backlog only after the failed try. A worker now wakes the loop when it starts a job, the loop announces the backlog before a second try, and retries it every 10 ms while it is not empty, for task queues that make room later. - A new connection could take the room the backlog waited for. It is now turned away while the backlog is not empty. - A failed arm() of the listening socket stopped accepting for good, and one of a connection left it unwatched. The first is retried after 10 ms, the second closes the connection.
This commit is contained in:
1 parent
114e3d0e46
commit
e1bc9c0140
2 files changed
+135
-15
No files matched your search
@@ -14470,11 +14470,15 @@ struct Server::ParkingState {
|
|||||||
|
|
||||||
// The rest belongs to parking_loop(). A parked connection's token is its
|
// The rest belongs to parking_loop(). A parked connection's token is its
|
||||||
// address, and `parked` lists them in the order they expire.
|
// 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;
|
auto &c = *conn;
|
||||||
c.deadline = deadline;
|
c.deadline = deadline;
|
||||||
c.parked_at = parked.insert(parked.end(), std::move(conn));
|
c.parked_at = parked.insert(parked.end(), std::move(conn));
|
||||||
poller->arm(c.sock, reinterpret_cast<Token>(&c));
|
if (poller->arm(c.sock, reinterpret_cast<Token>(&c))) { return true; }
|
||||||
|
unpark(reinterpret_cast<Token>(&c))->close_idle();
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Connection unpark(Token token) {
|
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 listen_rearm_at = Clock::now(); // `never` while it is armed
|
||||||
auto fatal = false;
|
auto fatal = false;
|
||||||
|
|
||||||
const auto keep_alive = std::chrono::seconds(keep_alive_timeout_sec_);
|
const auto check_interval =
|
||||||
|
std::chrono::duration_cast<Clock::duration>(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
|
// Only an interval the user asked for: Windows' default exists for the sake
|
||||||
// of stop(), which wake() already serves here.
|
// of stop(), which wake() already serves here.
|
||||||
const auto idle = std::chrono::microseconds(
|
const auto idle = std::chrono::microseconds(
|
||||||
@@ -16523,16 +16534,28 @@ inline bool Server::parking_loop(TaskQueue &task_queue) {
|
|||||||
auto dispatch = [&](const Connection &conn) {
|
auto dispatch = [&](const Connection &conn) {
|
||||||
return task_queue.enqueue([this, conn]() { resume_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<Connection> returned;
|
std::vector<Connection> returned;
|
||||||
while (listening && svr_sock_ == listen_sock) {
|
while (listening && svr_sock_ == listen_sock) {
|
||||||
auto now = Clock::now();
|
auto now = Clock::now();
|
||||||
if (now >= listen_rearm_at) {
|
if (now >= listen_rearm_at) {
|
||||||
poller.arm(listen_sock, listen_token);
|
listen_rearm_at = poller.arm(listen_sock, listen_token)
|
||||||
listen_rearm_at = never;
|
? 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,
|
auto until = (std::min)({st.next_deadline(), listen_rearm_at, next_idle,
|
||||||
|
st.backlog.empty() ? never : now + check_interval,
|
||||||
now + std::chrono::seconds(60)});
|
now + std::chrono::seconds(60)});
|
||||||
// Rounded up, so that the wait does not end just short of the deadline.
|
// Rounded up, so that the wait does not end just short of the deadline.
|
||||||
auto timeout = std::chrono::duration_cast<std::chrono::milliseconds>(
|
auto timeout = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||||
@@ -16548,7 +16571,9 @@ inline bool Server::parking_loop(TaskQueue &task_queue) {
|
|||||||
returned.swap(st.returned);
|
returned.swap(st.returned);
|
||||||
}
|
}
|
||||||
for (auto &conn : 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();
|
returned.clear();
|
||||||
|
|
||||||
@@ -16576,7 +16601,9 @@ inline bool Server::parking_loop(TaskQueue &task_queue) {
|
|||||||
// Some platforms make it non-blocking like the listening socket.
|
// Some platforms make it non-blocking like the listening socket.
|
||||||
detail::set_nonblocking(sock, false);
|
detail::set_nonblocking(sock, false);
|
||||||
prepare_accepted_socket(sock);
|
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; }
|
if (idle.count() > 0) { next_idle = now + idle; }
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
@@ -16585,12 +16612,12 @@ inline bool Server::parking_loop(TaskQueue &task_queue) {
|
|||||||
auto conn = st.unpark(token);
|
auto conn = st.unpark(token);
|
||||||
if (!conn->opened) {
|
if (!conn->opened) {
|
||||||
// A new connection is turned away when the queue is full, as
|
// A new connection is turned away when the queue is full, as
|
||||||
// accept_loop() does.
|
// accept_loop() does, and may not take room the backlog waits for.
|
||||||
if (!dispatch(conn)) {
|
if (!st.backlog.empty() || !dispatch(conn)) {
|
||||||
conn->close_idle();
|
conn->close_idle();
|
||||||
output_error_log(Error::ResourceExhaustion, nullptr);
|
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.
|
// Closing an established connection would lose its request.
|
||||||
st.backlog.push_back(std::move(conn));
|
st.backlog.push_back(std::move(conn));
|
||||||
}
|
}
|
||||||
@@ -16626,12 +16653,12 @@ Server::new_keep_alive_connection(socket_t sock) {
|
|||||||
|
|
||||||
inline void
|
inline void
|
||||||
Server::resume_connection(std::shared_ptr<KeepAliveConnection> conn) {
|
Server::resume_connection(std::shared_ptr<KeepAliveConnection> 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 &c = *conn;
|
||||||
auto ret = c.resume();
|
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) {
|
if (c.turn.park) {
|
||||||
c.release_buffers(); // it may stay idle for long
|
c.release_buffers(); // it may stay idle for long
|
||||||
if (st.give_back(conn)) { return; }
|
if (st.give_back(conn)) { return; }
|
||||||
|
|||||||
+94
-1
@@ -34277,18 +34277,50 @@ public:
|
|||||||
: pool_(threads, threads, max_queued), ok_(ok), full_(full) {}
|
: pool_(threads, threads, max_queued), ok_(ok), full_(full) {}
|
||||||
|
|
||||||
bool enqueue(std::function<void()> fn) override {
|
bool enqueue(std::function<void()> 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_)++;
|
(queued ? ok_ : full_)++;
|
||||||
return queued;
|
return queued;
|
||||||
}
|
}
|
||||||
void shutdown() override { pool_.shutdown(); }
|
void shutdown() override { pool_.shutdown(); }
|
||||||
|
|
||||||
|
std::atomic<int> pause_after_ms{0};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ThreadPool pool_;
|
ThreadPool pool_;
|
||||||
std::atomic<int> &ok_;
|
std::atomic<int> &ok_;
|
||||||
std::atomic<int> &full_;
|
std::atomic<int> &full_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Holds handlers until it opens, or for 3 seconds at most.
|
||||||
|
class Gate {
|
||||||
|
public:
|
||||||
|
void wait() {
|
||||||
|
std::unique_lock<std::mutex> lock(m_);
|
||||||
|
entered_++;
|
||||||
|
cv_.notify_all();
|
||||||
|
cv_.wait_for(lock, std::chrono::seconds(3), [&] { return open_; });
|
||||||
|
}
|
||||||
|
void open() {
|
||||||
|
std::lock_guard<std::mutex> guard(m_);
|
||||||
|
open_ = true;
|
||||||
|
cv_.notify_all();
|
||||||
|
}
|
||||||
|
int entered() {
|
||||||
|
std::lock_guard<std::mutex> guard(m_);
|
||||||
|
return entered_;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::mutex m_;
|
||||||
|
std::condition_variable cv_;
|
||||||
|
bool open_ = false;
|
||||||
|
int entered_ = 0;
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace parking_test
|
} // namespace parking_test
|
||||||
|
|
||||||
TEST(KeepAliveParkingTest, IdleConnectionsDoNotHoldTheWorkers) {
|
TEST(KeepAliveParkingTest, IdleConnectionsDoNotHoldTheWorkers) {
|
||||||
@@ -34387,6 +34419,67 @@ TEST(KeepAliveParkingTest, EstablishedConnectionWaitsForAFullQueue) {
|
|||||||
EXPECT_TRUE(receives_ok(a, 2000));
|
EXPECT_TRUE(receives_ok(a, 2000));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(KeepAliveParkingTest, BacklogIsRetriedOnceTheQueueHasRoom) {
|
||||||
|
using namespace parking_test;
|
||||||
|
std::atomic<int> 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) {
|
TEST(KeepAliveParkingTest, StopClosesParkedConnectionsPromptly) {
|
||||||
using namespace parking_test;
|
using namespace parking_test;
|
||||||
for (int round = 0; round < 5; round++) {
|
for (int round = 0; round < 5; round++) {
|
||||||
|
|||||||
Reference in new issue
Block a user