gpt4 book ai didi

http - GOLANG,HTTP 有 "use of closed network connection"错误

转载 作者:IT王子 更新时间:2023-10-29 01:55:20 27 4
gpt4 key购买 nike

我遇到了很多如下所述的错误,

读取 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
}

更新(7 月 14 日):

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

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

netstat status 来 self 的服务器(使用: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

sysctl -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

最佳答案

当通过 Internet 进行高速连接时,您很可能会遇到一些连接问题。您无法完全缓解它们,因此您可能希望围绕请求添加重试逻辑。此时的实际错误类型可能无关紧要,但是匹配错误字符串以用于use of closed network connectionconnection reset by peer 是您能做的最好的事情如果你想具体一点。确保使用回退限制重试,因为某些系统会丢弃或重置连接作为限制请求率的一种方式,并且您重新连接的速度越快,您可能会遇到更多错误。

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

如果可以,试试go1.5 beta。围绕保持事件连接进行了一些更改,这可能有助于减少您看到的错误数量。

关于http - GOLANG,HTTP 有 "use of closed network connection"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31385487/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com