Go HTTP客户端性能优化核心是连接池调参、Client复用和异步限流:调大MaxIdleConns/PerHost、设IdleConnTimeout等;全局复用client实例;用errgroup并发限流请求。
Go 的 HTTP 客户端默认使用 http.DefaultTransport,它已内置连接复用(Keep-Alive)和基础连接池,但若不显式调优,在高并发、低延迟或大量外部请求场景下,仍易出现连接耗尽、DNS 阻塞、TLS 握手慢、响应等待久等问题。优化核心是两点:**管好连接生命周期(连接池) + 让请求不互相卡住(异步/并发控制)**。
默认连接池对每个 host 最多复用 2 个空闲连接,最大总连接数也偏低,容易成为瓶颈。需根据业务规模合理扩大:
1000)100 起90 * time.Second
5 * time.Second
示例配置:
transport := &http.Transport{
MaxIdleConns: 1000,
MaxIdleConnsPerHost: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 5 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
client := &http.Client{Transport: transport}每次 new http.Client 都会新建 Transport,丢失连接池上下文。Client 应全局复用(如定义为包变量或注入到结构体中),尤其在 HTTP handler 或微服务调用中:
立即学习“go语言免费学习笔记(深入)”;
http.Get(...) 或 new(http.Client)
context.WithTimeout 控制单次请求,而非换 client多个独立请求(如查多个下游 API)不应串行阻塞。用并发加速,但必须限流防打崩对方或自身 fd 耗尽:
sync.WaitGroup 启动 goroutine,并发请求后统一等待golang.org/x/sync/errgroup,自动传播错误、支持 context 取消示例(errgroup 限流):
g, ctx := errgroup.WithContext(r.Context()) g.SetLimit(10) // 最多同时 10 个请求for , url := range urls { url := url // 避免闭包引用 g.Go(func() error { req, := http.NewRequestWithContext(ctx, "GET", url, nil) resp, err := client.Do(req) if err != nil { return err } defer resp.Body.Close() // 处理 resp... return nil }) }
if err := g.Wait(); err != nil { // 处理错误 }
github.com/miekg/dns 或用 net.Resolver 自建缓存CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse }
ioutil.ReadAll,改用 io.LimitReader(resp.Body, maxBytes) 防止 OOM基本上就这些。连
接池调参 + client 复用 + 异步限流,三者配合就能覆盖 90% 的 Go HTTP 性能瓶颈。不复杂但容易忽略。