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 {