请问如何处理net.Conn连接超时?

每个协程创建一个新的net.Conn, 我试过net.DialTimeout, conn.SetDeadline, 在几百个并发请求的时候,会出现如下错误:

goroutine 46669 [GC assist wait]:
net.IPv4(...)
        /usr/local/go/src/net/ip.go:46
net.parseIPv4(0xc420509a59, 0x2, 0xc4200160b8, 0x707cbd, 0x3)
        /usr/local/go/src/net/ip.go:530 +0x1bf
net.(*Resolver).internetAddrList(0x893d50, 0x8620c0, 0xc4200160b8, 0x707cbd, 0x3, 0xc420509a50, 0x10, 0x0, 0x0, 0x0, ...)
        /usr/local/go/src/net/ipsock.go:280 +0x228
net.(*Resolver).resolveAddrList(0x893d50, 0x8620c0, 0xc4200160b8, 0x707e48, 0x4, 0x707cbd, 0x3, 0xc420509a50, 0x10, 0x0, ...)
        /usr/local/go/src/net/dial.go:193 +0x594
net.(*Dialer).DialContext(0xc42036ecf0, 0x8620c0, 0xc4200160b8, 0x707cbd, 0x3, 0xc420509a50, 0x10, 0x0, 0x0, 0x0, ...)
        /usr/local/go/src/net/dial.go:375 +0x248
net.(*Dialer).Dial(0xc4203664f0, 0x707cbd, 0x3, 0xc420509a50, 0x10, 0x412c2d, 0xc420779c98, 0x71cea8, 0x4b0a55)
        /usr/local/go/src/net/dial.go:320 +0x75
net.Dial(0x707cbd, 0x3, 0xc420509a50, 0x10, 0x69b540, 0x740080, 0xc400000076, 0x412c2d)
        /usr/local/go/src/net/dial.go:291 +0x99

应该时gcc没有正常回收这些协程吧? 我现在defer conn.Close()好像就没有这个问题, conn必须主动close吗?我以为gc会自动回收呢...

已邀请:

zdt3476

赞同来自:

GC回收的是内存啊大兄弟,连接肯定得主动Close啊。 net.DialTimeout conn.SetDeadline,你看下注释就明白了,不是你以为的那种用法啊。。。

要回复问题请先登录注册