pub struct ProxyHandlerManager { /* private fields */ }Expand description
MCP 代理管理器
使用 RAII 模式管理 MCP 服务:
- 从 map 中移除 mcp_id 时,自动释放对应的进程资源
- 不需要显式调用 cleanup 方法(但仍提供显式清理接口)
Implementations§
Source§impl ProxyHandlerManager
impl ProxyHandlerManager
Sourcepub fn add_mcp_service(
&self,
mcp_id: String,
mcp_type: McpType,
mcp_router_path: McpRouterPath,
cancellation_token: CancellationToken,
)
pub fn add_mcp_service( &self, mcp_id: String, mcp_type: McpType, mcp_router_path: McpRouterPath, cancellation_token: CancellationToken, )
添加 MCP 服务(RAII 模式)
使用新的 RAII 结构创建服务,当服务被移除时会自动清理资源
Sourcepub fn add_mcp_service_status_and_proxy(
&self,
mcp_service_status: McpServiceStatus,
proxy_handler: Option<McpHandler>,
)
pub fn add_mcp_service_status_and_proxy( &self, mcp_service_status: McpServiceStatus, proxy_handler: Option<McpHandler>, )
添加 MCP 服务状态(兼容旧 API)
保持与现有代码的兼容性,内部转换为新的 RAII 结构
注意:last_accessed 会被重置为当前时间(插入视为新访问)
Sourcepub fn get_all_mcp_service_status(&self) -> Vec<McpServiceStatus>
pub fn get_all_mcp_service_status(&self) -> Vec<McpServiceStatus>
获取所有的 MCP 服务状态(兼容旧 API)
优化:先快速收集所有 keys,然后逐个获取详细信息 避免 iter() 长时间锁住多个分片,让其他写操作有机会执行
Sourcepub fn get_mcp_service_status(&self, mcp_id: &str) -> Option<McpServiceStatus>
pub fn get_mcp_service_status(&self, mcp_id: &str) -> Option<McpServiceStatus>
获取 MCP 服务状态(兼容旧 API)
Sourcepub fn update_last_accessed(&self, mcp_id: &str)
pub fn update_last_accessed(&self, mcp_id: &str)
更新最后访问时间
使用 entry API 确保原子性操作
Sourcepub fn update_mcp_service_status(
&self,
mcp_id: &str,
status: CheckMcpStatusResponseStatus,
)
pub fn update_mcp_service_status( &self, mcp_id: &str, status: CheckMcpStatusResponseStatus, )
修改 MCP 服务状态 (Ready/Pending/Error)
使用 entry API 确保原子性操作
Sourcepub fn get_proxy_handler(&self, mcp_id: &str) -> Option<McpHandler>
pub fn get_proxy_handler(&self, mcp_id: &str) -> Option<McpHandler>
获取 MCP Handler
Sourcepub fn get_mcp_config(&self, mcp_id: &str) -> Option<McpConfig>
pub fn get_mcp_config(&self, mcp_id: &str) -> Option<McpConfig>
获取服务的 MCP 配置(用于自动重启)
Sourcepub fn add_proxy_handler(&self, mcp_id: &str, proxy_handler: McpHandler)
pub fn add_proxy_handler(&self, mcp_id: &str, proxy_handler: McpHandler)
添加 MCP Handler 到已存在的服务
使用 entry API 确保原子性操作
Sourcepub fn contains_service(&self, mcp_id: &str) -> bool
pub fn contains_service(&self, mcp_id: &str) -> bool
检查服务是否存在
Sourcepub fn service_count(&self) -> usize
pub fn service_count(&self) -> usize
获取服务数量
Sourcepub async fn register_mcp_config(&self, mcp_id: &str, config: McpConfig)
pub async fn register_mcp_config(&self, mcp_id: &str, config: McpConfig)
注册 MCP 配置到缓存
Sourcepub async fn get_mcp_config_from_cache(&self, mcp_id: &str) -> Option<McpConfig>
pub async fn get_mcp_config_from_cache(&self, mcp_id: &str) -> Option<McpConfig>
从缓存获取 MCP 配置
Sourcepub async fn unregister_mcp_config(&self, mcp_id: &str)
pub async fn unregister_mcp_config(&self, mcp_id: &str)
从缓存删除 MCP 配置
Sourcepub async fn cleanup_resources(&self, mcp_id: &str) -> Result<()>
pub async fn cleanup_resources(&self, mcp_id: &str) -> Result<()>
清理资源 (RAII 模式简化版)
通过 RAII 模式,从 DashMap 中移除服务会自动:
- 触发 McpProcessGuard::drop()
- 取消 CancellationToken
- 关联的子进程收到信号退出
此方法额外清理路由和缓存
Sourcepub async fn cleanup_all_resources(&self) -> Result<()>
pub async fn cleanup_all_resources(&self) -> Result<()>
系统关闭,清理所有资源
RAII 模式下,清除 DashMap 会自动释放所有资源
Sourcepub fn remove_service(&self, mcp_id: &str) -> bool
pub fn remove_service(&self, mcp_id: &str) -> bool
仅移除服务(依赖 RAII 自动清理进程)
从 DashMap 中移除服务,触发 RAII 自动清理。 不会清理路由和缓存,适用于需要快速移除服务的场景。
Sourcepub fn reset_probe_failures(&self, mcp_id: &str)
pub fn reset_probe_failures(&self, mcp_id: &str)
重置健康检查失败计数
使用 get_mut 避免为不存在的服务创建空 entry
Sourcepub fn increment_probe_failures(&self, mcp_id: &str) -> u32
pub fn increment_probe_failures(&self, mcp_id: &str) -> u32
增加健康检查失败计数,返回增加后的值
使用 entry API 确保原子性操作
Sourcepub async fn cleanup_resources_for_restart(&self, mcp_id: &str) -> Result<()>
pub async fn cleanup_resources_for_restart(&self, mcp_id: &str) -> Result<()>
清理资源用于重启(保留配置缓存)
与 cleanup_resources 不同,此方法不会清理配置缓存, 允许后续使用缓存的配置重新启动服务。
Trait Implementations§
Source§impl Debug for ProxyHandlerManager
impl Debug for ProxyHandlerManager
Auto Trait Implementations§
impl Freeze for ProxyHandlerManager
impl !RefUnwindSafe for ProxyHandlerManager
impl Send for ProxyHandlerManager
impl Sync for ProxyHandlerManager
impl Unpin for ProxyHandlerManager
impl UnsafeUnpin for ProxyHandlerManager
impl !UnwindSafe for ProxyHandlerManager
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request