Skip to content

Commit

Permalink
优化头像更新逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
movsb committed Dec 9, 2024
1 parent 234824a commit 2b66015
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions service/modules/renderers/friends/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 2b66015

Please sign in to comment.