net/http 出现“使用关闭的网络连接”错误

nir*_*jju 4 tcp http go

我收到很多类似下面提到的错误,

读取 tcp xx.xx.xx.xx:80:使用封闭的网络连接

读取 tcp xx.xx.xx.xx:80:连接被对等方重置

//HTTP连接函数

func GetResponseBytesByURL_raw(restUrl, connectionTimeOutStr, readTimeOutStr string) ([]byte, error) {
    connectionTimeOut, _ /*err*/ := time.ParseDuration(connectionTimeOutStr)
    readTimeOut, _ /*err*/ := time.ParseDuration(readTimeOutStr)
    timeout := connectionTimeOut + readTimeOut // time.Duration((strconv.Atoi(connectionTimeOutStr) + strconv.Atoi(readTimeOutStr)))
    //timeout = 200 * time.Millisecond
    client := http.Client{
        Timeout: timeout,
    }
    resp, err := client.Get(restUrl)
    if nil != err {
        logger.SetLog("Error GetResponseBytesByURL_raw |err: ", logs.LevelError, err)
        return make([]byte, 0), err
    }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    return body, err
}
Run Code Online (Sandbox Code Playgroud)

更新(7 月 14 日):

服务器:NumCPU=8,RAM=24GB,GO=go1.4.2.linux-amd64

我在一些高流量期间遇到这样的错误。 每分钟 20000-30000 个请求,我有500 毫秒的时间范围从第三方 api 获取响应。

来自我的服务器的netstat 状态(使用:netstat -nat | awk '{print $6}' | sort | uniq -c | sort -n)来获取频率

      1 established)
      1 Foreign
      9 LISTEN
     33 FIN_WAIT1
    338 ESTABLISHED
   5530 SYN_SENT
  32202 TIME_WAIT
Run Code Online (Sandbox Code Playgroud)

系统控制-p

**sysctl -p**
fs.file-max = 2097152
vm.swappiness = 10
vm.dirty_ratio = 60
vm.dirty_background_ratio = 2
net.ipv4.tcp_synack_retries = 2
net.ipv4.ip_local_port_range = 2000 65535
net.ipv4.tcp_rfc1337 = 1
net.ipv4.tcp_fin_timeout = 5
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_keepalive_probes = 5
net.ipv4.tcp_keepalive_intvl = 15
net.core.rmem_default = 31457280
net.core.rmem_max = 12582912
net.core.wmem_default = 31457280
net.core.wmem_max = 12582912
net.core.somaxconn = 65536
net.core.netdev_max_backlog = 65536
net.core.optmem_max = 25165824
net.ipv4.tcp_mem = 65536 131072 262144
net.ipv4.udp_mem = 65536 131072 262144
net.ipv4.tcp_rmem = 8192 87380 16777216
net.ipv4.udp_rmem_min = 16384
net.ipv4.tcp_wmem = 8192 65536 16777216
net.ipv4.udp_wmem_min = 16384
net.ipv4.tcp_max_tw_buckets = 1440000
net.ipv4.tcp_tw_recycle = 0
net.ipv4.tcp_tw_reuse = 1
net.ipv6.bindv6only = 1
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.icmp_echo_ignore_broadcasts = 1
error: "net.ipv4.icmp_ignore_bogus_error_messages" is an unknown key
kernel.exec-shield = 1
kernel.randomize_va_space = 1
net.ipv4.conf.all.log_martians = 1
net.ipv4.conf.default.log_martians = 1
net.ipv4.icmp_ignore_bogus_error_responses = 1
net.ipv4.ip_forward = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.all.secure_redirects = 0
net.ipv4.conf.default.secure_redirects = 0
Run Code Online (Sandbox Code Playgroud)

Jim*_*imB 5

当通过互联网进行高速率连接时,您很可能会遇到一些连接问题。您无法完全缓解它们,因此您可能需要在请求周围添加重试逻辑。此时实际的错误类型可能并不重要,但如果您想具体的话,匹配use of closed network connection或 的错误字符串connection reset by peer大约是您可以做的最好的事情。确保通过回退来限制重试,因为某些系统会断开或重置连接作为限制请求率的一种方式,并且重新连接的速度越快,您可能会收到更多的错误。

根据您正在通信的远程主机的数量,您可能需要增加数量Transport.MaxIdleConnsPerHost(默认值仅为 2)。与您交谈的主机越少,您可以将其设置得越高。这将减少新连接的数量,并总体上加快请求速度。

如果可以的话,尝试一下 go1.5 beta。关于保持活动连接的一些更改可能有助于减少您看到的错误数量。