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
|
||||
// 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<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) {
|
||||
@@ -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<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
|
||||
// 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<Connection> 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<std::chrono::milliseconds>(
|
||||
@@ -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<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 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; }
|
||||
|
||||
Reference in new issue
Block a user