This commit is contained in:
2025-11-02 20:33:28 +08:00
parent bb88c78c82
commit 2773c1a60b
13 changed files with 521 additions and 132 deletions

View File

@@ -31,7 +31,7 @@ func NewBaiduOCRService(apiKey, secretKey string, logger *zap.Logger) *BaiduOCRS
apiKey: apiKey,
secretKey: secretKey,
endpoint: "https://aip.baidubce.com",
timeout: 30 * time.Second,
timeout: 60 * time.Second,
logger: logger,
}
}
@@ -258,6 +258,23 @@ func (s *BaiduOCRService) sendRequest(ctx context.Context, method, url string, b
// 发送请求
resp, err := client.Do(req)
if err != nil {
// 检查是否是超时错误
isTimeout := false
if ctx.Err() == context.DeadlineExceeded {
isTimeout = true
} else if netErr, ok := err.(interface{ Timeout() bool }); ok && netErr.Timeout() {
isTimeout = true
} else if errStr := err.Error();
errStr == "context deadline exceeded" ||
errStr == "timeout" ||
errStr == "Client.Timeout exceeded" ||
errStr == "net/http: request canceled" {
isTimeout = true
}
if isTimeout {
return nil, fmt.Errorf("API请求超时: %w", err)
}
return nil, fmt.Errorf("发送请求失败: %w", err)
}
defer resp.Body.Close()
@@ -443,7 +460,7 @@ func (s *BaiduOCRService) extractWords(result map[string]interface{}) []string {
func (s *BaiduOCRService) downloadImage(ctx context.Context, imageURL string) ([]byte, error) {
// 创建HTTP客户端
client := &http.Client{
Timeout: 30 * time.Second,
Timeout: 60 * time.Second,
}
// 创建请求