降低 admin/dashboard 读路径压力,避免 snapshot-v2 并发击穿#933
Merged
Wei-Shaw merged 2 commits intoWei-Shaw:mainfrom Mar 12, 2026
Merged
Conversation
liafonx
pushed a commit
to liafonx/sub2api
that referenced
this pull request
Mar 20, 2026
…re-20260311 降低 admin/dashboard 读路径压力,避免 snapshot-v2 并发击穿
qyhfrank
pushed a commit
to qyhfrank/sub2api
that referenced
this pull request
Mar 24, 2026
…re-20260311 降低 admin/dashboard 读路径压力,避免 snapshot-v2 并发击穿
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.
背景
线上排查时,后台
admin/dashboard这条链路里还有几块重查询会直接打到usage_logs:trend、models、groups、api-keys-trend、users-trend,而snapshot-v2在缓存 miss 时会串行组装这些子块。高并发下如果多个相同请求同时 miss,就会把读压集中打到数据库,放大后台卡顿。这次 PR 不改统计口径、不改表结构、不新增聚合表,只在 handler 层做短 TTL 缓存和并发合并,先把
admin/dashboard的读放大问题收住。改动内容
GET /api/v1/admin/dashboard/trendGET /api/v1/admin/dashboard/modelsGET /api/v1/admin/dashboard/groupsGET /api/v1/admin/dashboard/api-keys-trendGET /api/v1/admin/dashboard/users-trendsnapshot-v2缓存增加singleflight并发合并,避免相同 key 在缓存 miss 时并发击穿。snapshot-v2的组装过程改为复用上述 section 级缓存 helper,而不是每次直接下钻到 service/repository。为什么这样做
trend场景,models、groups、users-trend还没有现成的预聚合读模型。snapshot-v2上做并发合并,可以在不改变业务语义的前提下,先降低数据库读压。风险控制
本地验证
单元/静态检查
docker run --rm -v /home/ius/sub2api/backend:/src -w /src golang:1.26.1 /usr/local/go/bin/go test ./internal/handler/admin -count=1docker run --rm -v /home/ius/sub2api/backend:/src -w /src golangci/golangci-lint:v2.9.0 golangci-lint run --timeout=30mDocker 烟测
docker compose -f /tmp/sub2api-prtest-compose.yml up -d --buildPOST /api/v1/auth/login返回200GET /api/v1/admin/dashboard/trend返回200GET /api/v1/admin/dashboard/users-trend返回200GET /api/v1/admin/dashboard/snapshot-v2返回200X-Snapshot-Cache: miss,再次请求X-Snapshot-Cache: hitCI
fix/dashboard-read-pressure-20260311的CI已通过fix/dashboard-read-pressure-20260311的Security Scan已通过