From 076dc4f9ef96458a14de5c54c2f2bc4437c74dcc Mon Sep 17 00:00:00 2001 From: ArvinLovegood Date: Fri, 28 Mar 2025 21:31:35 +0800 Subject: [PATCH] =?UTF-8?q?fix(backend/data):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E7=88=AC=E8=99=AB=E4=BB=BB=E5=8A=A1=E5=8F=96=E6=B6=88=E5=90=8E?= =?UTF-8?q?=E6=9C=AA=E5=85=B3=E9=97=AD=20ctx=20=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 crawler_api.go 文件中的多个函数中添加了 chromedp.Cancel(ctx) 调用 - 确保在任务取消时能够正确关闭 ctx,避免资源泄露 --- backend/data/crawler_api.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/backend/data/crawler_api.go b/backend/data/crawler_api.go index d78c8c8..b20a3f2 100644 --- a/backend/data/crawler_api.go +++ b/backend/data/crawler_api.go @@ -68,6 +68,7 @@ func (c *CrawlerApi) GetHtml(url, waitVisible string, headless bool) (string, bo defer pcancel() ctx, cancel := chromedp.NewContext(pctx, chromedp.WithLogf(logger.SugaredLogger.Infof)) defer cancel() + defer chromedp.Cancel(ctx) err := chromedp.Run(ctx, chromedp.Navigate(url), chromedp.WaitVisible(waitVisible, chromedp.ByQuery), // 确保 元素可见 chromedp.WaitReady(waitVisible, chromedp.ByQuery), // 确保 元素准备好 @@ -80,6 +81,7 @@ func (c *CrawlerApi) GetHtml(url, waitVisible string, headless bool) (string, bo } else { ctx, cancel := chromedp.NewContext(c.crawlerCtx, chromedp.WithLogf(logger.SugaredLogger.Infof)) defer cancel() + defer chromedp.Cancel(ctx) err := chromedp.Run(ctx, chromedp.Navigate(url), chromedp.WaitVisible("body"), chromedp.InnerHTML("body", &htmlContent)) if err != nil { logger.SugaredLogger.Error(err.Error()) @@ -197,6 +199,7 @@ func (c *CrawlerApi) GetHtmlWithActions(actions *[]chromedp.Action, headless boo defer pcancel() ctx, cancel := chromedp.NewContext(pctx, chromedp.WithLogf(logger.SugaredLogger.Infof)) defer cancel() + defer chromedp.Cancel(ctx) err := chromedp.Run(ctx, *actions...) if err != nil { @@ -206,6 +209,7 @@ func (c *CrawlerApi) GetHtmlWithActions(actions *[]chromedp.Action, headless boo } else { ctx, cancel := chromedp.NewContext(c.crawlerCtx, chromedp.WithLogf(logger.SugaredLogger.Infof)) defer cancel() + defer chromedp.Cancel(ctx) err := chromedp.Run(ctx, *actions...) if err != nil {