为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

Go
qq_花开花谢_0 2022-08-15 10:16:35
该代码向主机发送并行请求,如果我发送有效的主机,该程序工作正常,但是如果我在执行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 intype 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 limitfunc 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                        }
查看完整描述

1 回答

?
慕盖茨4494581

TA贡献1850条经验 获得超11个赞

当 触发错误时,您将立即返回,而不会从通道中删除阻塞项。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 in

type result struct {

    index int

    res   *http.Response

    err   error

}


// ...


// 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)

            } else {

                fmt.Println(res.Status)

            }


            result := &result{i, res, err}


            // now we can send the result struct through the resultsChan

            resultsChan <- result


            // once we're done it's we read from the semaphoreChan which

            // has the effect of removing one from the limit and allowing

            // another goroutine to start

            <-semaphoreChan


        }(i, url)

    }


查看完整回答
反对 回复 2022-08-15
  • 1 回答
  • 0 关注
  • 74 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信