- New plugins_upper/ai_common/ static library: shared PluginConfig, ToolCallAccum, StreamContext, secure_zero, extract_host_port, serialize_tool_calls, free_chat_result - Refactored openai/anthropic plugins to use dstalk_ai:: namespace from ai_common - Fixed anthropic g_config raw pointer → std::atomic (data race) - Added SSE parse error counter with threshold abort (kMaxSseParseErrors=5) - Fixed missing closing brace in both plugins' error-body catch block - Updated test targets: ai_common include path + link, using namespace dstalk_ai - plugin_loader_test: added stub_unreg + service_registry.cpp for unregister_service - Includes pre-existing uncommitted changes from prior waves Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
54 lines
1.9 KiB
C++
54 lines
1.9 KiB
C++
/* @file service_registry.hpp
|
||
* @brief Name-versioned service registry for decoupled plugin communication.
|
||
* 基于名称+版本的服务注册表,用于插件间解耦通信。
|
||
* Copyright (c) 2026 dstalk contributors. GPLv3.
|
||
*/
|
||
|
||
#pragma once
|
||
|
||
#include <mutex>
|
||
#include <shared_mutex>
|
||
#include <string>
|
||
#include <unordered_map>
|
||
#include <vector>
|
||
|
||
namespace dstalk {
|
||
|
||
// 名称 + 最低版本服务目录 / Name + minimum-version service directory.
|
||
// 插件注册 vtable;消费者按名称和版本约束查询 /
|
||
// Plugins register vtables; consumers query by name and version constraint.
|
||
// 读取(query)使用 shared_lock;写入(register/unregister)使用 unique_lock /
|
||
// Reads (query) use shared_lock; writes (register/unregister) use unique_lock.
|
||
class ServiceRegistry {
|
||
public:
|
||
ServiceRegistry() = default;
|
||
~ServiceRegistry() = default;
|
||
|
||
// 注册服务 / Register a named service at a given version
|
||
int register_service(const char* name, int version, void* vtable);
|
||
|
||
// 查询服务(返回 vtable 指针,或 nullptr)/ Query a service by name and minimum version
|
||
void* query_service(const char* name, int min_version) const;
|
||
|
||
// 注销服务 / Unregister a named service
|
||
void unregister_service(const char* name);
|
||
|
||
// 列出所有已注册服务名称(用于 diff/遍历)/ List all currently registered service names (for diff / iteration)
|
||
std::vector<std::string> list_service_names() const;
|
||
|
||
// 清空所有注册服务 / Remove all registered services
|
||
void clear();
|
||
|
||
private:
|
||
struct ServiceEntry {
|
||
std::string name;
|
||
int version;
|
||
void* vtable;
|
||
};
|
||
|
||
mutable std::shared_mutex mutex_; // 读写锁:query 用 shared,register/unregister 用 unique / RW lock: shared for query, unique for register/unregister
|
||
std::unordered_map<std::string, ServiceEntry> services_;
|
||
};
|
||
|
||
} // namespace dstalk
|