增加安全字段校验提示、请求预算和就绪/存活探针, 让绑定、数据库、Redis 和出站响应解码保留可信诊断上下文。 补充完整应用装配及逆序清理示例,同步接入和迁移文档。 验证:make verify、make race-check 全部通过。 BREAKING CHANGE: 普通 Controller 默认启用 30 秒协作式请求预算, 可用 request_timeout_ms: 0 或 max-exec-time:"0" 关闭; 字段校验失败的 400 响应增加安全字段明细。
This commit is contained in:
1 parent
b4c3ac622c
commit
3196e45a01
22 files changed
+1294
-79
No files matched your search
@@ -7,6 +7,7 @@ import (
|
||||
"net/http"
|
||||
"sort"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"git.zhangdeman.cn/github/ginframework/diagnostic"
|
||||
@@ -28,6 +29,7 @@ type Config struct {
|
||||
SuccessCode int `json:"success_code" yaml:"success_code" dc:"成功业务码,零为默认"`
|
||||
ReadHeaderTimeoutMS int `json:"read_header_timeout_ms" yaml:"read_header_timeout_ms" dc:"请求头读取超时毫秒,缺省 5000"`
|
||||
TrustedProxies []string `json:"trusted_proxies" yaml:"trusted_proxies" dc:"可信代理名单,缺省不信任转发头"`
|
||||
RequestTimeoutMS *int64 `json:"request_timeout_ms" yaml:"request_timeout_ms" dc:"普通 Controller 请求合作式预算毫秒,缺省 30000,零禁用;原生 Handle 不自动套用"`
|
||||
}
|
||||
|
||||
// Options 是调用方显式注入的 HTTP 运行依赖。
|
||||
@@ -53,12 +55,21 @@ type Server struct {
|
||||
cancel context.CancelFunc `dc:"取消长请求及 SSE 的函数"`
|
||||
routes []route `dc:"启动期保存的不可变路由计划"`
|
||||
trustedFieldNames []string `dc:"启动时排序的可信字段名,避免每个请求重复构建和排序"`
|
||||
ready atomic.Bool `dc:"应用装配完成的就绪信号,不代替依赖探测"`
|
||||
}
|
||||
|
||||
// NewServer 创建独立 HTTP 实例,不修改 Gin 全局运行模式。
|
||||
// 入参:cfg 为数据配置,options 为运行期函数及日志依赖。
|
||||
// 返回:服务实例;非法代理和超时配置返回内部错误。
|
||||
func NewServer(cfg Config, options Options) (*Server, error) {
|
||||
budget := int64(30000)
|
||||
if cfg.RequestTimeoutMS != nil {
|
||||
budget = *cfg.RequestTimeoutMS
|
||||
}
|
||||
if budget < 0 || budget > int64((1<<63-1)/time.Millisecond) {
|
||||
return nil, failure.Wrap(codeRequestBudget, "validate HTTP request budget failed", errors.New("invalid request budget"))
|
||||
}
|
||||
cfg.RequestTimeoutMS = &budget
|
||||
if cfg.ReadHeaderTimeoutMS < 0 {
|
||||
return nil, failure.Wrap(codeServerConfig, "validate HTTP timeout failed", errors.New("negative header timeout"))
|
||||
}
|
||||
@@ -154,6 +165,7 @@ func (s *Server) Serve(listener net.Listener) error {
|
||||
func (s *Server) Shutdown(ctx context.Context) error {
|
||||
s.lock.Lock()
|
||||
s.stopping = true
|
||||
s.ready.Store(false)
|
||||
s.frozen = true
|
||||
server := s.server
|
||||
s.cancel()
|
||||
|
||||