feat(daemon): add bounded long-poll watch request for editor extensions
This commit is contained in:
1 parent
c7a6bad3c0
commit
64116f6e0d
9 files changed
+129
-3
No files matched your search
@@ -58,6 +58,8 @@ constexpr std::size_t kMaxDescribeTargetNames = 128U;
|
||||
constexpr std::size_t kMaxDescribeTargetNameBytes = 200U;
|
||||
constexpr std::size_t kMaxBuildTargets = 64U;
|
||||
constexpr std::size_t kMaxPendingChanges = 4096U;
|
||||
constexpr std::int64_t kMaxWatchWaitMs = 5000;
|
||||
constexpr std::int64_t kDefaultWatchWaitMs = 2000;
|
||||
|
||||
Void::Result::Error make_error(Void::Result::ErrorKind kind, std::string message,
|
||||
int system_errno = 0) {
|
||||
@@ -901,6 +903,44 @@ DaemonServer::Impl::DispatchResult DaemonServer::Impl::dispatch(std::string_view
|
||||
object.emplace("rescan_required", JsonValue{batch->rescan_required});
|
||||
return {serialize_response(JsonValue{std::move(object)}), false};
|
||||
}
|
||||
if (*name == "watch_wait") {
|
||||
std::int64_t timeout_ms = kDefaultWatchWaitMs;
|
||||
if (const auto entry = object->find("timeout_ms"); entry != object->end()) {
|
||||
const auto *value = std::get_if<std::int64_t>(&entry->second.value);
|
||||
if (value == nullptr || *value < 0 || *value > kMaxWatchWaitMs) {
|
||||
return {error_response(std::format("\"timeout_ms\" must be an integer in [0, {}]",
|
||||
kMaxWatchWaitMs)),
|
||||
false};
|
||||
}
|
||||
timeout_ms = *value;
|
||||
}
|
||||
|
||||
bool timed_out = false;
|
||||
if (pending_changes.empty() && timeout_ms > 0) {
|
||||
auto batch = watcher.poll_events(std::chrono::milliseconds{timeout_ms},
|
||||
std::chrono::milliseconds{0});
|
||||
if (!batch) {
|
||||
return {error_response(std::format("watch_wait: {}", batch.error().format())),
|
||||
false};
|
||||
}
|
||||
timed_out =
|
||||
batch->changed_paths.empty() && !batch->overflowed && !batch->rescan_required;
|
||||
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_wait"}});
|
||||
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("timed_out", JsonValue{timed_out});
|
||||
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"};
|
||||
|
||||
@@ -1365,6 +1365,7 @@ struct DaemonCmdOptions {
|
||||
std::vector<std::string> targets;
|
||||
std::string jobs_arg{"0"};
|
||||
std::string editor_arg{"all"};
|
||||
std::string timeout_ms_arg{"2000"};
|
||||
bool force{false};
|
||||
std::string max_bytes_arg{"0"};
|
||||
std::string max_requests_arg{"0"};
|
||||
@@ -1447,6 +1448,18 @@ int execute_daemon_command(std::string_view sub, const DaemonCmdOptions &opts) {
|
||||
std::format(R"({{"op":"describe","profile":{}}})", Util::json_quote(opts.profile_arg));
|
||||
} else if (sub == "watch_poll") {
|
||||
request_json = R"({"op":"watch_poll"})";
|
||||
} else if (sub == "watch_wait") {
|
||||
std::int64_t timeout_ms = 0;
|
||||
const auto [ptr, ec] =
|
||||
std::from_chars(opts.timeout_ms_arg.data(),
|
||||
opts.timeout_ms_arg.data() + opts.timeout_ms_arg.size(), timeout_ms);
|
||||
if (ec != std::errc{} || ptr != opts.timeout_ms_arg.data() + opts.timeout_ms_arg.size() ||
|
||||
timeout_ms < 0 || timeout_ms > 5000) {
|
||||
std::cerr << "vbs daemon ide wait: invalid --timeout-ms value '" << opts.timeout_ms_arg
|
||||
<< "' (0..5000)\n";
|
||||
return 2;
|
||||
}
|
||||
request_json = std::format(R"({{"op":"watch_wait","timeout_ms":{}}})", timeout_ms);
|
||||
} else if (sub == "ide_generate") {
|
||||
if (opts.profile_arg.empty()) {
|
||||
std::cerr << "vbs daemon ide generate: --profile must not be empty\n";
|
||||
@@ -1496,6 +1509,7 @@ int execute_daemon_command(std::string_view sub, const DaemonCmdOptions &opts) {
|
||||
const auto request_timeout =
|
||||
sub == "build" ? std::chrono::milliseconds{std::chrono::hours{1}}
|
||||
: sub == "watch_poll" ? std::chrono::milliseconds{5000}
|
||||
: sub == "watch_wait" ? std::chrono::milliseconds{10000}
|
||||
: sub == "describe" ? std::chrono::milliseconds{15000}
|
||||
: (sub == "ide_query" || sub == "ide_generate" || sub == "compdb_export")
|
||||
? std::chrono::milliseconds{30000}
|
||||
@@ -2459,6 +2473,11 @@ int main(int argc, char **argv) {
|
||||
auto &daemon_ide_poll_cmd = daemon_ide_cmd.subcommand(
|
||||
"poll", "Poll pending project file changes (editor extension protocol)");
|
||||
configure_daemon_common(daemon_ide_poll_cmd);
|
||||
auto &daemon_ide_wait_cmd = daemon_ide_cmd.subcommand(
|
||||
"wait", "Long-poll for project file changes (bounded, editor extension protocol)");
|
||||
configure_daemon_common(daemon_ide_wait_cmd);
|
||||
daemon_ide_wait_cmd.option_default("--timeout-ms", "", "Maximum wait in milliseconds (0..5000)",
|
||||
daemon_opts.timeout_ms_arg, "2000");
|
||||
auto &daemon_ide_generate_cmd =
|
||||
daemon_ide_cmd.subcommand("generate", "Write editor configuration files");
|
||||
configure_daemon_common(daemon_ide_generate_cmd);
|
||||
@@ -2569,6 +2588,9 @@ int main(int argc, char **argv) {
|
||||
} else if (daemon_sub == "ide" && parsed.matched_command_path.size() > 2 &&
|
||||
parsed.matched_command_path[2] == "poll") {
|
||||
daemon_sub = "watch_poll";
|
||||
} else if (daemon_sub == "ide" && parsed.matched_command_path.size() > 2 &&
|
||||
parsed.matched_command_path[2] == "wait") {
|
||||
daemon_sub = "watch_wait";
|
||||
} else if (daemon_sub == "compdb" && parsed.matched_command_path.size() > 2 &&
|
||||
parsed.matched_command_path[2] == "export") {
|
||||
daemon_sub = "compdb_export";
|
||||
|
||||
Reference in new issue
Block a user