From 6e41fae8a15ec981c96b161d8672dd12104ea5cd Mon Sep 17 00:00:00 2001 From: malai Date: Mon, 20 Jul 2026 09:51:36 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20SearchPodcasts=20=E6=94=AF=E6=8C=81=20c?= =?UTF-8?q?ontext=20=E9=80=8F=E4=BC=A0=E5=B9=B6=E4=BF=AE=E5=A4=8D=20resp?= =?UTF-8?q?=20nil=20=E8=A7=A3=E5=BC=95=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 与 Charts/LoadFeed 同型:新增 SearchPodcastsContext(原方法转发 context.Background() 保持兼容);resp.StatusCode 日志移到 err 检查后, 消除网络错误/ctx 取消时的 nil 解引用 panic。 Co-Authored-By: Claude Fable 5 --- search/itunes_search.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/search/itunes_search.go b/search/itunes_search.go index 227b958..f06fc2c 100644 --- a/search/itunes_search.go +++ b/search/itunes_search.go @@ -128,8 +128,13 @@ type ChartsOptions struct { QueryParams map[string]string } -// SearchPodcasts 搜索播客 +// SearchPodcasts 搜索播客(无取消语义,等价于 SearchPodcastsContext(context.Background(), ...))。 func (p *ITunesProvider) SearchPodcasts(term string, options *SearchOptions) (*model.SearchResult2, error) { + return p.SearchPodcastsContext(context.Background(), term, options) +} + +// SearchPodcastsContext 搜索播客,请求挂在调用方 ctx 上:ctx 取消/超时即中断。 +func (p *ITunesProvider) SearchPodcastsContext(ctx context.Context, term string, options *SearchOptions) (*model.SearchResult2, error) { if term == "" { return nil, fmt.Errorf("搜索词不能为空") } @@ -173,7 +178,7 @@ func (p *ITunesProvider) SearchPodcasts(term string, options *SearchOptions) (*m p.Logger.Infof("url: %s", url) // 发送请求 - req, err := http.NewRequest("GET", url, nil) + req, err := http.NewRequestWithContext(ctx, "GET", url, nil) if err != nil { return nil, fmt.Errorf("创建请求失败: %w", err) } @@ -184,11 +189,11 @@ func (p *ITunesProvider) SearchPodcasts(term string, options *SearchOptions) (*m // 发送请求 p.Limiter.Take() resp, err := p.Client.Do(req) - p.Logger.Infof("response code: %d", resp.StatusCode) if err != nil { return nil, fmt.Errorf("发送请求失败: %w", err) } defer resp.Body.Close() + p.Logger.Infof("response code: %d", resp.StatusCode) // 检查响应状态 if resp.StatusCode != http.StatusOK {