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
+94
-1
@@ -34277,18 +34277,50 @@ public:
|
||||
: pool_(threads, threads, max_queued), ok_(ok), full_(full) {}
|
||||
|
||||
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_)++;
|
||||
return queued;
|
||||
}
|
||||
void shutdown() override { pool_.shutdown(); }
|
||||
|
||||
std::atomic<int> pause_after_ms{0};
|
||||
|
||||
private:
|
||||
ThreadPool pool_;
|
||||
std::atomic<int> &ok_;
|
||||
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
|
||||
|
||||
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<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) {
|
||||
using namespace parking_test;
|
||||
for (int round = 0; round < 5; round++) {
|
||||
|
||||
Reference in new issue
Block a user