Skip to content

fix(option): 配置项为空字符串时按空集合处理,消除 option 同步噪声日志#18

Merged
reputationly merged 1 commit into
mainfrom
fix/empty-json-option-guard
Jun 23, 2026
Merged

fix(option): 配置项为空字符串时按空集合处理,消除 option 同步噪声日志#18
reputationly merged 1 commit into
mainfrom
fix/empty-json-option-guard

Conversation

@reputationly

Copy link
Copy Markdown
Owner

背景

线上单实例排查时发现日志每 60s(SyncOptions 周期)刷一条:

[SYS] failed to update option map: unexpected end of JSON input

定位为:DB 中某个 JSON 类型的 option 被清空成 "",定时 SyncOptions → loadOptionsFromDatabase → updateOptionMap 对空串做 json.Unmarshal,触发 unexpected end of JSON input。本次现网实际是 GroupGroupRatio 为空(已在 DB 侧改为 {} 临时止血),但同类风险存在于所有「可被前端清空」的 JSON 配置项。

改动

统一把「空字符串/纯空白」视为「空集合」,直接返回 nil,不再 unmarshal。空配置语义本就等价于空 map / 空数组,行为不变,仅消除噪声日志,并避免该项在内存中保持上一轮旧值。

集中点 + 散点覆盖:

位置 覆盖的配置
types/rw_map.go(LoadFromJsonString / LoadFromJsonStringWithCallback) 全部 9 个 ratio:ModelRatio/GroupRatio/GroupGroupRatio/CompletionRatio/ModelPrice/CacheRatio/CreateCacheRatio/ImageRatio/AudioRatio/AudioCompletionRatio
setting/chat.go Chats
setting/auto_group.go AutoGroups
common/topup-ratio.go TopupGroupRatio
setting/rate_limit.go ModelRequestRateLimitGroup
setting/user_usable_group.go UserUsableGroups
setting/operation_setting/payment_setting_old.go PayMethods

测试

新增 types/rw_map_empty_test.go:空串 / 纯空白 / 合法 JSON 三类输入。

go build ./...        # OK
go test ./types/ -run LoadFromJsonString   # PASS

备注

  • 不改动既有 encoding/json vs common.* 的调用方式(保持 surgical,未顺带重构)。
  • 与数据库无关,三库(SQLite/MySQL/PostgreSQL)行为一致。

当 DB 中某个 JSON 类型的 option(如 GroupGroupRatio、ModelRatio、PayMethods 等)
被清空为 "",定时 SyncOptions -> updateOptionMap 会对空串做 json.Unmarshal,
每个同步周期(默认 60s)刷一条:
  [SYS] failed to update option map: unexpected end of JSON input

统一在各 Update*ByJSONString 入口及 types.LoadFromJsonString/WithCallback 中
将「空字符串/纯空白」视为「空集合」直接返回 nil,不再 unmarshal。空配置语义
本就等价于空 map/空数组,行为不变,只是消除噪声日志。

覆盖:
- types/rw_map.go: 覆盖全部 9 个 ratio 配置 + GroupGroupRatio
- Chats / AutoGroups / TopupGroupRatio / ModelRequestRateLimitGroup
  / UserUsableGroups / PayMethods

新增 types/rw_map_empty_test.go 验证空串/空白/合法 JSON 三种输入。
@reputationly reputationly merged commit 467437a into main Jun 23, 2026
1 check passed
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.

1 participant