mirror of
https://github.com/woodchen-ink/proxy-go.git
synced 2025-07-18 08:31:55 +08:00
feat(proxy): Optimize HTTP transport and timeout configurations
- Increase connection pool and idle connection limits - Extend timeout durations for more robust network handling - Configure HTTP/2 transport with enhanced performance settings - Add HTTP/2-specific optimizations and security constraints
This commit is contained in:
parent
ca79cc7dac
commit
0446eb1c53
5
go.mod
5
go.mod
@ -6,3 +6,8 @@ require (
|
|||||||
github.com/andybalholm/brotli v1.1.1
|
github.com/andybalholm/brotli v1.1.1
|
||||||
golang.org/x/time v0.9.0
|
golang.org/x/time v0.9.0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
golang.org/x/net v0.35.0 // indirect
|
||||||
|
golang.org/x/text v0.22.0 // indirect
|
||||||
|
)
|
||||||
|
4
go.sum
4
go.sum
@ -2,5 +2,9 @@ github.com/andybalholm/brotli v1.1.1 h1:PR2pgnyFznKEugtsUo0xLdDop5SKXd5Qf5ysW+7X
|
|||||||
github.com/andybalholm/brotli v1.1.1/go.mod h1:05ib4cKhjx3OQYUY22hTVd34Bc8upXjOLL2rKwwZBoA=
|
github.com/andybalholm/brotli v1.1.1/go.mod h1:05ib4cKhjx3OQYUY22hTVd34Bc8upXjOLL2rKwwZBoA=
|
||||||
github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZqKjWU=
|
github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZqKjWU=
|
||||||
github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E=
|
github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E=
|
||||||
|
golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8=
|
||||||
|
golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk=
|
||||||
|
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
|
||||||
|
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
|
||||||
golang.org/x/time v0.9.0 h1:EsRrnYcQiGH+5FfbgvV4AP7qEZstoyrHB0DzarOQ4ZY=
|
golang.org/x/time v0.9.0 h1:EsRrnYcQiGH+5FfbgvV4AP7qEZstoyrHB0DzarOQ4ZY=
|
||||||
golang.org/x/time v0.9.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
|
golang.org/x/time v0.9.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
|
||||||
|
@ -16,6 +16,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"golang.org/x/net/http2"
|
||||||
"golang.org/x/time/rate"
|
"golang.org/x/time/rate"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -25,11 +26,11 @@ const (
|
|||||||
largeBufferSize = 64 * 1024 // 64KB
|
largeBufferSize = 64 * 1024 // 64KB
|
||||||
|
|
||||||
// 超时时间常量
|
// 超时时间常量
|
||||||
clientConnTimeout = 3 * time.Second // 客户端连接超时
|
clientConnTimeout = 5 * time.Second // 客户端连接超时
|
||||||
proxyRespTimeout = 10 * time.Second // 代理响应超时
|
proxyRespTimeout = 30 * time.Second // 代理响应超时
|
||||||
backendServTimeout = 8 * time.Second // 后端服务超时
|
backendServTimeout = 20 * time.Second // 后端服务超时
|
||||||
idleConnTimeout = 120 * time.Second // 空闲连接超时
|
idleConnTimeout = 120 * time.Second // 空闲连接超时
|
||||||
tlsHandshakeTimeout = 5 * time.Second // TLS握手超时
|
tlsHandshakeTimeout = 10 * time.Second // TLS握手超时
|
||||||
|
|
||||||
// 限流相关常量
|
// 限流相关常量
|
||||||
globalRateLimit = 1000 // 全局每秒请求数限制
|
globalRateLimit = 1000 // 全局每秒请求数限制
|
||||||
@ -237,18 +238,30 @@ func NewProxyHandler(cfg *config.Config) *ProxyHandler {
|
|||||||
|
|
||||||
transport := &http.Transport{
|
transport := &http.Transport{
|
||||||
DialContext: dialer.DialContext,
|
DialContext: dialer.DialContext,
|
||||||
MaxIdleConns: 200,
|
MaxIdleConns: 300, // 增加最大空闲连接数
|
||||||
MaxIdleConnsPerHost: 20,
|
MaxIdleConnsPerHost: 50, // 增加每个主机的最大空闲连接数
|
||||||
IdleConnTimeout: idleConnTimeout,
|
IdleConnTimeout: idleConnTimeout,
|
||||||
TLSHandshakeTimeout: tlsHandshakeTimeout,
|
TLSHandshakeTimeout: tlsHandshakeTimeout,
|
||||||
ExpectContinueTimeout: 1 * time.Second,
|
ExpectContinueTimeout: 1 * time.Second,
|
||||||
MaxConnsPerHost: 50,
|
MaxConnsPerHost: 100, // 增加每个主机的最大连接数
|
||||||
DisableKeepAlives: false,
|
DisableKeepAlives: false,
|
||||||
DisableCompression: false,
|
DisableCompression: false,
|
||||||
ForceAttemptHTTP2: true,
|
ForceAttemptHTTP2: true,
|
||||||
WriteBufferSize: 64 * 1024,
|
WriteBufferSize: 64 * 1024,
|
||||||
ReadBufferSize: 64 * 1024,
|
ReadBufferSize: 64 * 1024,
|
||||||
ResponseHeaderTimeout: backendServTimeout,
|
ResponseHeaderTimeout: backendServTimeout,
|
||||||
|
// HTTP/2 特定设置
|
||||||
|
MaxResponseHeaderBytes: 64 * 1024, // 增加最大响应头大小
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置HTTP/2传输配置
|
||||||
|
http2Transport, err := http2.ConfigureTransports(transport)
|
||||||
|
if err == nil && http2Transport != nil {
|
||||||
|
http2Transport.ReadIdleTimeout = 10 * time.Second // HTTP/2读取超时
|
||||||
|
http2Transport.PingTimeout = 5 * time.Second // HTTP/2 ping超时
|
||||||
|
http2Transport.AllowHTTP = false // 只允许HTTPS
|
||||||
|
http2Transport.MaxReadFrameSize = 32 * 1024 // 增加帧大小
|
||||||
|
http2Transport.StrictMaxConcurrentStreams = true // 严格遵守最大并发流
|
||||||
}
|
}
|
||||||
|
|
||||||
return &ProxyHandler{
|
return &ProxyHandler{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user