feat(daemon): add editor-extension watch poll protocol
This commit is contained in:
1 parent
e15e2cb2b4
commit
c7a6bad3c0
9 files changed
+122
-14
No files matched your search
+48
-9
@@ -57,6 +57,7 @@ constexpr std::size_t kMaxDescribeProfileBytes = 128U;
|
||||
constexpr std::size_t kMaxDescribeTargetNames = 128U;
|
||||
constexpr std::size_t kMaxDescribeTargetNameBytes = 200U;
|
||||
constexpr std::size_t kMaxBuildTargets = 64U;
|
||||
constexpr std::size_t kMaxPendingChanges = 4096U;
|
||||
|
||||
Void::Result::Error make_error(Void::Result::ErrorKind kind, std::string message,
|
||||
int system_errno = 0) {
|
||||
@@ -769,6 +770,7 @@ struct DaemonServer::Impl {
|
||||
[[nodiscard]] DaemonStatus current_status() const;
|
||||
[[nodiscard]] std::string status_response() const;
|
||||
DispatchResult dispatch(std::string_view line);
|
||||
void record_watch_batch(const Watch::WatchBatch &batch);
|
||||
|
||||
void close_socket() noexcept {
|
||||
if (listen_fd >= 0) {
|
||||
@@ -798,6 +800,7 @@ struct DaemonServer::Impl {
|
||||
std::uint64_t watch_events{0};
|
||||
std::string last_event_path;
|
||||
std::uint64_t sequence{0};
|
||||
std::vector<std::filesystem::path> pending_changes;
|
||||
};
|
||||
|
||||
DaemonStatus DaemonServer::Impl::current_status() const {
|
||||
@@ -813,6 +816,28 @@ DaemonStatus DaemonServer::Impl::current_status() const {
|
||||
return status;
|
||||
}
|
||||
|
||||
void DaemonServer::Impl::record_watch_batch(const Watch::WatchBatch &batch) {
|
||||
if (batch.overflowed || batch.rescan_required) {
|
||||
static_cast<void>(watcher.rescan_watches());
|
||||
++sequence;
|
||||
}
|
||||
if (batch.changed_paths.empty()) {
|
||||
return;
|
||||
}
|
||||
watch_events += batch.changed_paths.size();
|
||||
last_event_path = batch.changed_paths.back().string();
|
||||
++sequence;
|
||||
for (const auto &path : batch.changed_paths) {
|
||||
pending_changes.push_back(path);
|
||||
}
|
||||
if (pending_changes.size() > kMaxPendingChanges) {
|
||||
pending_changes.erase(
|
||||
pending_changes.begin(),
|
||||
pending_changes.begin() +
|
||||
static_cast<std::ptrdiff_t>(pending_changes.size() - kMaxPendingChanges));
|
||||
}
|
||||
}
|
||||
|
||||
std::string DaemonServer::Impl::status_response() const {
|
||||
const DaemonStatus status = current_status();
|
||||
JsonValue::Object object;
|
||||
@@ -854,6 +879,28 @@ DaemonServer::Impl::DispatchResult DaemonServer::Impl::dispatch(std::string_view
|
||||
if (*name == "status") {
|
||||
return {status_response(), false};
|
||||
}
|
||||
if (*name == "watch_poll") {
|
||||
auto batch =
|
||||
watcher.poll_events(std::chrono::milliseconds{0}, std::chrono::milliseconds{0});
|
||||
if (!batch) {
|
||||
return {error_response(std::format("watch_poll: {}", batch.error().format())), false};
|
||||
}
|
||||
record_watch_batch(*batch);
|
||||
JsonValue::Array changed;
|
||||
for (const auto &path : pending_changes) {
|
||||
changed.emplace_back(path.string());
|
||||
}
|
||||
pending_changes.clear();
|
||||
JsonValue::Object object;
|
||||
object.emplace("ok", JsonValue{true});
|
||||
object.emplace("op", JsonValue{std::string{"watch_poll"}});
|
||||
object.emplace("sequence", JsonValue{static_cast<std::int64_t>(sequence)});
|
||||
object.emplace("watch_events", JsonValue{static_cast<std::int64_t>(watch_events)});
|
||||
object.emplace("changed", JsonValue{std::move(changed)});
|
||||
object.emplace("overflowed", JsonValue{batch->overflowed});
|
||||
object.emplace("rescan_required", JsonValue{batch->rescan_required});
|
||||
return {serialize_response(JsonValue{std::move(object)}), false};
|
||||
}
|
||||
if (*name == "describe" || *name == "build" || *name == "ide_query" ||
|
||||
*name == "ide_generate" || *name == "compdb_export") {
|
||||
std::string profile_name{"debug"};
|
||||
@@ -1195,15 +1242,7 @@ Void::Result::Result<void, Void::Result::Error> DaemonServer::run(std::stop_toke
|
||||
// Watcher failures are surfaced through the next scan; the daemon stays alive.
|
||||
continue;
|
||||
}
|
||||
if (batch->overflowed || batch->rescan_required) {
|
||||
static_cast<void>(impl.watcher.rescan_watches());
|
||||
++impl.sequence;
|
||||
}
|
||||
if (!batch->changed_paths.empty()) {
|
||||
impl.watch_events += batch->changed_paths.size();
|
||||
impl.last_event_path = batch->changed_paths.back().string();
|
||||
++impl.sequence;
|
||||
}
|
||||
impl.record_watch_batch(*batch);
|
||||
}
|
||||
impl.close_socket();
|
||||
return {};
|
||||
|
||||
Reference in new issue
Block a user