go - 取消 HTTP 请求时关闭所有 goroutine

标签 go synchronization web-crawler go-echo


我正在制作一个网络爬虫。我通过爬虫函数传递 url 并解析它以获取 anchor 标记中的所有链接,然后我为所有这些 url 调用相同的爬虫函数,为每个 url 使用单独的 goroutine。
但是,如果在我收到响应之前发送请求并取消它,则该特定请求的所有 groutine 仍在运行。
现在我想要的是,当我取消请求时,所有因该请求而被调用的 goroutine 都会停止。
请指导。
以下是我的爬虫函数代码。

func crawler(c echo.Context, urlRec string, feed chan string, urlList *[]string, wg *sync.WaitGroup) {
    defer wg.Done()
    URL, _ := url.Parse(urlRec)
    response, err := http.Get(urlRec)
    if err != nil {
        log.Print(err)
        return
    }

    body := response.Body
    defer body.Close()

    tokenizer := html.NewTokenizer(body)
    flag := true
    for flag {
        tokenType := tokenizer.Next()
        switch {
        case tokenType == html.ErrorToken:
            flag = false
            break
        case tokenType == html.StartTagToken:
            token := tokenizer.Token()

            // Check if the token is an <a> tag
            isAnchor := token.Data == "a"
            if !isAnchor {
                continue
            }

            ok, urlHref := getReference(token)
            if !ok {
                continue
            }

            // Make sure the url begines in http**
            hasProto := strings.Index(urlHref, "http") == 0
            if hasProto {
                if !urlInURLList(urlHref, urlList) {
                    if strings.Contains(urlHref, URL.Host) {
                        *urlList = append(*urlList, urlHref)
                        // fmt.Println(urlHref)
                        // c.String(http.StatusOK, urlHref+"\n")Documents
                        if !checkExt(filepath.Ext(urlHref)) {
                            wg.Add(1)
                            go crawler(c, urlHref, feed, urlList, wg)
                        }
                    }
                }
            }
        }
    }
}

下面是我的 POST 请求处理程序

func scrapePOST(c echo.Context) error {
    var urlList []string
    urlSession := urlFound{}
    var wg sync.WaitGroup
    urlParam := c.FormValue("url")
    feed := make(chan string, 1000)
    wg.Add(1)
    go crawler(c, urlParam, feed, &urlList, &wg)
    wg.Wait()
    var count = 0
    for _, url := range urlList {
        if filepath.Ext(url) == ".jpg" || filepath.Ext(url) == ".jpeg" || filepath.Ext(url) == ".png" {
            urlSession.Images = append(urlSession.Images, url)
        } else if filepath.Ext(url) == ".doc" || filepath.Ext(url) == ".docx" || filepath.Ext(url) == ".pdf" || filepath.Ext(url) == ".ppt" {
            urlSession.Documents = append(urlSession.Documents, url)
        } else {
            urlSession.Links = append(urlSession.Links, url)
        }
        count = count + 1
    }
    urlSession.Count = count
    // jsonResp, _ := json.Marshal(urlSession)
    // fmt.Print(urlSession)
    return c.JSON(http.StatusOK, urlSession)
}

最佳答案

回显上下文公开了 HTTP 请求,它已经有一个与服务器请求相关联的上下文。只需获取该上下文,检查它是否取消,和/或将其传递给采用上下文的方法。

ctx := c.Request().Context()
select {
case <-ctx.Done():
    return ctx.Err()
default:
    // Continue handling the request
}

// and pass along to the db or whatever else:
rows, err := db.QueryContext(ctx, ...)

如果客户端中止连接,请求范围的上下文将自动取消。

如果您想添加自己的取消条件(超时或其他),您也可以这样做:

req := c.Request()
ctx, cancel := context.WithCancel(req.Context())
req = req.WithContext(ctx)
defer cancel()
// do stuff, which may conditionally call cancel() to cancel the context early

关于go - 取消 HTTP 请求时关闭所有 goroutine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45525332/

相关文章:

go - 如何将 slice 作为可变输入传递?

xml - golang : Omit name of struct in xml.编码(marshal)

java - Java : no matter the number of writing threads… it only really matters if the operation is atomic or not中的同步关键字

search-engine - 谷歌在我的网站上索引了我的测试文件夹 :( How do I restrict the web crawlers!

search-engine - 如何使用 Apache Nutch 保存原始 html 文件

postgresql - 如何在postgresql中跟踪sql查询

go - 将 int96 时间戳从 Parquet 转换到 golang

linux - 安排 cron 条目仅在尚未运行时运行脚本

c - 关于信号量和条件变量

python - 重定向后的 Scrapy 回调