Skip to content

Conversation

@Erica177
Copy link
Collaborator

Ⅰ. Describe what this PR did

Ⅱ. Does this pull request fix one issue?

Ⅲ. Why don't you add test cases (unit test/integration test)?

Ⅳ. Describe how to verify it

Ⅴ. Special notes for reviews

@lingma-agents
Copy link

lingma-agents bot commented Jun 10, 2025

修复Nacos MCP凭证处理逻辑并添加单元测试

变更文件

文件路径 变更说明
registry/memory/cache.go 在获取配置时增加对McpServerRule规则集合为空的判断,记录日志后直接返回空配置,避免后续处理错误
registry/nacos/mcpserver/client.go 1. 新增凭证删除队列管理机制 2. 调整配置监听触发顺序确保数据更新优先 3. 合并重复条件判断逻辑
registry/nacos/mcpserver/client_test.go 包含分页列表、配置变更监听、凭证解析、异常处理等13个测试用例,覆盖服务器版本更新、模板替换、凭证关联等核心功能

时序图

sequenceDiagram
    participant NacosClient as NacosRegistryClient
    participant Cache as MemoryCache
    participant Listener as ConfigListener
    NacosClient->>Cache: GetAllConfigs()
    Cache->>Cache: Check pluginConfig.Rules length
    Cache-->>NacosClient: Return empty map if no rules
    NacosClient->>Listener: replaceTemplateAndExactConfigsItems
    Listener->>NacosClient: Collect credentialsNeedDelete
    NacosClient->>Listener: Trigger config update
    Listener->>NacosClient: Update credentials map
    NacosClient->>NacosClient: refreshServiceListenerIfNeeded
Loading

💡 小贴士

与 lingma-agents 交流的方式

📜 直接回复评论
直接回复本条评论,lingma-agents 将自动处理您的请求。例如:

  • 在当前代码中添加详细的注释说明。

  • 请详细介绍一下你说的 LRU 改造方案,并使用伪代码加以说明。

📜 在代码行处标记
在文件的特定位置创建评论并 @lingma-agents。例如:

  • @Lingma-Agent 分析这个方法的性能瓶颈并提供优化建议。

  • @Lingma-Agent 对这个方法生成优化代码。

📜 在讨论中提问
在任何讨论中 @lingma-agents 来获取帮助。例如:

  • @Lingma-Agent 请总结上述讨论并提出解决方案。

  • @Lingma-Agent 请根据讨论内容生成优化代码。

Copy link

@lingma-agents lingma-agents bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 代码评审报告

📋 评审意见详情

💡 单文件建议

✅ 未发现需要特别关注的代码问题。

🚀 跨文件建议

以下是对代码架构和设计的综合分析,聚焦于跨文件交互、系统一致性和潜在优化空间。
🔍 1. 潜在的资源泄漏风险:未正确清理配置监听器可能导致内存泄漏

在文件registry/nacos/mcpserver/client.goresetNacosTemplateConfigs方法中,新增的credentialsNeedDelete列表用于记录需要删除的凭证密钥,但仅删除了ctx.configsMap中的条目,却未处理对应的监听器资源。原代码在删除配置时虽然调用了n.cancelListenToConfig(wrap),但未确保监听器的完全释放,可能导致残留的监听器继续占用资源或触发不必要的回调。此外,新配置添加时直接覆盖了ctx.configsMap,但未检查是否已存在相同密钥的监听器,可能导致重复注册监听器,增加内存消耗和潜在竞争条件。

📌 关键代码:

for key, wrap := range ctx.configsMap {
    if strings.HasPrefix(key, CredentialPrefix) {
        if _, ok := newCredentials[key]; !ok {
            credentialsNeedDelete = append(credentialsNeedDelete, key)
            err := n.cancelListenToConfig(wrap)
            // 未处理err可能导致未释放资源
        }
    }
}
// 延迟删除操作可能导致监听器未及时释放
🔍 2. 递归调用风险:配置变更触发无限循环

