该代码向主机发送并行请求,如果我发送有效的主机,该程序工作正常,但是如果我在执行10次后发送无效主机,则该程序将停止工作。但是这个程序已经在结构中返回错误。和main函数处理不正确,我无法找出为什么这个程序在返回10个无效结果后停止。它不会返回 100 个结果。
import (
"fmt"
"net/http"
"sort"
"time"
)
// a struct to hold the result from each request including an index
// which will be used for sorting the results after they come in
type result struct {
index int
res http.Response
err error
}
// boundedParallelGet sends requests in parallel but only up to a certain
// limit, and furthermore it's only parallel up to the amount of CPUs but
// is always concurrent up to the concurrency limit
func boundedParallelGet(urls []string, concurrencyLimit int) []result {
// this buffered channel will block at the concurrency limit
semaphoreChan := make(chan struct{}, concurrencyLimit)
// this channel will not block and collect the http request results
resultsChan := make(chan *result)
// make sure we close these channels when we're done with them
defer func() {
close(semaphoreChan)
close(resultsChan)
}()
// keen an index and loop through every url we will send a request to
for i, url := range urls {
// start a go routine with the index and url in a closure
go func(i int, url string) {
// this sends an empty struct into the semaphoreChan which
// is basically saying add one to the limit, but when the
// limit has been reached block until there is room
semaphoreChan <- struct{}{}
// send the request and put the response in a result struct
// along with the index so we can sort them later along with
// any error that might have occoured
res, err := http.Get(url)
if err != nil {
fmt.Println(err)
return
}
慕盖茨4494581
相关分类