From 566482ab2cdacb7c2d83911573845115c1c41a84 Mon Sep 17 00:00:00 2001 From: nanlei <36439285+nanlei2000@users.noreply.github.com> Date: Sat, 4 Jun 2022 06:57:19 +0800 Subject: [PATCH] Add http timeout --- pkg/weibo/download.go | 11 +++++++++-- pkg/weibo/download_test.go | 4 +++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/pkg/weibo/download.go b/pkg/weibo/download.go index 6aefbe5..e2d460f 100644 --- a/pkg/weibo/download.go +++ b/pkg/weibo/download.go @@ -1,6 +1,7 @@ package weibo import ( + "context" "fmt" "io" "io/ioutil" @@ -87,18 +88,24 @@ func (w *Weibo) downloadPics(pics []string, distDir string) error { go func(p string) (err error) { c <- struct{}{} defer func() { - wg.Done() + if pErr := recover(); pErr != nil { + err = fmt.Errorf("panic: err: %v", pErr) + } if err != nil { lastErr = err } <-c + wg.Done() }() // 防止频控 ran := rand.Int31n(100) time.Sleep(time.Duration(ran) * time.Millisecond) - req, err := http.NewRequest("GET", p, nil) + ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) + defer cancel() + + req, err := http.NewRequestWithContext(ctx, "GET", p, nil) if err != nil { return err } diff --git a/pkg/weibo/download_test.go b/pkg/weibo/download_test.go index 87b51ee..526f165 100644 --- a/pkg/weibo/download_test.go +++ b/pkg/weibo/download_test.go @@ -1,6 +1,8 @@ package weibo -import "testing" +import ( + "testing" +) func TestDownLoadShowPics(t *testing.T) { w := Weibo{}