From ed796e6d7f1d87b4680817d5fd13e8d93b8e6cb3 Mon Sep 17 00:00:00 2001 From: xing Date: Mon, 18 Mar 2024 14:58:11 +0800 Subject: [PATCH] defer release resouce --- bbclearn/bbc.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/bbclearn/bbc.go b/bbclearn/bbc.go index 2cdae9c..a60b7e5 100644 --- a/bbclearn/bbc.go +++ b/bbclearn/bbc.go @@ -65,6 +65,7 @@ func fetch(u string) (r rss2.Item) { return } dom, err := goquery.NewDocumentFromReader(res.Body) + defer res.Body.Close() if err != nil { return } @@ -104,6 +105,7 @@ func downAndSendMail(doc *goquery.Document, title string) { if err != nil { return } + var fn []func() for _, ss := range []string{".bbcle-download-extension-pdf", ".bbcle-download-extension-mp3"} { uu, ok := doc.Find(ss).Attr("href") if ok { @@ -116,6 +118,10 @@ func downAndSendMail(doc *goquery.Document, title string) { if err != nil { continue } + fn = append(fn, func() { + response.Body.Close() + file.Close() + }) _, err = io.Copy(file, response.Body) if err != nil { continue @@ -123,6 +129,11 @@ func downAndSendMail(doc *goquery.Document, title string) { mm.f = append(mm.f, name) } } + defer func() { + for _, f := range fn { + f() + } + }() if len(mm.f) < 1 { return }