直接用 http.Get 开多个 goroutine 容易失败,因默认客户端连接池限制(MaxIdleConns 和 MaxIdleConnsPerHost 均为100),高并发时请求阻塞排队、超时或取消;应自定义 Client 并设 Timeout,用带缓冲 channel 限流并发。
http.Get 开多个 goroutine 容易失败Go 的 http.DefaultClient 底层复用 TCP 连接,但默认只允许最多 100 个并发连接(MaxIdleConns)和每 host 最多 100 个(MaxIdleConnsPerHost)。如果你起 500 个 goroutine 调 http.Get,大量请求会阻塞在连接池排队,甚至超时或返回 net/http: request canceled (Client.Timeout exceeded)。
http.Client,调大连接池参数Timeout,否则失败请求可能永久挂起 goroutinehttp.Get —— 它用的是默认 client,不可控核心是控制并发数,不能无节制起 goroutine。用带缓冲的 channel 当“信号量”最直观:它天然限流,且能配合 select 做超时/取消。
sem := make(chan struct{}, 10) // 限制最多 10 并发
for _, url := range urls {
sem <- struct{}{} // 获取令牌
go func(u string) {
defer func() { <-sem }() // 归还令牌
resp, err := client.Get(u)
if err != nil {
log.Printf("download %s failed: %v", u, err)
return
}
defer resp.Body.Close()
// 写入文件...
}(url)
}defer func() { ,不能在外层,否则会提前释放
url 变量(常见 bug)用 io.Copy 流式写入,别把整个响应体读进 []byte。尤其下载大文件时,resp.Body.ReadAll() 会一次性分配几百 MB 内存,极易 OOM。
out, err := os.Create(filename)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, resp.Body) // 零拷贝流式写入
if err != nil {
return err
}
io.Copy 内部用 32KB 缓冲区,内存占用恒定defer resp.Body.Close(),否则连接不释放,连接池迅速耗尽io.TeeReader 边读边算,不要先存再算http.Client 默认跟随重定向(CheckRedirect 为 nil),但 404 不报错,需手动检查 resp.StatusCode;自签名证书则要定制 Transport.TLSClientConfig。
200 当作成功,其余统一按错误处理
CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse }
&http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}},生产环境必须配好 CA真正难的不是并发本身,而是每个下载请求背后隐含的状态管理
