问题描述:Go客户端通过http方式访问服务器,并发访问时出现大量TIME_WAIT的TCP连接
过程:已经尝试过各种修改,最后确定测试Go语言的http标准库,包括自定义transport等方法,仍然无法解决问题,还请大神赐教。(不想修改内核参数)
测试代码如下,最简单的方式了:
// adtestNew project adtestNew.go
package main
import (
"io"
"io/ioutil"
"net/http"
"time"
)
var client *http.Client
func main() {
//client = http.DefaultClient
tr := &http.Transport{
DisableKeepAlives: false,
MaxIdleConnsPerHost: 40,
}
client = &http.Client{Transport: tr}
timer1 := time.NewTicker(time.Second * 10)
<-timer1.C
for {
go dotest()
go dotest()
go dotest()
go dotest()
time.Sleep(time.Millisecond * 500)
}
}
func dotest() {
url := "http://www.baidu.com"
res, _ := http.Get(url)
io.Copy(ioutil.Discard, res.Body)
res.Body.Close()
}