Go 客户端程序生成了很多 TIME_WAIT 状态的套接字
Go client program generates a lot a sockets in TIME_WAIT state
我有一个 Go 程序,它从多个 goroutines 生成大量 HTTP 请求。在 运行ning 一段时间后,程序报错:连接:无法分配请求的地址。
在检查 netstat
时,我在 TIME_WAIT
中得到了大量 (28229) 的连接。
当我的 goroutine 数量为 3 时,会出现大量 TIME_WAIT
套接字,并且严重到足以在它为 5 时导致崩溃。
我 运行 Ubuntu 14.4 在 docker 下,然后转到版本 1.7
这是 Go 程序。
package main
import (
"io/ioutil"
"log"
"net/http"
"sync"
)
var wg sync.WaitGroup
var url="http://172.17.0.9:3000/";
const num_coroutines=5;
const num_request_per_coroutine=100000
func get_page(){
response, err := http.Get(url)
if err != nil {
log.Fatal(err)
} else {
defer response.Body.Close()
_, err =ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
}
}
func get_pages(){
defer wg.Done()
for i := 0; i < num_request_per_coroutine; i++{
get_page();
}
}
func main() {
for i:=0;i<num_coroutines;i++{
wg.Add(1)
go get_pages()
}
wg.Wait()
}
这是服务器程序:
package main
import (
"fmt"
"net/http"
"log"
)
var count int;
func sayhelloName(w http.ResponseWriter, r *http.Request) {
count++;
fmt.Fprintf(w,"Hello World, count is %d",count) // send data to client side
}
func main() {
http.HandleFunc("/", sayhelloName) // set router
err := http.ListenAndServe(":3000", nil) // set listen port
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
默认 http.Transport 打开和关闭连接的速度太快。由于所有连接都是相同的 host:port 组合,您需要增加 MaxIdleConnsPerHost
以匹配 num_coroutines
的值。否则,传输将频繁关闭额外的连接,只是让它们立即重新打开。
您可以在默认传输上全局设置:
http.DefaultTransport.(*http.Transport).MaxIdleConnsPerHost = numCoroutines
或者在创建自己的交通工具时
t := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
MaxIdleConnsPerHost: numCoroutines,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
类似问题:
我有一个 Go 程序,它从多个 goroutines 生成大量 HTTP 请求。在 运行ning 一段时间后,程序报错:连接:无法分配请求的地址。
在检查 netstat
时,我在 TIME_WAIT
中得到了大量 (28229) 的连接。
当我的 goroutine 数量为 3 时,会出现大量 TIME_WAIT
套接字,并且严重到足以在它为 5 时导致崩溃。
我 运行 Ubuntu 14.4 在 docker 下,然后转到版本 1.7
这是 Go 程序。
package main
import (
"io/ioutil"
"log"
"net/http"
"sync"
)
var wg sync.WaitGroup
var url="http://172.17.0.9:3000/";
const num_coroutines=5;
const num_request_per_coroutine=100000
func get_page(){
response, err := http.Get(url)
if err != nil {
log.Fatal(err)
} else {
defer response.Body.Close()
_, err =ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
}
}
func get_pages(){
defer wg.Done()
for i := 0; i < num_request_per_coroutine; i++{
get_page();
}
}
func main() {
for i:=0;i<num_coroutines;i++{
wg.Add(1)
go get_pages()
}
wg.Wait()
}
这是服务器程序:
package main
import (
"fmt"
"net/http"
"log"
)
var count int;
func sayhelloName(w http.ResponseWriter, r *http.Request) {
count++;
fmt.Fprintf(w,"Hello World, count is %d",count) // send data to client side
}
func main() {
http.HandleFunc("/", sayhelloName) // set router
err := http.ListenAndServe(":3000", nil) // set listen port
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
默认 http.Transport 打开和关闭连接的速度太快。由于所有连接都是相同的 host:port 组合,您需要增加 MaxIdleConnsPerHost
以匹配 num_coroutines
的值。否则,传输将频繁关闭额外的连接,只是让它们立即重新打开。
您可以在默认传输上全局设置:
http.DefaultTransport.(*http.Transport).MaxIdleConnsPerHost = numCoroutines
或者在创建自己的交通工具时
t := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
MaxIdleConnsPerHost: numCoroutines,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
类似问题: