Repository navigation
feat: 出站传输压缩与效果统计 - #102
Closed
tktogether wants to merge 9 commits into
Closed
tktogether wants to merge 9 commits into
tktogether wants to merge 9 commits into
Conversation
added 9 commits
October 31, 2026 11:24
传输压缩策略默认全部关闭;Provider 保存探测结论、时间、证据与配置指纹。 新增请求体 gzip 编码工具和策略门控测试,端点变化时清空旧探测结论。
按 provider 能力、配置指纹与 4 KiB 阈值压缩请求体;遇到 400/415/422 先明文重试,成功后降级并关闭该提供商的请求压缩。BuildHeaders 清理 入站 Content-Encoding/Content-Length,避免旧值进入压缩后的请求。
严格解析 Accept-Encoding 的 q 值,仅在总开关、流式开关、客户端声明 gzip 且上游未编码时压缩;删除 Content-Length、补充 Vary,并在每次 Write 后同步刷新,保持事件到达时序。
新增关联列表、同步探测、provider 级开关三个端点。探测复用最小测试 请求,按 2xx、明文对照与无法判定分类并持久化;总开关关闭时仍可探测。 启动时种子传输压缩策略,默认全部关闭。
新增 provider 检测入口与对话框、三语 tooltip 文案;设置页新增传输压缩 卡片(总开关、流式响应、上游请求,默认关闭)。探测结果支持可选关联与 手动切换,prompt 级开关仅在结果为支持时可用;补齐组件测试与 API mock。
Radix 同时响应悬停与聚焦;弹窗自动聚焦“?”时帮助文案会自行弹出。 共享 TooltipTrigger 用 :focus-visible 区分聚焦来源:程序或鼠标聚焦不展开, Tab 键盘聚焦仍展开。顺带清理 provider 模型弹窗的重复 Provider,并补上 复制按钮的无障碍名称与提示内容。
新增进程内原子计数:上游请求按实际压缩发送且未降级的尝试统计,SSE 按正常完成的压缩流统计;提供只读端点 /api/metrics/compression。 统计自服务启动起累计,重启归零,不写入数据库。
在传输压缩卡片中展示上游请求与流式响应的压缩次数、原始/压缩后/节省 字节和压缩比;每 10 秒自动刷新,页面隐藏时暂停。无压缩流量时显示 “尚无压缩流量”,并附统计口径与重启归零说明。
BalanceChat 返回后 retryLog 协程仍在写库,t.TempDir 清理会撞上 仍在使用的文件,CI 表现为 directory not empty。三个备用降级测试 补上 waitForFailRows,等待失败行结算后再结束。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
流量占当前账单成本的 90.8%(2026-10-27 备份,36 天)。两条出站链路均计费:上游请求 25.01 GB(82.6%)、客户端响应 5.27 GB(其中 99% 是 SSE)。实测 gzip:请求体 2.57x、SSE 逐事件 flush 14.3x。
本 PR 为出站链路增加可灰度、可回滚的传输压缩,并在设置页展示实际压缩效果。
主要变更
1. 三层开关,默认全部关闭
traffic_compression_policy.enabledsse_response、upstream_requestrequest_gzip_enabled,仅在探测结果为“支持”时允许开启2. 上游请求体 gzip + 能力探测
BuildHeaders清理入站Content-Encoding/Content-LengthGET /api/providers/gzip-probe/:id/associations、POST /api/providers/:id/gzip-probe、PATCH /api/providers/:id/request-gzip3. SSE 响应 gzip
Accept-Encoding的 q 值(gzip;q=0不压)Content-Length、补Vary: Accept-Encodingbefore.Stream;客户端未声明 gzip 时保持明文4. 压缩效果统计
GET /api/metrics/compression5. UI 与文案
验证
go build ./...、go test ./...通过webui:tsc -b、eslint .、vitest run(1110 项)通过;vite build通过Accept-Encoding与gzip;q=0均为明文encoding=gzip,小请求保持identity灰度与回滚
兼容性说明
Checklist