fix(library): 整库只读接口改清单级读取,修掉真实超时
build / build (push) Successful in 1m17s

候选归档 79 版 / 56MB blob 上,overview、storage、snapshots 三个只读接口
都在 10s 请求期限后返回 503。分段计时定位到唯一瓶颈是按版本重读全部 blob:
Open 0ms、LoadManifest×79 11~22ms、ContentUsage 15~25ms、
Usage(逐版 Load 验字节)13.75~14.11s;单版 Load 261~290ms。

- Usage 拆成 Usage(逐版复核字节,同步/恢复与运维 CLI 用)与
  UsageFromManifests(同一份去重实现,只做清单级校验),列举路径改用后者;
- library_storage.go 每版两次 Load、snapshots 每版一次 Load 改为一次清单读取;
- wiki/文档差异只读所需部件,被读部件仍由 OpenPart 复核摘要;
- LoadManifest 与 Load 共用同一套清单判据(严格解码 + 重新编码校验);
- 列举响应带 verification=manifest / verified=false,详情整版复核后才是 content,
  避免把"清单可读"读成"完整性已验证";
- libraryWithDeadline 去掉超时后再写已结束响应的 goroutine 写法
  (候选环境实测 http: superfluous response.WriteHeader call)。

回归:多版本共享大 blob 时枚举不读 blob 字节(篡改后枚举仍成功),
而 Load/OpenPart/Usage 仍拒绝坏 blob;坏清单两条路径都整条报错。
make verify 15 项全绿(local 档案)。
This commit is contained in:
姚章浩
2026-10-28 21:52:14 +08:00
parent 1609a0c9be
commit fe6e218d9c
12 changed files with 602 additions and 96 deletions
+11 -16
View File
@@ -103,23 +103,18 @@ type Store interface {
RequeueArtifactFetchWithOperation(ctx context.Context, request governance.RequeueArtifactFetchRequest, operationID string) (governance.RequeueArtifactFetchResult, bool, error)
}
// libraryWithDeadline 给整库只读接口加请求级超时:超时就返回 503 + 明确原因,
// 而不是让浏览器连接挂住(归档大时读盘可能很慢)。
func libraryWithDeadline(w http.ResponseWriter, r *http.Request, limit time.Duration, label string,
// libraryWithDeadline 给整库只读接口一个**真实**的请求期限:仓库读取按 ctx 在版本之间
// 中断,超时由 handler 自己返回 503 并说明原因。
//
// 为什么不再"另起 goroutine + 到点先回 503":那样主 goroutine 已经把响应结束,
// 被放弃的 handler 十几秒后仍会写同一个 ResponseWriter——候选环境实测日志
// `http: superfluous response.WriteHeader call`,并且留下仍在扫全部 blob 的 goroutine。
// 响应只允许一个写入者。
func libraryWithDeadline(w http.ResponseWriter, r *http.Request, limit time.Duration,
handler func(http.ResponseWriter, *http.Request)) {
ctx, cancel := context.WithTimeout(r.Context(), limit)
defer cancel()
done := make(chan struct{})
go func() {
defer close(done)
handler(w, r.WithContext(ctx))
}()
select {
case <-done:
case <-ctx.Done():
writeJSON(w, http.StatusServiceUnavailable, map[string]any{
"code": "library_read_timeout", "error": label + " 读取超时(归档较大),请稍后重试"})
}
handler(w, r.WithContext(ctx))
}
type Config struct {
@@ -437,13 +432,13 @@ func NewHandler(config Config) (http.Handler, error) {
// 整库状态:测试封存 / 生产生效 / 生产预备份 三类版本 + 差异 + 持久作业。
// 归档可能很大:加请求级超时,读不动就明确降级,别让页面请求挂死。
mux.HandleFunc("GET /api/v1/library/overview", func(w http.ResponseWriter, r *http.Request) {
libraryWithDeadline(w, r, 10*time.Second, "overview", func(w http.ResponseWriter, r *http.Request) {
libraryWithDeadline(w, r, 10*time.Second, func(w http.ResponseWriter, r *http.Request) {
libraryOverviewHandler(w, r, config)
})
})
// 存储管理:容量、保留策略、依赖保护与清理预览(只读,没有删除动作)。
mux.HandleFunc("GET /api/v1/library/storage", func(w http.ResponseWriter, r *http.Request) {
libraryWithDeadline(w, r, 10*time.Second, "storage", func(w http.ResponseWriter, r *http.Request) {
libraryWithDeadline(w, r, 10*time.Second, func(w http.ResponseWriter, r *http.Request) {
libraryStorageHandler(w, r, config)
})
})