Skip to content

feat: expose Smart sticky sessions - #5198

Open
eyaeya wants to merge 3 commits into
vernesong:devfrom
eyaeya:codex/smart-sticky-openclash
Open

eyaeya wants to merge 3 commits into
vernesong:devfrom
eyaeya:codex/smart-sticky-openclash

Conversation

@eyaeya

@eyaeya eyaeya commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Dependency chain

This PR is part of a three-PR change set for Smart strategy: sticky-sessions support:

  1. OIX core support: vernesong/mihomo-oix#6
  2. Matching non-OIX core support: vernesong/mihomo#10
  3. OpenClash LuCI/YAML wiring, this PR: vernesong/OpenClash#5198

This OpenClash PR depends on compatible core support. It only exposes and writes strategy: sticky-sessions; the actual affinity behavior must be implemented by the selected core.

Compatibility dependency by selected core:

  • OIX users need vernesong/mihomo-oix#6, or an equivalent OIX core release, before enabling this option.
  • Non-OIX users need vernesong/mihomo#10, or an equivalent non-OIX core release, before enabling this option.

Recommended merge/release order:

  1. Merge/release core support: vernesong/mihomo-oix#6 and vernesong/mihomo#10.
  2. Merge this OpenClash PR after supported cores can parse and enforce Smart strategy: sticky-sessions.

On older cores, generated YAML containing Smart strategy may fail config parsing. That is why this PR keeps the option opt-in, leaves defaults unchanged, and includes LuCI wording that calls out the core dependency.

Summary

Expose Smart strategy: sticky-sessions in OpenClash without changing existing defaults.

This PR only wires configuration and LuCI. The actual sticky behavior must be provided by a compatible Smart-capable core.

Root cause

Router-side investigation found that OpenClash had partial Smart strategy plumbing but no working end-to-end path:

  • yml_groups_get.sh could import a Smart YAML strategy into UCI as strategy_smart.
  • yml_groups_set.sh did not read or write strategy_smart, so exporting/regenerating YAML dropped it.
  • LuCI exposed strategy only for load-balance, not Smart.
  • Smart auto-switch converted url-test/load-balance groups to smart and added LightGBM/collect/prefer-ASN fields, but never wrote a Smart strategy.
  • Current cores before the paired core PRs do not parse Smart strategy, so OpenClash must not present this as an OpenClash-only fix.

Design

  • Add per-group strategy_smart LuCI option for Smart groups.
  • Only offer sticky-sessions for Smart groups; do not offer round-robin or consistent-hashing because those remain load-balance strategies.
  • Read strategy_smart in yml_groups_set.sh and write strategy: sticky-sessions only when explicitly set.
  • Add global smart_strategy, defaulting to 0, for Smart auto-switch/overwrite flows.
  • Pass smart_strategy into yml_rules_change.sh and only write group['strategy'] = 'sticky-sessions' when the user explicitly selects it.
  • Keep default behavior unchanged: no Smart group receives strategy unless explicitly configured.
  • Restrict global smart_strategy to groups newly converted by Smart auto-switch; existing Smart groups continue to use their per-group strategy_smart setting.
  • Add a small offline regression script checking the critical wiring paths.

