From 2b66015b1baf0a6607b671e0c6fc369afc6c3cd9 Mon Sep 17 00:00:00 2001 From: movsb Date: Mon, 9 Dec 2024 14:19:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=A4=B4=E5=83=8F=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/modules/renderers/friends/task.go | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/service/modules/renderers/friends/task.go b/service/modules/renderers/friends/task.go index fbd6a32b..115b4f84 100644 --- a/service/modules/renderers/friends/task.go +++ b/service/modules/renderers/friends/task.go @@ -135,10 +135,23 @@ func (t *Task) refreshLoop(ctx context.Context) { } } +// 会重复多次尝试获取。 func (t *Task) update(postID int, faviconURL string) { - contentType, content, err := t.get(faviconURL) + var ( + contentType string + content []byte + err error + ) + for range 3 { + contentType, content, err = t.get(faviconURL) + if err != nil { + log.Println(faviconURL, err) + time.Sleep(time.Second * 5) + continue + } + break + } if err != nil { - log.Println(faviconURL, err) return } t.cache.Set(CacheKey{postID, faviconURL}, CacheValue{ @@ -174,7 +187,11 @@ func (t *Task) get(faviconURL string) (string, []byte, error) { log.Println(`头像请求失败:`, rsp.Status) return ``, nil, fmt.Errorf(`StatusCode: %d`, rsp.StatusCode) } - body, _ := io.ReadAll(io.LimitReader(rsp.Body, maxBodySize)) + body, err := io.ReadAll(io.LimitReader(rsp.Body, maxBodySize)) + if err != nil { + log.Println(`读取头像 body 时出错:`, err) + return ``, nil, err + } contentType, _, _ := mime.ParseMediaType(rsp.Header.Get(`Content-Type`)) if contentType == "" { contentType = http.DetectContentType(body)