增加渠道重试优先级模式 - #2729
Conversation
WalkthroughThis PR implements an enhanced channel retry mechanism with two new configuration options: the ability to exclude already-failed channels from retry attempts, and support for both sequential and round-robin priority selection modes during retries. The feature spans backend logic, data models, service layer, and frontend UI components. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
service/channel_select.go (1)
147-201: Auto-group retries may skip lower priorities when exclusions are enabled.
model.GetRandomSatisfiedChannelnow returnsnilwhen only the current priority is exhausted due to exclusions. In the auto-group branch,nilis treated as “group has no channel” and immediately advances to the next group, which can skip lower priorities within the same group and violates the “exhaust all priorities before switching” behavior described above.Consider distinguishing “priority exhausted” vs “group empty” (e.g., a sentinel error/flag from
GetRandomSatisfiedChannel) and only advance groups when the group is truly exhausted; otherwise advance priority within the same group.
🤖 Fix all issues with AI agents
In `@model/option.go`:
- Around line 414-417: The branch handling the "RetryPriorityMode" option
currently accepts any value into common.OptionMap and only sets
common.RetryPriorityMode for "sequential" or "round-robin", causing invalid
values to persist; modify the setter in the switch for "RetryPriorityMode" to
validate the incoming value and return an error for any unsupported value
instead of silently accepting it, and update UpdateOption to validate
RetryPriorityMode (and any similar enums) before persisting to the DB — if a
validation fails, restore the previous entry in common.OptionMap (or avoid
mutating it) and return the error so the UI does not show “saved.” Ensure
references to RetryPriorityMode, common.RetryPriorityMode, common.OptionMap and
the UpdateOption function are used to locate and change the logic.
| case "RetryPriorityMode": | ||
| if value == "sequential" || value == "round-robin" { | ||
| common.RetryPriorityMode = value | ||
| } |
There was a problem hiding this comment.
Reject invalid RetryPriorityMode values instead of silently accepting them
Right now an unsupported value still gets stored in common.OptionMap and the update returns nil, so the UI sees “saved” even though runtime keeps the old mode. Since UpdateOption saves to DB before validation, this can persist a bad value and create a confusing mismatch.
Consider validating and returning an error (and restoring the OptionMap entry). Ideally validate before DB save in UpdateOption as well.
🛠️ Suggested fix
-import (
- "strconv"
- "strings"
- "time"
+import (
+ "fmt"
+ "strconv"
+ "strings"
+ "time"
@@
case "RetryPriorityMode":
- if value == "sequential" || value == "round-robin" {
- common.RetryPriorityMode = value
- }
+ if value != "sequential" && value != "round-robin" {
+ common.OptionMap[key] = common.RetryPriorityMode
+ return fmt.Errorf("invalid RetryPriorityMode: %s", value)
+ }
+ common.RetryPriorityMode = value🤖 Prompt for AI Agents
In `@model/option.go` around lines 414 - 417, The branch handling the
"RetryPriorityMode" option currently accepts any value into common.OptionMap and
only sets common.RetryPriorityMode for "sequential" or "round-robin", causing
invalid values to persist; modify the setter in the switch for
"RetryPriorityMode" to validate the incoming value and return an error for any
unsupported value instead of silently accepting it, and update UpdateOption to
validate RetryPriorityMode (and any similar enums) before persisting to the DB —
if a validation fails, restore the previous entry in common.OptionMap (or avoid
mutating it) and return the error so the UI does not show “saved.” Ensure
references to RetryPriorityMode, common.RetryPriorityMode, common.OptionMap and
the UpdateOption function are used to locate and change the logic.
功能描述
本 PR 包含两个相关的渠道重试增强功能:
在渠道重试过程中,系统会自动排除已经尝试过的渠道,避免重复请求同一个失败的渠道。
功能特性:
实现了两种渠道重试的优先级模式,用户可根据业务需求选择:
Sequential(顺序模式):
Round-robin(轮询模式):
实现细节
功能 1: 重试避开已尝试渠道
修改的文件:
- 新增 RetryAvoidUsedChannelEnabled 配置常量
- 添加配置项的加载和更新逻辑
- 在重试循环中记录已使用的渠道 ID
- 将排除列表传递给渠道选择函数
- 新增 UsedChannelIds 字段到 RetryParam 结构体
- 将排除列表传递给底层查询函数
- 在渠道查询时应用排除逻辑
- 使用 NOT IN 子句过滤已使用的渠道
- web/src/pages/Setting/Operation/SettingsMonitoring.jsx - 新增开关按钮
- web/src/i18n/locales/zh.json & en.json - 国际化文本
功能 2: 渠道重试优先级模式
修改的文件:
- 修复了 RetryPriorityMode 配置加载问题
- 原本错误地放在了只处理 "Enabled" 结尾配置项的条件块内
- 在 getPriority 函数中添加了 round-robin 模式的模运算逻辑
- 确保在轮询模式下正确循环优先级
- 在 GetRandomSatisfiedChannel 函数中添加了 round-robin 模式的模运算逻辑
- 支持内存缓存场景下的轮询模式
- 移除了 round-robin 模式下的 IncreasePriorityIndex() 调用
- 避免双重优先级切换导致的跳过问题
- 区分两种模式的参数传递
- Round-robin 模式传递 retry 参数
- Sequential 模式传递 priorityIndex 参数
- web/src/pages/Setting/Operation/SettingsMonitoring.jsx - 优先级模式选择器
- web/src/i18n/locales/zh.json & en.json - 国际化文本
核心逻辑
Round-robin 模式:
if common.RetryPriorityMode == "round-robin" && len(priorities) > 0 {
priorityToUse = priorities[retry%len(priorities)]
}
Sequential 模式:
if retry >= len(priorities) {
priorityToUse = priorities[len(priorities)-1]
} else {
priorityToUse = priorities[retry]
}
渠道排除逻辑:
if len(excludeIds) > 0 {
var excludeIdList []int
for id := range excludeIds {
excludeIdList = append(excludeIdList, id)
}
channelQuery = channelQuery.Where("channel_id NOT IN ?", excludeIdList)
}
测试结果
功能 1: 重试避开已尝试渠道
功能 2: 渠道重试优先级模式
测试环境:
Round-robin 模式测试结果:
retry=0 → 渠道 #2 (优先级 10) ✓
retry=1 → 渠道 #5 (优先级 0) ✓
retry=2 → 渠道 #3 (优先级 10) ✓
retry=3 → 渠道 #6 (优先级 0) ✓
符合预期的轮询模式:在优先级之间交替选择。
相关 Issue
(如果有相关 issue,请在此处引用)
检查清单
备注
Summary by CodeRabbit
New Features
Documentation
✏️ Tip: You can customize this high-level summary in your review settings.