无法在返回函数中正确处理错误

该代码向主机发送并行请求,如果我发送有效的主机,该程序工作正常,但是如果我在执行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


                        }



qq_花开花谢_0
浏览 78回答 1
1回答

慕盖茨4494581

当 触发错误时,您将立即返回,而不会从通道中删除阻塞项。Get您可以在此处删除阻止项,也可以删除并实际在结构中存储指向 的指针(这样就可以从失败的请求中存储)。returnhttp.Responsenil下面是使用第二种方法的 for 循环的代码,请注意,状态的打印现在处于一个中,因为它可能为 nil。else// a struct to hold a pointer to the result from each request// including an index which will be used for sorting the// results after they come intype result struct {&nbsp; &nbsp; index int&nbsp; &nbsp; res&nbsp; &nbsp;*http.Response&nbsp; &nbsp; err&nbsp; &nbsp;error}// ...// keen an index and loop through every url we will send a request to&nbsp; &nbsp; for i, url := range urls {&nbsp; &nbsp; &nbsp; &nbsp; // start a go routine with the index and url in a closure&nbsp; &nbsp; &nbsp; &nbsp; go func(i int, url string) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // this sends an empty struct into the semaphoreChan which&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // is basically saying add one to the limit, but when the&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // limit has been reached block until there is room&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; semaphoreChan <- struct{}{}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // send the request and put the response in a result struct&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // along with the index so we can sort them later along with&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // any error that might have occoured&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res, err := http.Get(url)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(err)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(res.Status)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result := &result{i, res, err}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // now we can send the result struct through the resultsChan&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resultsChan <- result&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // once we're done it's we read from the semaphoreChan which&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // has the effect of removing one from the limit and allowing&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // another goroutine to start&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <-semaphoreChan&nbsp; &nbsp; &nbsp; &nbsp; }(i, url)&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go