Verify / verify (push) Canceled after 0s
增加安全字段校验提示、请求预算和就绪/存活探针, 让绑定、数据库、Redis 和出站响应解码保留可信诊断上下文。 补充完整应用装配及逆序清理示例,同步接入和迁移文档。 验证:make verify、make race-check 全部通过。 BREAKING CHANGE: 普通 Controller 默认启用 30 秒协作式请求预算, 可用 request_timeout_ms: 0 或 max-exec-time:"0" 关闭; 字段校验失败的 400 响应增加安全字段明细。
34 lines
1.0 KiB
Go
34 lines
1.0 KiB
Go
package http
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
|
|
failure "git.zhangdeman.cn/github/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
|
|
}
|