Validation

  • bash -n luci-app-openclash/root/usr/share/openclash/*.sh luci-app-openclash/root/etc/init.d/openclash tests/openclash_smart_strategy_test.sh
  • tests/openclash_smart_strategy_test.sh
  • git diff --check HEAD^ HEAD

@Pluviophile714

Copy link
Copy Markdown

用ai用魔怔了?

@eyaeya

eyaeya commented Jun 30, 2026

Copy link
Copy Markdown
Contributor Author

用ai用魔怔了?

@Pluviophile714
如果是 PR 质量问题,请明确指出。
如果是情绪问题,那么,烦请大佬你出手解决 Smart 模式下 strategy: sticky-sessions 失效的问题,例如短时间内 chatgpt.com:443:170 条连接命中过 9 个节点。我会认真阅读大佬你的PR。

@eyaeya
eyaeya marked this pull request as ready for review June 30, 2026 16:06
@eyaeya

eyaeya commented Jul 2, 2026

Copy link
Copy Markdown
Contributor Author

2026-07-02 最新核验

结论:暂不关闭。当前 upstream 只已有 load-balance 侧的 strategy 选项;尚未覆盖 Smart sticky-sessions 的 OpenClash YAML/LuCI wiring。

核验基准:OpenClash dev = c460dcf(0.47.113,2026-07-01 05:54 +0800);OIX Alpha/Pre-Alpha = 2b1742db8a2e38dc49329133a11576804b468a6b。mihomo-oix 当前没有 GitHub Release 对象。

证据:

  • git cherry -v origin/dev pr/519837953a2 仍为 +
  • origin/dev:yml_groups_set.shorigin/dev:yml_rules_change.sh 没有 strategy_smartsmart_strategy 写入链路。
  • origin/dev:config-overwrite.lua 没有全局 smart_strategy 设置;origin/dev:root/etc/config/openclash 也没有默认配置项。
  • OIX origin/Alpha:adapter/outboundgroup/smart.go 没有 SmartOption.StrategystickyCachesticky-sessions 解析/执行;core 侧配套仍在 feat: add Smart sticky sessions mihomo-oix#6

收益差异:upstream 当前收益是保留已有 load-balance strategy 和 Smart 基础开关;本 PR 额外把 Smart 组的 strategy: sticky-sessions 作为显式 opt-in 从 LuCI/UCI 写到 YAML,并让 Smart auto-switch 流程能按用户选择写入该策略。默认不改变现有行为,等兼容 core 支持后可提供应用/域名层面的 Smart 会话亲和能力。

因此本 PR 仍有独立收益,保留开放,并继续依赖 OIX #6 / 非 OIX core 等价实现。

@eyaeya

eyaeya commented Jul 3, 2026

Copy link
Copy Markdown
Contributor Author

2026-07-04 Code Review 修复说明

本轮重新基于当前 origin/dev 审查了该 PR,并发现 Smart sticky-sessions 暴露后有两个联动问题,已追加修复 commit 0931d0e

  1. 全局 smart_strategy=sticky-sessions 在 LuCI 中可独立配置,但此前只对 auto smart switch 转换出来的组写入 strategy,已有 type: smart 的组不会生效。
  2. auto smart switch 将 load-balance 转为 smart 时会保留原 YAML 的 strategy: consistent-hashing 等旧策略;配合 mihomo Smart sticky-sessions 解析逻辑时,非空且非 sticky-sessions 的 Smart strategy 会导致 core 拒绝配置。

修复内容:

  • 转换 url-test/load-balancesmart 时先清理旧 strategy
  • 当全局 smart_strategy=sticky-sessions 且组类型为 smart 时统一写入 strategy: sticky-sessions,覆盖已有 Smart 组和转换后的 Smart 组。
  • 补充 tests/openclash_smart_strategy_test.sh 覆盖默认转换不保留 legacy strategy、全局 sticky 作用于已有 Smart 组、非 Smart 组不被写入 strategy。

验证:

  • bash -n luci-app-openclash/root/usr/share/openclash/yml_rules_change.sh luci-app-openclash/root/usr/share/openclash/yml_groups_set.sh tests/openclash_smart_strategy_test.sh
  • bash tests/openclash_smart_strategy_test.sh
  • git diff --check
  • 对抗式复审未发现新的需要修正问题。

@eyaeya

eyaeya commented Jul 8, 2026

Copy link
Copy Markdown
Contributor Author

2026-07-08 最新核验与范围更新

结论:不关闭。本 PR 的 Smart sticky-sessions 目标只被最新 dev 部分覆盖,剩余 LuCI/UCI/YAML 写入链路仍未进入上游。

核验结果:

  • 最新 dev 已到 0e05592d313d46dca69240aa4a7e5fb2d67625a2
  • upstream 已有部分前置能力:yml_groups_get.sh 能把 Smart YAML strategy 导入到 strategy_smartgroups-config.lua 的 load-balance strategy 列表也已有 sticky-sessions
  • upstream 仍缺 Smart 专用 strategy_smart UI、全局 smart_strategy 开关/default/init 传参、yml_groups_set.sh 导出 Smart strategy: sticky-sessions,以及 yml_rules_change.sh 在 auto smart switch/全局 sticky 场景下的写入逻辑。

执行计划与本次更新:

  1. 基于最新 origin/dev 重新合并 PR 分支,保留 upstream 已经覆盖的 yml_groups_get.sh 导入逻辑,不重复实现。
  2. 保留本 PR 仍缺失的 wiring:Smart group strategy_smart UI、导出 YAML、全局 smart_strategy UI/default/init 参数和 yml_rules_change.sh 写入链路。
  3. 保留安全边界:load-balance 原 strategy 仍只作用于 load-balance;auto smart switch 转换为 smart 时清理旧的 load-balance strategy;全局 sticky 只写入 type: smart 的组。
  4. 用聚焦测试覆盖静态 wiring、legacy strategy 清理、全局 sticky 作用范围和导入/导出 round-trip。

已推送:

  • 合入最新 dev0e05592d313d46dca69240aa4a7e5fb2d67625a2
  • 新 head:db716245f19d645adf79fdc33537bda379399d56
  • 当前 PR 状态读回:MERGEABLE / CLEAN,没有 merge conflict。

验证:

bash tests/openclash_smart_strategy_test.sh
bash -n luci-app-openclash/root/usr/share/openclash/*.sh
bash -n tests/*.sh
sh -n luci-app-openclash/root/etc/init.d/openclash
git diff --check
rg -n '^(<<<<<<<|=======|>>>>>>>)'

以上均已通过。另做了一轮只读复核,确认最终 diff 未重新引入 upstream 已覆盖的 yml_groups_get.sh 逻辑,且仍只聚焦 Smart sticky wiring。

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