Skip to content

Conversation

Jing-ze
Copy link
Collaborator

@Jing-ze Jing-ze commented Sep 10, 2025

Ⅰ. Describe what this PR did

This PR optimizes the MCP server host pattern matching and fixes SSE message formatting issues:

Optimizations:

  • Pre-parse host patterns during configuration phase to avoid runtime parsing overhead
  • Remove unused DomainList field and optimize memory usage
  • Improve URL parsing to include complete URL information

Bug Fixes:

  • Fix extra newline characters in SSE message data field by using json.Marshal instead of json.NewEncoder
  • Ensure proper SSE message formatting for client consumption

Ⅱ. Does this pull request fix one issue?

No specific issue number.

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

The changes are primarily performance optimizations and bug fixes that maintain existing functionality:

  • All matching logic remains functionally equivalent
  • JSON encoding changes only affect formatting, not data structure
  • URL parsing improvements are backward compatible

Ⅳ. Describe how to verify it

Ⅴ. Special notes for reviews

Copy link

lingma-agents bot commented Sep 10, 2025

优化 MCP 服务器主机匹配并修复 SSE 消息格式问题

变更概述
  • 性能优化

    • 在配置阶段预解析主机模式,避免运行时解析开销
    • 移除未使用的 DomainList 字段以优化内存使用
    • 使用预解析的 HostMatcher 结构提高匹配效率
  • 问题修复

    • 修复 SSE 消息数据字段中的多余换行符问题,改用 json.Marshal 替代 json.NewEncoder
    • 确保 SSE 消息格式正确,便于客户端处理
  • 重构

    • 重构 URL 解析逻辑,包含完整的 URL 信息
    • 引入 HostMatcher 结构体用于高效域名匹配
    • 更新匹配逻辑以使用新的 HostMatcher API
  • 其他

    • 更新相关结构体和函数以支持新的主机匹配机制
    • 修改 SSE 消息处理逻辑以确保正确的数据格式
变更文件
文件路径 变更说明
plugins/​golang-filter/​mcp-server/​config.​go 新增 `HostMatchers` 字段用于存储预解析的主机匹配器,并在配置解析阶段直接构建这些匹配器,提高运行时匹配效率。
plugins/​golang-filter/​mcp-server/​filter.​go 更新过滤器逻辑,使用预解析的 `HostMatchers` 进行域名匹配,提高匹配效率。
plugins/​golang-filter/​mcp-session/​common/​match.​go 引入 `HostMatcher` 结构体和相关函数,实现高效的域名匹配逻辑。更新 `MatchRule` 结构体以包含 `HostMatcher`,并修改匹配函数以使用新的匹配机制。
plugins/​golang-filter/​mcp-session/​common/​sse.​go 修改 SSE 消息处理逻辑,使用 `json.Marshal` 替代 `json.NewEncoder` 来避免多余的换行符,确保 SSE 消息格式正确。
plugins/​golang-filter/​mcp-session/​common/​utils.​go 更新 URL 解析逻辑,构建完整的 URL 信息并存储在 `RequestURL` 结构体中。
plugins/​golang-filter/​mcp-session/​filter.​go 修改 SSE 消息发布逻辑,确保事件数据格式正确。
时序图
sequenceDiagram
    participant C as ConfigParser
    participant M as MatchLogic
    participant F as Filter
    participant S as SSEServer
    
    C->>M: ParseHostPattern("*")
    M-->>C: HostMatcher{matchType: HostSuffix, pattern: ""}
    
    C->>F: Parse(config)
    Note over F: Build HostMatchers during config phase
    
    F->>F: DecodeHeaders()
    F->>M: MatchDomainWithMatchers(host, hostMatchers)
    M-->>F: true/false
    
    F->>S: HandleMessage()
    S->>S: json.Marshal(response)
    S->>S: w.Write(jsonData)
Loading

💡 小贴士

与 lingma-agents 交流的方式

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

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

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

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

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

  • @lingma-agents 对这个方法生成优化代码。

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

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

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

@codecov-commenter
Copy link

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 44.96%. Comparing base (ef31e09) to head (b3c0b52).
⚠️ Report is 699 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2899      +/-   ##
==========================================
+ Coverage   35.91%   44.96%   +9.05%     
==========================================
  Files          69       82      +13     
  Lines       11576    13347    +1771     
==========================================
+ Hits         4157     6002    +1845     
+ Misses       7104     6997     -107     
- Partials      315      348      +33     

see 80 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.

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.

2 participants