0c9f256dfc
对应体检报告 #10-#24,一次性完成 v1.1.0 全部 13 项: - #10 storage/cache/redis/jwt/logger 五组件 Manager 化(XxxManager + DefaultXxx + SetDefaultXxxManager,包级 facade 保留兼容) - #11 删除 wire 包 + 清理 StartServerWithPort/GracefulShutdown 双轨 - #12 Lifecycle Hooks(OnInit/OnStart/OnReady/OnStop) - #13 Server 参数配置化(timeout/TLS/unix_socket/response_mode) - #14 JWTConfig.Expire 改 time.Duration + 删 AppConfig.TokenExpire - #15 response Mode 开关(ModeBusiness/ModeREST) - #16 Config.Validate 启动期校验 - #17 livez/readyz 探针路由 - #18 Prometheus metrics 中间件 + /metrics - #19 请求级 Timeout 中间件 - #21 主库探活 + replica 健康剔除 - #22 App.Go + in-flight goroutine 管理 - #24 RequestID 默认装入 + #23 核对 Breaking:删 wire、删 TokenExpire、JWTConfig.Expire 类型变更、删双轨函数。 详见 CHANGELOG 升级说明。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
33 lines
748 B
Go
33 lines
748 B
Go
package middleware
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// Timeout 请求级超时中间件(#19)。
|
|
//
|
|
// 与 http.Server.ReadTimeout(连接级)不同,这是业务级超时:
|
|
// 为每个请求的 context 设置 deadline,下游 GORM / Redis / HTTP 调用
|
|
// 走 c.Request.Context() 即可级联取消,避免单个慢请求拖垮协程。
|
|
//
|
|
// 用法:
|
|
//
|
|
// r.Use(middleware.Timeout(5 * time.Second))
|
|
//
|
|
// d <= 0 时不启用超时(直接 Next)。
|
|
func Timeout(d time.Duration) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
if d <= 0 {
|
|
c.Next()
|
|
return
|
|
}
|
|
ctx, cancel := context.WithTimeout(c.Request.Context(), d)
|
|
defer cancel()
|
|
c.Request = c.Request.WithContext(ctx)
|
|
c.Next()
|
|
}
|
|
}
|