@@ -585,85 +585,6 @@ func (s *rssProcessorService) processResponse(resp *http.Response, config model.
585585 return content , nil
586586}
587587
588- // 检查是否是可重试的错误
589- if lastErr != nil && ! s .isRetryableError (lastErr ) {
590- logger .Warn ("遇到不可重试的错误,停止重试" , "error" , lastErr )
591- break
592- }
593-
594- time .Sleep (backoffTime )
595- }
596-
597- start := time .Now ()
598-
599- // 为每次请求创建新的上下文,避免使用已取消的上下文
600- ctx , cancel := context .WithTimeout (context .Background (), apiTimeout )
601-
602- // 创建新的请求,使用新的上下文
603- reqWithCtx := req .WithContext (ctx )
604- resp , lastErr = client .Do (reqWithCtx )
605- requestDuration = time .Since (start )
606-
607- // 请求完成后取消上下文
608- cancel ()
609-
610- // 处理错误情况
611- if lastErr != nil {
612- // 特别处理超时错误
613- if strings .Contains (lastErr .Error (), "context deadline exceeded" ) || strings .Contains (lastErr .Error (), "timeout" ) {
614- logger .Warn ("API请求超时,准备重试" , "error" , lastErr , "attempt" , retryCount + 1 , "duration_ms" , requestDuration .Milliseconds ())
615- } else {
616- logger .Warn ("发送API请求失败,准备重试" , "error" , lastErr , "attempt" , retryCount + 1 , "duration_ms" , requestDuration .Milliseconds ())
617- }
618- continue
619- }
620-
621- // 读取响应
622- responseBody , err := s .readAPIResponse (resp , config )
623- // 立即关闭响应体,避免在循环中使用defer
624- if resp != nil && resp .Body != nil {
625- if closeErr := resp .Body .Close (); closeErr != nil {
626- logger .Warn ("关闭响应体失败" , "error" , closeErr )
627- }
628- }
629-
630- if err != nil {
631- return "" , err
632- }
633-
634- // 检查响应状态码并处理错误
635- if resp .StatusCode != http .StatusOK {
636- return "" , s .handleAPIError (resp .StatusCode , responseBody , requestDuration )
637- }
638-
639- // 记录原始响应内容(仅记录预览,避免日志过大)
640- responsePreview := string (responseBody )
641- if len (responsePreview ) > 200 {
642- responsePreview = responsePreview [:200 ] + "..."
643- }
644- logger .Debug ("Deepseek API原始响应" , "response_length" , len (responseBody ), "response_preview" , responsePreview )
645-
646- // 处理响应
647- content , err := s .processAPIResponse (responseBody , responsePreview )
648- if err != nil {
649- return "" , err
650- }
651-
652- logger .Info ("成功获取Deepseek API响应" , "content_length" , len (content ), "duration_ms" , requestDuration .Milliseconds ())
653- logger .Debug ("API响应内容" , "content" , content )
654-
655- return content , nil
656- }
657-
658- // 所有重试都失败
659- if lastErr != nil {
660- logger .Error ("发送API请求失败,已达到最大重试次数" , "error" , lastErr , "max_retries" , maxRetries )
661- return "" , fmt .Errorf ("发送API请求失败: %w" , lastErr )
662- }
663-
664- return "" , nil
665- }
666-
667588// isRetryableError 判断错误是否可重试
668589func (s * rssProcessorService ) isRetryableError (err error ) bool {
669590 if err == nil {
0 commit comments