我正在用 Go 构建一个工具,它需要向许多不同的服务器同时发出大量 HTTP 请求。我在 Python 中的初始原型可以同时处理数百个请求。
但是,我发现在 Go 中Get http://www.google.com: dial tcp 216.58.205.228:80: i/o timeout
,如果同时请求的数量超过 ~ 30-40,这几乎总是会导致 a for some 。
我已经在 macOS、openSUSE、不同的硬件、不同的网络和不同的域列表上进行了测试,并且按照其他 Stackoverflow 答案中的描述更改 DNS 服务器也不起作用。
有趣的是,失败的请求甚至不会产生数据包,这在使用 Wireshark 进行检查时可以看出。
有什么我做错了还是 Go 中的错误?
最低可重现程序如下:
package main
import (
"fmt"
"net/http"
"sync"
)
func main() {
domains := []string{/* large domain list here, eg from https://moz.com/top500 */}
limiter := make(chan string, 50) // Limits simultaneous requests
wg := sync.WaitGroup{} // Needed to not prematurely exit before all requests have been finished
for i, domain := range domains {
wg.Add(1)
limiter <- domain
go func(i int, domain string) {
defer func() { <-limiter }()
defer wg.Done()
resp, err := http.Get("http://"+domain)
if err != nil {
fmt.Printf("%d %s failed: %s\n", i, domain, err)
return
}
fmt.Printf("%d %s: %s\n", i, domain, resp.Status)
}(i, domain)
}
wg.Wait()
}
Two particular error messages are happening, a net.DNSError
that does not make any sense and a non-descript poll.TimeoutError
:
&url.Error{Op:"Get", URL:"http://harvard.edu", Err:(*net.OpError)(0xc00022a460)}
&net.OpError{Op:"dial", Net:"tcp", Source:net.Addr(nil), Addr:net.Addr(nil), Err:(*net.DNSError)(0xc000aca200)}
&net.DNSError{Err:"no such host", Name:"harvard.edu", Server:"", IsTimeout:false, IsTemporary:false}
&url.Error{Op:"Get", URL:"http://latimes.com", Err:(*net.OpError)(0xc000d92730)}
&net.OpError{Op:"dial", Net:"tcp", Source:net.Addr(nil), Addr:net.Addr(nil), Err:(*poll.TimeoutError)(0x14779a0)}
&poll.TimeoutError{}
Update:
Running the requests with a seperate http.Client
as well as http.Transport
and net.Dialer
does not make any difference as can be seen when running code from this playground.