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

@@ -158,15 +158,33 @@ func (x *XingweiService) CallAPI(ctx context.Context, projectID string, params m
req.Header.Set("API-ID", x.config.ApiID)
req.Header.Set("project_id", projectID)
// 创建HTTP客户端
// 创建HTTP客户端超时时间设置为60秒
client := &http.Client{
Timeout: 20 * time.Second,
Timeout: 60 * time.Second,
}
// 发送请求
httpResp, clientDoErr := client.Do(req)
if clientDoErr != nil {
err = errors.Join(ErrSystem, clientDoErr)
// 检查是否是超时错误
isTimeout := false
if ctx.Err() == context.DeadlineExceeded {
isTimeout = true
} else if netErr, ok := clientDoErr.(interface{ Timeout() bool }); ok && netErr.Timeout() {
isTimeout = true
} else if errStr := clientDoErr.Error();
errStr == "context deadline exceeded" ||
errStr == "timeout" ||
errStr == "Client.Timeout exceeded" ||
errStr == "net/http: request canceled" {
isTimeout = true
}
if isTimeout {
err = errors.Join(ErrDatasource, fmt.Errorf("API请求超时: %v", clientDoErr))
} else {
err = errors.Join(ErrSystem, clientDoErr)
}
if x.logger != nil {
x.logger.LogError(requestID, transactionID, "xingwei_api", err, params)
}