From b76a433574b17c27607ca3146724652f73330264 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9A=E7=AB=A0=E6=B5=A9?= Date: Wed, 28 Oct 2026 20:36:59 +0800 Subject: [PATCH] =?UTF-8?q?fix(httpapi):=20=E6=95=B4=E5=BA=93=E5=8F=AA?= =?UTF-8?q?=E8=AF=BB=E6=8E=A5=E5=8F=A3=E5=8A=A0=E8=AF=B7=E6=B1=82=E7=BA=A7?= =?UTF-8?q?=E8=B6=85=E6=97=B6=EF=BC=8C=E9=81=BF=E5=85=8D=E5=BD=92=E6=A1=A3?= =?UTF-8?q?=E5=A4=A7=E6=97=B6=E8=AF=B7=E6=B1=82=E6=8C=82=E6=AD=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 为什么:候选前端联调时 /library/overview 与 /library/storage 会挂到连接被重置, 浏览器无法渲染。加 10s 超时并返回明确降级原因,同时差异计算放进看门狗。 --- internal/httpapi/library_flow.go | 24 +++++++++++++++++++++++- internal/httpapi/server.go | 28 ++++++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/internal/httpapi/library_flow.go b/internal/httpapi/library_flow.go index eb32b37..71e1a09 100644 --- a/internal/httpapi/library_flow.go +++ b/internal/httpapi/library_flow.go @@ -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 } diff --git a/internal/httpapi/server.go b/internal/httpapi/server.go index a2d8479..d0d1d8e 100644 --- a/internal/httpapi/server.go +++ b/internal/httpapi/server.go @@ -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)