registry/nacos/mcpserver/client.goconfigListener函数中,当groupMcpToolSpecGroup时会调用resetNacosTemplateConfigs,该方法会重新注册新的配置监听器。若新配置再次触发相同分组的监听事件,可能导致无限递归调用,最终引发堆栈溢出。例如,当修改工具模板时重新初始化监听器,可能再次触发同一类型事件。

📌 关键代码:

if group == McpToolSpecGroup {
    n.resetNacosTemplateConfigs(ctx, &wrap)
} else if group == McpServerSpecGroup {
    n.refreshServiceListenerIfNeeded(ctx, data)
}
🔍 3. 测试覆盖不完整:Mock对象未实现关键方法可能导致测试失效

新增的测试文件registry/nacos/mcpserver/client_test.go中定义的MockedNacosConfigClientMockedNacosNamingClient存在大量未实现的方法(如PublishConfigRegisterInstance等),这些方法直接panic。如果实际测试场景中调用了这些未实现的方法(例如通过间接路径),会导致测试崩溃而非失败,从而掩盖真实问题。应确保所有可能被调用的方法均有合理mock实现。

📌 关键代码:

func (m MockedNacosConfigClient) PublishConfig(param vo.ConfigParam) (bool, error) {
    panic("implement me")
}
🔍 4. 凭证处理逻辑不一致:敏感数据日志记录风险

registry/memory/cache.goGetAllConfigs方法新增日志log.Infof("there is no mcp server rule exist, skip generate wasm plugin"),但未对可能包含在pluginConfig中的敏感数据(如凭证信息)进行过滤。若pluginConfig包含未过滤的日志输出,可能导致敏感信息泄露。需确保日志记录不包含敏感数据或使用安全日志级别。

📌 关键代码:

log.Infof("there is no mcp server rule exist, skip generate wasm plugin")
return map[string]*config.Config{}
🔍 5. 分页处理性能隐患:大数据量下可能引发内存问题

测试用例TestNacosRegistryClient_ListMcpServer模拟了151条配置项的分页查询,但SearchConfig方法在MockedNacosConfigClient中将所有匹配配置加载到内存切片result中。当实际数据量极大时,此实现可能导致内存占用过高。应考虑分页流式处理或优化内存使用策略,避免单次查询加载全部数据。

📌 关键代码:

for key, value := range m.configs {
    // 将所有匹配项加载到内存切片
    result = append(result, model.ConfigItem{...})
}

💡 小贴士

与 lingma-agents 交流的方式

📜 直接回复评论
直接回复本条评论,lingma-agents 将自动处理您的请求。例如:

  • 在当前代码中添加详细的注释说明。

  • 请详细介绍一下你说的 LRU 改造方案,并使用伪代码加以说明。

📜 在代码行处标记
在文件的特定位置创建评论并 @lingma-agents。例如:

  • @Lingma-Agent 分析这个方法的性能瓶颈并提供优化建议。

  • @Lingma-Agent 对这个方法生成优化代码。

📜 在讨论中提问
在任何讨论中 @lingma-agents 来获取帮助。例如:

  • @Lingma-Agent 请总结上述讨论并提出解决方案。

  • @Lingma-Agent 请根据讨论内容生成优化代码。

@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 45.97%. Comparing base (ef31e09) to head (eb554bf).
Report is 547 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##             main    #2394       +/-   ##
===========================================
+ Coverage   35.91%   45.97%   +10.06%     
===========================================
  Files          69       81       +12     
  Lines       11576    13018     +1442     
===========================================
+ Hits         4157     5985     +1828     
+ Misses       7104     6687      -417     
- Partials      315      346       +31     

see 78 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Collaborator

@johnlanni johnlanni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@johnlanni johnlanni merged commit 1666dfb into alibaba:main Jun 10, 2025
9 checks passed
ink-hz pushed a commit to ink-hz/higress-ai-capability-auth that referenced this pull request Nov 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants