重现代码:
package main
import (
"fmt"
"io"
"net/http"
"time"
)
func main() {
client := &http.Client{
Timeout: time.Duration(3) * time.Second,
}
for i := 0; i < 100; i++ {
go func() {
for {
req, _ := http.NewRequest(http.MethodGet, "https://baidu.com", nil)
rsp, err := client.Do(req)
if err != nil {
fmt.Println("request failed", err)
continue
}
rsp.Body.Close()
body, err := io.ReadAll(rsp.Body)
if err != nil {
fmt.Println("read body failed", err)
continue
}
fmt.Println(string(body))
}
}()
}
select {}
}
启动后,随着请求越来越多,很快就出现了"cannot assign requested address"错误,服务器出现大量TIME_WAIT连接。