This is a creation in Article, where the information may have evolved or changed.
Description
Since the use of Go's own net/http
library and the use of the HTTP proxy in conjunction with too many open files
the encounter and too many open connections
such errors, resulting in the inability to create a new connection, because the use of the connection is not freed, this article is only to summarize the settings to solve this problem
Code
When an HTTP request is assembled, the connection is automatically closed when access is complete, so that the server and client are not kept connected, causing the resource to be consumed
http_requ, _ := http.NewRequest(req.Method, dest_url, req.Body)http_requ.Header = req.Header//在头部设置使用之后关闭连接http_requ.Header.Set("Connection", "close")//或者直接设置Closehttp_requ.Close = true
Other settings are to look at the comments in the source code and guess
http_client := &http.Client{ Timeout: time.Duration(3 * time.Second),//客户端设置3秒超时}
http_client. Transport = &http. transport{disablekeepalives:false,//turns off connection multiplexing because too many background connections end up with port exhaustion Maxidleconns:-1,//maximum number of idle connections Idleconntimeout:tim E.duration (3 * time. Second),//Idle connection timeout time proxy:http. Proxyurl (PROXY_ADDR),//Set HTTP proxy address}