feat: Charts 与 LoadFeed 支持 context 透传取消
- 新增 ChartsContext / LoadFeedContext(原方法转发 context.Background(),兼容不变) - Charts 榜单请求与逐项 lookup 全部改 NewRequestWithContext,循环顶检查 ctx.Err() 提前中止剩余 lookup - 修复 resp 在 err 检查前解引用(网络错误/ctx 取消时必 panic) - 修复 lookup 循环 User-Agent 误设在外层请求上 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -63,13 +63,19 @@ type Podcast struct {
|
||||
Medium Medium `json:"medium,omitempty"`
|
||||
}
|
||||
|
||||
// LoadFeed 从URL加载播客RSS订阅,timeout为超时时间,如果timeout<=0则使用默认值30秒
|
||||
// LoadFeed 从URL加载播客RSS订阅,timeout为超时时间,如果timeout<=0则使用默认值30秒。
|
||||
// 需要透传取消时请用 LoadFeedContext。
|
||||
func LoadFeed(feedURL, proxy string, timeout time.Duration) (*Podcast, error) {
|
||||
return LoadFeedContext(context.Background(), feedURL, proxy, timeout)
|
||||
}
|
||||
|
||||
// LoadFeedContext 同 LoadFeed,但请求生命周期挂在调用方 ctx 上:ctx 取消/超时即中断下载。
|
||||
func LoadFeedContext(ctx context.Context, feedURL, proxy string, timeout time.Duration) (*Podcast, error) {
|
||||
if timeout <= 0 {
|
||||
timeout = 30 * time.Second
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
ctx, cancel := context.WithTimeout(ctx, timeout)
|
||||
defer cancel()
|
||||
|
||||
fp := gofeed.NewParser()
|
||||
|
||||
Reference in New Issue
Block a user