package http import ( "context" "errors" "net/http" failure "git.zhangdeman.cn/zhangdeman/ginframework/internal/failure" "github.com/gin-gonic/gin" ) // finishCanceled 在合作式预算结束后统一返回超时响应,保留已分类错误的日志语义。 // 入参:ctx 为当前请求,cause 为已接收的下层错误,允许 nil。 // 返回:上下文结束并应停止当前流程时为 true;客户端取消时不继续写响应。 func (s *Server) finishCanceled(ctx *gin.Context, cause error) bool { err := ctx.Request.Context().Err() if err == nil { return false } if cause == nil { cause = err } code, summary := codeRequestCancelled, "HTTP request cancelled" if errors.Is(err, context.DeadlineExceeded) { code, summary = codeRequestTimeout, "HTTP request budget exceeded" } classified := failure.WrapContext(ctx.Request.Context(), code, summary, cause) if errors.Is(err, context.DeadlineExceeded) { s.writeResult(ctx, nil, classified, http.StatusGatewayTimeout) } ctx.Abort() return true }