我正在尝试使N个获取请求,但是我的代码适用于8个URL,但10个始终可以堆叠而不会出现问题。
我是GO的新手,所以我无法理解问题。
我正在尝试编写一个应用程序来击败具有相同任务的.NET应用程序。
你能建议出什么问题了吗?
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
//"bufio"
"time"
)
type HttpResponse struct {
url string
response *http.Response
err error
}
func main() {
fmt.Println("Hello, world3.")
var urls []string = []string{
"www.webmagnat.ro",
"nickelfreesolutions.com",
"scheepvaarttelefoongids.nl",
"tursan.net",
"plannersanonymous.com",
"saltstack.com",
"deconsquad.com",
"migom.com",
"tjprc.org",
"worklife.dk",
"food-hub.org"}
start := time.Now()
results := asyncHttpGets(urls)
f, err := os.Create("test.txt")
if err != nil {
fmt.Println(err)
return
}
for _, result := range results {
fmt.Printf("%s status: %s\n", result.url,
result.response.Status)
l, err := f.WriteString(result.url+"\n")
if err != nil {
fmt.Println(err)
f.Close()
return
}
_ = l
}
t := time.Now()
elapsed := t.Sub(start)
fmt.Printf("Ellipsed: %s\n", elapsed)
err = f.Close()
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Buy, world2.")
}
func asyncHttpGets(urls []string) []*HttpResponse {
ch := make(chan *HttpResponse, len(urls)) // buffered
responses := []*HttpResponse{}
for _, url := range urls {
go func(url string) {
fmt.Printf("Fetching %s \n", url)
resp, err := http.Get("http://" + url)
if err != nil {
fmt.Printf("Failed to fetch %s\n", err)
return
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
fmt.Printf("HTTP Response Status : %v", resp.StatusCode)
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
bodyString := string(bodyBytes)
fmt.Printf("HTTP Response Content Length : %v\n", len(bodyString))
}
ch <- &HttpResponse{url, resp, err}
}(url)
}
for {
select {
case r := <-ch:
fmt.Printf("%s was fetched\n", r.url)
responses = append(responses, r)
if len(responses) == len(urls) {
return responses
}
case <-time.After(50 * time.Millisecond):
fmt.Printf(".")
}
}
return responses
}
https://play.golang.org/p/pcKYYM_PgIX
最佳答案
这里的第一个问题是发生错误时您不会返回响应,因此len(responses) == len(urls)
可能永远不会匹配,从而迫使循环永远继续。
首先为并发请求添加sync.WaitGroup
var wg sync.WaitGroup
ch := make(chan *HttpResponse)
responses := []*HttpResponse{}
for _, url := range urls {
wg.Add(1)
go func(url string) {
defer wg.Done()
然后,您可以遍历所有响应,直到完成所有出色的goroutines
go func() {
wg.Wait()
close(ch)
}()
for r := range ch {
fmt.Printf("%s was fetched\n", r.url)
responses = append(responses, r)
}
return responses
然后,您必须决定如何处理响应,是要在并发调用中读取响应,还是将其正文返回未读状态。由于要重新使用连接,您将始终尝试消耗主体,并且由于推迟了
Body.Close()
,因此当前需要在同一函数调用内进行。您可以更改httpResponse
类型以使其成为可能,或将resp.Body
替换为包含响应的缓冲区。最终,您将需要为客户端设置某种超时(可能使用
Context
),并对发出的并发请求数进行限制。
关于http - 如何发送和获取N> 10个URL的请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60148016/