后台服务协程化

This commit is contained in:
2026-08-18 11:57:43 +08:00
parent 704a6f02ff
commit 1e1de64199
10 changed files with 203 additions and 141 deletions
+8 -19
View File
@@ -8,7 +8,6 @@
#include <chrono>
#include <cmath>
#include <cstdint>
#include <mutex>
#include <optional>
#include <string>
#include <string_view>
@@ -185,11 +184,7 @@ struct Gallery_Plot_Session::Impl {
scene->set_client_metrics(performance);
return true;
}
[[nodiscard]] std::unique_lock<std::mutex> acquire_foreground_lock() {
return std::unique_lock<std::mutex>(mutex);
}
std::unique_ptr<Gallery_Scene_Interface> scene;
std::mutex mutex;
std::uint64_t session_id{};
std::optional<Web_Response> handle_gallery(const Gallery_Request& request) {
if (request.kind == Gallery_Request_Kind::Catalog)
@@ -302,18 +297,13 @@ struct Gallery_Plot_Session::Impl {
}
std::optional<Web_Response> handle_frame_request() {
const auto request_started = std::chrono::steady_clock::now();
std::optional<std::string> pixels;
{
auto lock = acquire_foreground_lock();
if (!scene || !scene->request_frame())
return std::nullopt;
const auto encode_started = std::chrono::steady_clock::now();
pixels = scene->encode_latest_pixels();
const auto encode_finished = std::chrono::steady_clock::now();
if (pixels)
scene->record_pixel_response(request_started, encode_started, encode_finished,
pixels->size());
}
if (!scene || !scene->request_frame())
return std::nullopt;
const auto encode_started = std::chrono::steady_clock::now();
auto pixels = scene->encode_latest_pixels();
const auto encode_finished = std::chrono::steady_clock::now();
if (pixels)
scene->record_pixel_response(request_started, encode_started, encode_finished, pixels->size());
if (!pixels)
return std::nullopt;
return Web_Response{Web_Response_Type::Pixels, std::move(*pixels)};
@@ -321,7 +311,6 @@ struct Gallery_Plot_Session::Impl {
std::optional<Web_Response> handle(const Web_Event& event) {
if (std::holds_alternative<Frame_Request>(event))
return handle_frame_request();
auto lock = acquire_foreground_lock();
return std::visit(
[this](const auto& value) -> std::optional<Web_Response> {
using T = std::decay_t<decltype(value)>;
@@ -348,7 +337,7 @@ struct Gallery_Plot_Session::Impl {
event);
}
};
Gallery_Plot_Session::Gallery_Plot_Session() : impl_(std::make_shared<Impl>()) {}
Gallery_Plot_Session::Gallery_Plot_Session() : impl_(std::make_unique<Impl>()) {}
Gallery_Plot_Session::~Gallery_Plot_Session() = default;
std::optional<Web_Response> Gallery_Plot_Session::handle(const Web_Event& event) {
return impl_->handle(event);