Repository navigation
fix(redirect): 301 有界缓存 + HEAD 支持 + 访问明细异步批量落库 - #2
Merged
Merged
Conversation
1. 跳转缓存语义
- 规则改写、多目标轮询、落地页与其余 302 一律 302 + Cache-Control: no-store
- 单目标 301 带有界缓存 private, max-age=JANUS_REDIRECT_301_MAX_AGE(默认 5m,0 = no-store);
开启规则的短链 301 改为 no-store(仍保留 301 状态码)
- 注册 HEAD /{code},与 GET 同响应;HEAD 不记明细、不推进轮询指针
2. 访问明细移出请求路径
- 新增 httpapi.VisitQueue:有界队列 + worker 池 + 多行批量 INSERT,
落库 ctx 与请求解耦(WithoutCancel + 超时),队列满丢弃并计数,批量失败逐行重试
- 后置访问钩子改为落库之后由队列触发;被丢弃的明细不触发钩子
- 未配置队列(黑盒测试)或队列已关闭时退回同步写入,同样不再随客户端断开取消
- cmd/janus 与 pkg/janus 在 http.Server.Shutdown 之后排空队列
- 新增 store.PickTargetFrom / InsertVisits,跳转不再二次读取 link_targets
- 连接池可配置 JANUS_DB_MAX_OPEN_CONNS(默认 25,空闲上限同值)
ruanbw
force-pushed
the
fix/high-redirect
branch
from
October 8, 2026 07:23
203aa19 to
cee9730
Compare
This was referenced Oct 8, 2026
Owner
Author
|
已 rebase 到 冲突:
合并顺序:#1 → #6 → #3 → #2 → #4 → #5(请按此顺序依次合并;每个 PR 合并后,下一个 PR 的 base 会由 GitHub 自动改指向其上一级的目标分支)。 验证:每一步 rebase 后均 |
This was referenced Oct 8, 2026
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.
概述
修复代码审查中访客跳转热路径的两个 High 问题。基于
fix/critical-review。1. 301 被浏览器无限期缓存
问题:
redirectToTarget从不设置Cache-Control,浏览器对 301 默认无限期缓存;规则改写的跳转也沿用短链自身的 301。结果是租户改目标、停用短链、加规则后,老访客永远收不到,访问明细也漏记。另外HEAD /{code}落进 NoRoute 一律 404,链接预览器/监控把健康短链当坏链。修复:
302+Cache-Control: no-storeCache-Control: private, max-age=<JANUS_REDIRECT_301_MAX_AGE>(默认5m,配0则no-store)no-store(仍保留 301 状态码),否则缓存会让访客绕过规则求值/{code}/click)同样 302 + no-storeHEAD /{code},与 GET 同状态码/Location/缓存头;HEAD 不记明细、不推进轮询指针(探测流量不应虚增访问量)2. 访问明细写入阻塞跳转
问题:每次跳转在请求内同步 INSERT 明细;
PickTarget把link_targets再查一遍;连接池写死 10;INSERT 用请求 ctx,访客断开即取消、明细静默丢失。修复:
httpapi.VisitQueue(internal/httpapi/visit_queue.go):有界队列 + 小型 worker 池 + 多行批量 INSERT(store.InsertVisits)context.WithoutCancel+ 每批 5s 超时)Deps.VisitQueue为 nil(黑盒测试)或队列已关闭时退回同步写入,保证测试"请求返回即可查到明细";同步路径同样改用WithoutCancel+ 超时cmd/janus/main.go与pkg/janus的App.Shutdown在http.Server.Shutdown之后Close队列(最多等 5s),并在退出时打印丢弃总数;App.initHandler被二次调用时先排空旧队列store.PickTargetFrom,跳转复用LookupLinkForVisit已读到的目标池,不再二次查询link_targets(PickTarget签名不变)JANUS_DB_MAX_OPEN_CONNS(默认 25),空闲连接上限取同值并设 5 分钟空闲回收(原 database/sql 默认只留 2 条空闲连接,高峰期反复建连)新增配置(已写入
.env.example,Validate拒绝负数,0 = 内置默认)JANUS_DB_MAX_OPEN_CONNSJANUS_REDIRECT_301_MAX_AGEJANUS_VISIT_QUEUE_SIZEJANUS_VISIT_QUEUE_WORKERSJANUS_VISIT_QUEUE_BATCH行为变更(需评审确认)
rules_redirect_test.go:原断言"301 短链的规则改写也走 301",按本次修复改为断言302 + no-storelinks_test.go:TestCustomCodeAnd301增加 Cache-Control 断言;新增TestHeadRedirectMatchesGet测试
gofmt/go build ./.../go vet ./...(含测试编译)通过visit_queue_test.go:攒批且不超 BatchSize、满队列立即丢弃并计数、Close 排空并触发回调、关闭后入队返回 Closed、批量失败逐行重试、请求取消不影响落库;-count=20稳定通过(环境无 cgo,未跑-race)internal/config、internal/rules、internal/jwt、internal/rbac、internal/mailer、pkg/plugin、pkg/janus(Builder)通过internal/httpapi黑盒测试本环境不可用,仅保证编译通过,需在有库环境跑一遍./scripts/quality.sh tests