perf(服务端): 启用 h2c 并保留 HTTP/1.1 回退

This commit is contained in:
yangjianbo
2026-01-24 20:01:03 +08:00
parent fbf72f0ec4
commit bece1b5201
4 changed files with 38 additions and 2 deletions

View File

@@ -14,6 +14,8 @@ import (
"github.com/gin-gonic/gin"
"github.com/google/wire"
"github.com/redis/go-redis/v9"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
)
// ProviderSet 提供服务器层的依赖
@@ -56,9 +58,10 @@ func ProvideRouter(
// ProvideHTTPServer 提供 HTTP 服务器
func ProvideHTTPServer(cfg *config.Config, router *gin.Engine) *http.Server {
handler := h2c.NewHandler(router, &http2.Server{})
return &http.Server{
Addr: cfg.Server.Address(),
Handler: router,
Handler: handler,
// ReadHeaderTimeout: 读取请求头的超时时间,防止慢速请求头攻击
ReadHeaderTimeout: time.Duration(cfg.Server.ReadHeaderTimeout) * time.Second,
// IdleTimeout: 空闲连接超时时间,释放不活跃的连接资源