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)