tree-sitter and lsp-methods

This commit is contained in:
csh
2025-09-14 12:28:37 +08:00
parent e9972fd869
commit aac12137cb
214 changed files with 350938 additions and 142202 deletions
+319
View File
@@ -0,0 +1,319 @@
#include <spdlog/spdlog.h>
#include "./request.hpp"
namespace lsp::scheduler
{
namespace
{
constexpr const char* kLogTag = "RequestScheduler";
// Helper for formatting durations
template<typename Duration>
std::string FormatDuration(Duration duration)
{
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
if (ms < 1000)
return std::to_string(ms) + "ms";
else
return std::to_string(ms / 1000.0) + "s";
}
}
// ============= Request Implementation =============
Request::Request(size_t concurrency) :
executor_(concurrency)
{
spdlog::info("{}: Initialized with {} threads (hardware: {})", kLogTag, concurrency, std::thread::hardware_concurrency());
}
Request::~Request()
{
spdlog::info("{}: Shutting down...", kLogTag);
LogStatus();
WaitAll();
spdlog::info("{}: Destroyed", kLogTag);
}
void Request::Submit(const std::string& request_id, TaskFunc task)
{
auto submit_start = Clock::now();
spdlog::debug("[{}] Submitting task", request_id);
auto context = std::make_shared<TaskContext>();
bool replaced_existing = RegisterTask(request_id, context);
LogTaskStart(request_id, replaced_existing);
try
{
executor_.async([this, request_id, task = std::move(task), context]() {
ExecuteTask(request_id, std::move(task), context);
});
auto duration = GetElapsedTime(submit_start);
spdlog::debug("[{}] Submitted in {}", request_id, FormatDuration(duration));
}
catch (const std::exception& e)
{
spdlog::error("[{}] Failed to submit: {}", request_id, e.what());
UnregisterTask(request_id);
total_failed_++;
throw;
}
}
bool Request::Cancel(const std::string& request_id)
{
bool cancelled = MarkTaskCancelled(request_id);
if (cancelled)
spdlog::info("[{}] Cancelled successfully", request_id);
else
spdlog::debug("[{}] Cancel requested but task not found", request_id);
return cancelled;
}
void Request::SetResponseCallback(ResponseCallback callback)
{
std::lock_guard lock(mutex_);
response_callback_ = std::move(callback);
}
void Request::WaitAll()
{
spdlog::info("{}: Waiting for all tasks...", kLogTag);
auto wait_start = Clock::now();
executor_.wait_for_all();
auto duration = GetElapsedTime(wait_start);
spdlog::info("{}: All tasks completed in {}", kLogTag, FormatDuration(duration));
LogStatus();
}
size_t Request::GetRunningTaskCount() const
{
std::lock_guard lock(mutex_);
return running_tasks_.size();
}
Request::Statistics Request::GetStatistics() const
{
std::lock_guard lock(mutex_);
return {
.running = running_tasks_.size(),
.submitted = total_submitted_.load(),
.completed = total_completed_.load(),
.failed = total_failed_.load(),
.cancelled = total_cancelled_.load()
};
}
void Request::ExecuteTask(const std::string& request_id, TaskFunc task, TaskContextPtr context)
{
auto task_start = Clock::now();
spdlog::info("[{}] Task execution started", request_id);
try
{
// Check if already cancelled
if (context->cancelled.load())
{
spdlog::debug("[{}] Task was pre-cancelled", request_id);
total_cancelled_++;
LogTaskCompletion(request_id, task_start, false);
UnregisterTask(request_id);
return;
}
// Execute the task
spdlog::debug("[{}] Executing task function", request_id);
auto result = task();
// Check if cancelled during execution
if (context->cancelled.load())
{
spdlog::debug("[{}] Task cancelled during execution", request_id);
total_cancelled_++;
}
else if (result)
{
spdlog::trace("[{}] Task produced {} bytes result", request_id, result->size());
SendResponse(*result);
total_completed_++;
}
else
{
spdlog::warn("[{}] Task completed with no result", request_id);
total_completed_++;
}
LogTaskCompletion(request_id, task_start, true);
}
catch (const std::exception& e)
{
spdlog::error("[{}] Task failed: {}", request_id, e.what());
total_failed_++;
LogTaskCompletion(request_id, task_start, false);
}
catch (...)
{
spdlog::error("[{}] Task failed with unknown exception", request_id);
total_failed_++;
LogTaskCompletion(request_id, task_start, false);
}
UnregisterTask(request_id);
// Periodic status logging
if (total_completed_.load() % kStatusLogInterval == 0)
{
LogStatus();
}
}
bool Request::RegisterTask(const std::string& request_id, TaskContextPtr context)
{
std::lock_guard lock(mutex_);
bool replaced = false;
auto it = running_tasks_.find(request_id);
if (it != running_tasks_.end())
{
spdlog::warn("[{}] Cancelling existing task", request_id);
it->second->cancelled.store(true);
total_cancelled_++;
replaced = true;
}
running_tasks_[request_id] = context;
total_submitted_++;
spdlog::debug("[{}] Registered (running: {}, total: {})", request_id, running_tasks_.size(), total_submitted_.load());
return replaced;
}
void Request::UnregisterTask(const std::string& request_id)
{
std::lock_guard lock(mutex_);
auto it = running_tasks_.find(request_id);
if (it != running_tasks_.end())
{
auto lifetime = GetElapsedTime(it->second->start_time);
spdlog::trace("[{}] Task lifetime: {}", request_id, FormatDuration(lifetime));
running_tasks_.erase(it);
spdlog::debug("[{}] Unregistered (remaining: {})", request_id, running_tasks_.size());
}
else
{
spdlog::warn("[{}] Attempted to unregister non-existent task", request_id);
}
}
bool Request::MarkTaskCancelled(const std::string& request_id)
{
std::lock_guard lock(mutex_);
auto it = running_tasks_.find(request_id);
if (it != running_tasks_.end())
{
it->second->cancelled.store(true);
return true;
}
return false;
}
void Request::SendResponse(const std::string& response) const
{
ResponseCallback callback;
{
std::lock_guard lock(mutex_);
callback = response_callback_;
}
if (!callback)
{
spdlog::error("{}: No response callback set! Response lost ({} bytes)", kLogTag, response.size());
return;
}
spdlog::trace("{}: Sending response ({} bytes)", kLogTag, response.size());
try
{
callback(response);
spdlog::trace("{}: Response sent successfully", kLogTag);
}
catch (const std::exception& e)
{
spdlog::error("{}: Response callback failed: {}", kLogTag, e.what());
}
catch (...)
{
spdlog::error("{}: Response callback failed with unknown exception", kLogTag);
}
}
void Request::LogTaskStart(const std::string& request_id, bool replaced_existing) const
{
auto stats = GetStatistics();
if (replaced_existing)
spdlog::info("[{}] Submitted (replaced existing) - Running: {}, Total: {}", request_id, stats.running, stats.submitted);
else
spdlog::debug("[{}] Submitted - Running: {}, Total: {}", request_id, stats.running, stats.submitted);
}
void Request::LogTaskCompletion(const std::string& request_id, TimePoint start_time, bool success) const
{
auto duration = GetElapsedTime(start_time);
if (success)
spdlog::info("[{}] Completed in {}", request_id, FormatDuration(duration));
else
spdlog::warn("[{}] Failed/cancelled after {}", request_id, FormatDuration(duration));
}
std::chrono::milliseconds Request::GetElapsedTime(TimePoint start_time) const
{
return std::chrono::duration_cast<std::chrono::milliseconds>(Clock::now() - start_time);
}
void Request::LogStatus() const
{
auto stats = GetStatistics();
auto total = stats.submitted;
spdlog::debug("┌─────────────────────────────────────────┐");
spdlog::debug("│ {} Status │", kLogTag);
spdlog::debug("├─────────────────────────────────────────┤");
spdlog::debug("│ Running │ {:>8} │ {:>6.1f}% │", stats.running, total > 0 ? (stats.running * 100.0 / total) : 0);
spdlog::debug("│ Submitted │ {:>8} │ │", stats.submitted);
spdlog::debug("│ Completed │ {:>8} │ {:>6.1f}% │", stats.completed, total > 0 ? (stats.completed * 100.0 / total) : 0);
spdlog::debug("│ Failed │ {:>8} │ {:>6.1f}% │", stats.failed, total > 0 ? (stats.failed * 100.0 / total) : 0);
spdlog::debug("│ Cancelled │ {:>8} │ {:>6.1f}% │", stats.cancelled, total > 0 ? (stats.cancelled * 100.0 / total) : 0);
if (stats.running > 0)
{
spdlog::debug("├─────────────────────────────────────────┤");
spdlog::debug("│ Active Tasks │");
spdlog::debug("├─────────────────────────────────────────┤");
std::lock_guard lock(mutex_);
for (const auto& [id, context] : running_tasks_)
{
auto runtime = GetElapsedTime(context->start_time);
auto status = context->cancelled.load() ? "CANCEL" : "ACTIVE";
spdlog::debug("│ {:>6} │ {:>12} │ {:>8} │", status, id.substr(0, 12), FormatDuration(runtime));
}
}
spdlog::debug("└─────────────────────────────────────────┘");
}
}
+92
View File
@@ -0,0 +1,92 @@
// request_scheduler.hpp
#pragma once
#include <atomic>
#include <functional>
#include <mutex>
#include <optional>
#include <string>
#include <unordered_map>
#include <chrono>
#include <taskflow/taskflow.hpp>
namespace lsp::scheduler
{
class Request
{
public:
using TaskFunc = std::function<std::optional<std::string>()>;
using ResponseCallback = std::function<void(const std::string&)>;
using Clock = std::chrono::steady_clock;
using TimePoint = Clock::time_point;
explicit Request(size_t concurrency = std::thread::hardware_concurrency());
~Request();
// Delete copy operations
Request(const Request&) = delete;
Request& operator=(const Request&) = delete;
// Public interface
void Submit(const std::string& request_id, TaskFunc task);
bool Cancel(const std::string& request_id);
void SetResponseCallback(ResponseCallback callback);
void WaitAll();
// Status and statistics
size_t GetRunningTaskCount() const;
void LogStatus() const;
struct Statistics
{
size_t running = 0;
uint64_t submitted = 0;
uint64_t completed = 0;
uint64_t failed = 0;
uint64_t cancelled = 0;
};
Statistics GetStatistics() const;
private:
struct TaskContext
{
std::atomic<bool> cancelled{ false };
TimePoint start_time;
TaskContext() :
start_time(Clock::now()) {}
};
using TaskContextPtr = std::shared_ptr<TaskContext>;
// Task lifecycle management
void ExecuteTask(const std::string& request_id, TaskFunc task, TaskContextPtr context);
bool RegisterTask(const std::string& request_id, TaskContextPtr context);
void UnregisterTask(const std::string& request_id);
bool MarkTaskCancelled(const std::string& request_id);
// Response handling
void SendResponse(const std::string& response) const;
// Logging helpers
void LogTaskStart(const std::string& request_id, bool replaced_existing) const;
void LogTaskCompletion(const std::string& request_id, TimePoint start_time, bool success) const;
std::chrono::milliseconds GetElapsedTime(TimePoint start_time) const;
private:
// Core components
tf::Executor executor_;
mutable std::mutex mutex_;
std::unordered_map<std::string, TaskContextPtr> running_tasks_;
ResponseCallback response_callback_;
// Statistics
std::atomic<uint64_t> total_submitted_{ 0 };
std::atomic<uint64_t> total_completed_{ 0 };
std::atomic<uint64_t> total_failed_{ 0 };
std::atomic<uint64_t> total_cancelled_{ 0 };
// Configuration
static constexpr size_t kStatusLogInterval = 5;
};
}
@@ -1,101 +0,0 @@
#include <spdlog/spdlog.h>
#include "./request_scheduler.hpp"
namespace lsp::scheduler
{
RequestScheduler::RequestScheduler(size_t concurrency) :
executor_(concurrency)
{
spdlog::info("RequestScheduler initialized with {} threads", concurrency);
}
RequestScheduler::~RequestScheduler()
{
WaitAll();
}
void RequestScheduler::Submit(const std::string& request_id, TaskFunc task)
{
auto context = std::make_shared<TaskContext>();
{
std::lock_guard<std::mutex> lock(mutex_);
// 取消旧任务
auto it = running_tasks_.find(request_id);
if (it != running_tasks_.end())
{
it->second->cancelled.store(true);
}
running_tasks_[request_id] = context;
}
executor_.async([this, request_id, task = std::move(task), context]() {
try
{
if (context->cancelled.load())
{
spdlog::debug("Task {} was cancelled", request_id);
return;
}
auto result = task();
if (!context->cancelled.load() && result)
{
SendResponse(*result);
}
}
catch (const std::exception& e)
{
spdlog::error("Task {} failed: {}", request_id, e.what());
}
// 清理
{
std::lock_guard<std::mutex> lock(mutex_);
running_tasks_.erase(request_id);
}
});
}
bool RequestScheduler::Cancel(const std::string& request_id)
{
std::lock_guard<std::mutex> lock(mutex_);
auto it = running_tasks_.find(request_id);
if (it != running_tasks_.end())
{
it->second->cancelled.store(true);
return true;
}
return false;
}
void RequestScheduler::SetResponseCallback(ResponseCallback callback)
{
std::lock_guard<std::mutex> lock(mutex_);
response_callback_ = std::move(callback);
}
void RequestScheduler::WaitAll()
{
executor_.wait_for_all();
}
void RequestScheduler::SendResponse(const std::string& response)
{
ResponseCallback callback;
{
std::lock_guard<std::mutex> lock(mutex_);
callback = response_callback_;
}
if (callback)
callback(response);
else
spdlog::error("No response callback set!");
}
}
@@ -1,41 +0,0 @@
// request_scheduler.hpp
#pragma once
#include <atomic>
#include <functional>
#include <mutex>
#include <optional>
#include <string>
#include <unordered_map>
#include <taskflow/taskflow.hpp>
namespace lsp::scheduler
{
class RequestScheduler
{
public:
using TaskFunc = std::function<std::optional<std::string>()>;
using ResponseCallback = std::function<void(const std::string&)>;
explicit RequestScheduler(size_t concurrency = std::thread::hardware_concurrency());
~RequestScheduler();
void Submit(const std::string& request_id, TaskFunc task);
bool Cancel(const std::string& request_id);
void SetResponseCallback(ResponseCallback callback);
void WaitAll();
private:
struct TaskContext
{
std::atomic<bool> cancelled{false};
};
void SendResponse(const std::string& response);
private:
tf::Executor executor_;
mutable std::mutex mutex_;
std::unordered_map<std::string, std::shared_ptr<TaskContext>> running_tasks_;
ResponseCallback response_callback_;
};
}