fix(httpapi): 整库只读接口加请求级超时,避免归档大时请求挂死
build / build (push) Successful in 1m14s

为什么:候选前端联调时 /library/overview 与 /library/storage 会挂到连接被重置,
浏览器无法渲染。加 10s 超时并返回明确降级原因,同时差异计算放进看门狗。
This commit is contained in:
姚章浩
2026-10-28 20:36:59 +08:00
parent def6022048
commit b76a433574
2 changed files with 49 additions and 3 deletions
+23 -1
View File
@@ -472,7 +472,29 @@ func libraryOverviewHandler(w http.ResponseWriter, r *http.Request, config Confi
if overview.Production.State == "known" && overview.Production.Version != nil {
baseSnapshot = overview.Production.Version.SnapshotID
}
if view, ok := computeLibraryDiff(config.LibraryArchiveRoot, baseSnapshot, target); ok || view.Reason != "" {
// 归档可能很大:差异计算放进看门狗,超时就保持 unknown,别让页面请求挂死。
diffResult := make(chan struct {
view libraryDiffView
ok bool
}, 1)
go func() {
view, ok := computeLibraryDiff(config.LibraryArchiveRoot, baseSnapshot, target)
diffResult <- struct {
view libraryDiffView
ok bool
}{view, ok}
}()
var view libraryDiffView
var diffOK bool
select {
case outcome := <-diffResult:
view, diffOK = outcome.view, outcome.ok
case <-time.After(8 * time.Second):
view = libraryDiffView{State: "unknown", Reason: "差异计算超时(归档较大),本次保持不变更结论",
Added: []any{}, Updated: []any{}, Removed: []any{}, BaseSnapshot: baseSnapshot, TargetSnapshot: target}
diffOK = false
}
if diffOK || view.Reason != "" {
overview.Diff = view
}
+26 -2
View File
@@ -103,6 +103,25 @@ 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,
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 + " 读取超时(归档较大),请稍后重试"})
}
}
type Config struct {
QAAI *qaopsai.Store
QAAITrustTLSProxy bool
@@ -416,12 +435,17 @@ func NewHandler(config Config) (http.Handler, error) {
libraryCaptureJobs(w, r, config)
})
// 整库状态:测试封存 / 生产生效 / 生产预备份 三类版本 + 差异 + 持久作业。
// 归档可能很大:加请求级超时,读不动就明确降级,别让页面请求挂死。
mux.HandleFunc("GET /api/v1/library/overview", func(w http.ResponseWriter, r *http.Request) {
libraryOverviewHandler(w, r, config)
libraryWithDeadline(w, r, 10*time.Second, "overview", 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) {
libraryStorageHandler(w, r, config)
libraryWithDeadline(w, r, 10*time.Second, "storage", func(w http.ResponseWriter, r *http.Request) {
libraryStorageHandler(w, r, config)
})
})
mux.HandleFunc("GET /api/v1/library/jobs", func(w http.ResponseWriter, r *http.Request) {
libraryJobsHandler(w, r, config)