Skip to content

refactor(failover): 重构故障转移状态码配置为标准模式#413

Merged
tbphp merged 4 commits intomainfrom
dev
May 5, 2026
Merged

refactor(failover): 重构故障转移状态码配置为标准模式#413
tbphp merged 4 commits intomainfrom
dev

Conversation

@tbphp
Copy link
Copy Markdown
Owner

@tbphp tbphp commented May 5, 2026

关联 Issue / Related Issue

基于 #397 代码审查后的重构,解决设计不一致问题。

变更内容 / Change Content

  • Bug 修复 / Bug fix
  • 新功能 / New feature
  • 其他改动 / Other changes(重构)

问题

#397 引入的 failover_status_codes 存在两处设计缺陷:

  1. 配置模式不一致:项目其他可覆盖字段(max_retriesblacklist_threshold 等)均遵循"系统全局默认 + 分组覆盖"标准模式,而 failover_status_codes 被特殊处理为"仅分组级",通过 sanitize / visibility / payload validation 三套机制强制隐藏系统级入口,带来约 50 行冗余代码。

  2. 字段语义与名字错位:原实现为 OR 叠加语义(分组配置叠加在硬编码默认条件之上),导致单独配置 429 完全无效(默认条件 >=400 except 404 已覆盖),字段名"故障转移状态码"与"追加状态码"的实际行为不符。

改动

  • types.go:添加 default:"400-403,405-999",将原硬编码的默认行为显式化
  • system_settings.go:删除 sanitizeRuntimeSystemSettingsIsSystemSettingVisibleValidateSystemSettingsPayload 三套特殊化机制及所有调用点;ValidateSettings 补充 failover_status_codes 格式校验
  • settings_handler.go:删除可见性过滤循环和 payload 拒绝逻辑
  • proxy/server.goshouldFailoverOnStatusCode 改为纯 matcher 匹配,删除硬编码 >=400 except 404 条件
  • i18n(zh/en/ja):字段名去掉"增量/Extra/追加",描述改为"完整列表"语义,示例更新

升级兼容说明

  • 未配置过该字段的用户(绝大多数):EnsureSettingsInitialized 启动时自动写入默认值 400-403,405-999,行为与升级前完全等价,无感知。
  • 配置过分组级 failover_status_codes 的用户(v1.4.7 新字段,极少数):语义从"追加"变为"完整列表",需将旧配置改为完整表达式,例如原来配 404 需改为 400-599

自查清单 / Checklist

  • 本地编译通过 go build ./...
  • 逻辑已人工验证

cheluen and others added 4 commits April 6, 2026 20:32
* feat: 支持分组级故障转移状态码(增量)

* fix: 限制故障转移状态码为分组级配置
- 新增 validateStringSettingValue 辅助函数,消除 ValidateGroupConfigOverrides 中的重复校验逻辑
- 删除 ValidateSettings 中已成死代码的 failover_status_codes 分支(上游 ValidateSystemSettingsPayload 已拦截)
- 统一使用 GroupScopedSettingKeyFailoverStatusCodes 常量,消除字符串字面量
- 将 failover_status_codes 回归"系统默认 + 分组覆盖"标准模式
  - 添加 default:"400-403,405-999",与原硬编码行为等价
  - EnsureSettingsInitialized 不再跳过该字段,升级后自动写入默认值
  - GetSettings 不再 sanitize,系统设置页可见可改

- proxy 层改为纯 matcher 匹配,删除硬编码默认条件
  - shouldFailoverOnStatusCode 直接读 group.FailoverStatusCodeMatcher
  - 字段语义从"追加"变为"完整策略",名字与行为对齐

- 删除特殊化机制:sanitizeRuntimeSystemSettings、IsSystemSettingVisible、
  ValidateSystemSettingsPayload 及所有调用点

- ValidateSettings 补充 failover_status_codes 格式校验,与分组校验一致

- 更新三语 i18n 文案,示例改为 400-403,405-999,250-260

升级兼容:未配置用户行为完全不变;配置过分组级该字段的用户
需将旧的"追加"写法改为"完整列表"写法
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c1ca9ff71f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread internal/services/group_manager.go
@tbphp tbphp self-assigned this May 5, 2026
@tbphp tbphp added bug Something isn't working enhancement New feature or request labels May 5, 2026
@tbphp tbphp added this to the v1.4.7 milestone May 5, 2026
@tbphp tbphp merged commit ed87a87 into main May 5, 2026
5 checks passed
@tbphp tbphp deleted the dev branch May 5, 2026 01:11
@tbphp tbphp restored the dev branch May 5, 2026 01:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

在上游第一个地址出现404等错误时,可以自动切换到第二个地址

2 participants