fix(channel): unify weighted selection across cache modes - #6554
fix(channel): unify weighted selection across cache modes#6554faithleysath wants to merge 1 commit into
Conversation
Use raw channel weights in both database and memory-cache routing, fall back to equal intervals only when every candidate weight is zero, and share one half-open weighted selector to avoid boundary drift. Refs QuantumNous#6510
WalkthroughChannel selection now shares a weighted-index helper between database and memory-cache paths. Raw channel weights are used, zero-weight channels are excluded when positive weights exist, all-zero candidates are selected equally, and both behaviors are covered by tests. ChangesChannel weight selection
Estimated code review effort: 3 (Moderate) | ~20 minutes Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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.
🧹 Nitpick comments (2)
model/channel_cache.go (2)
183-187: 🧹 Nitpick | 🔵 TrivialBehavioral change worth calling out in release notes.
Zero-weight channels sharing a priority tier with any positive-weight channel will now receive zero traffic (previously the smoothed algorithm gave them some). This is the intended fix, but operators relying on the old "slow trickle" behavior for zero-weight channels (e.g., soft-disabling without a hard cutoff) should be informed via changelog/migration notes.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@model/channel_cache.go` around lines 183 - 187, Update the release notes or migration documentation for the channel selection behavior around selectWeightedIndex: document that zero-weight channels in a priority tier containing positive-weight channels now receive no traffic, and warn operators that the previous slow-trickle soft-disable behavior no longer applies.
162-172: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winUse
Channel.GetWeight()instead of duplicating the nil-default here.
Channel.GetWeight()already returns0whenWeightisnil, so assigning the localuint(0)and manually dereferencingWeightduplicates logic that should go only through the accessor.♻️ Suggested consolidation
- weight := uint(0) - if channel.Weight != nil { - weight = *channel.Weight - } - targetChannels = append(targetChannels, channel) - targetWeights = append(targetWeights, weight) + targetChannels = append(targetChannels, channel) + targetWeights = append(targetWeights, uint(channel.GetWeight()))🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@model/channel_cache.go` around lines 162 - 172, In the channel filtering loop, replace the local nil-safe weight initialization and manual Weight dereference with the existing Channel.GetWeight() accessor when appending to targetWeights. Keep the targetChannels and priority filtering behavior unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@model/channel_cache.go`:
- Around line 183-187: Update the release notes or migration documentation for
the channel selection behavior around selectWeightedIndex: document that
zero-weight channels in a priority tier containing positive-weight channels now
receive no traffic, and warn operators that the previous slow-trickle
soft-disable behavior no longer applies.
- Around line 162-172: In the channel filtering loop, replace the local nil-safe
weight initialization and manual Weight dereference with the existing
Channel.GetWeight() accessor when appending to targetWeights. Keep the
targetChannels and priority filtering behavior unchanged.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: d797e05a-2070-409e-9c5b-ef75d34d471f
📒 Files selected for processing (4)
model/ability.gomodel/channel_cache.gomodel/channel_weight.gomodel/channel_weight_test.go
|
已核对 CodeRabbit 的两个 nit:
因此本轮无需修改代码。 |
Important
Note
AI 辅助说明:本 PR 的代码与说明由 AI 编程助手协助生成,并由提交者授权提交,请按常规标准审阅。
📝 变更描述 / Description
数据库选路当前使用
weight + 10和闭区间边界,而内存缓存选路使用原始weight和半开区间,导致相同渠道配置因缓存开关不同而产生不同流量比例。本 PR 将两条路径收敛到同一个加权选择 helper:
[0, totalWeight)区间。该变更只调整渠道选择算法,不修改现有渠道配置、优先级、分组或缓存设置。
Warning
升级提示:如果某个同优先级渠道池同时包含正权重与零权重渠道,修复后零权重渠道将不再接收新流量。过去依赖零权重渠道仍获得少量流量的部署,应在升级前显式调整对应权重。
🚀 变更类型 / Type of change
🔗 关联任务 / Related Issue
✅ 提交前检查项 / Checklist
Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。📸 运行证明 / Proof of Work
以下检查均在最新
main@66ee6b8f的 PR 分支上通过:确定性测试直接验证选择区间:
另有集成测试分别关闭和开启
MemoryCacheEnabled,确认1:0候选集在数据库与内存缓存路径下都只选择权重为 1 的渠道。