增加es归档 分布式和constants变量
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
package ragflow
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@@ -12,9 +14,13 @@ import (
|
||||
"github.com/gogf/gf/v2/encoding/gjson"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/gclient"
|
||||
)
|
||||
|
||||
// gclient 完全不能用!
|
||||
// 1. New() 默认 ResponseHeaderTimeout=30s
|
||||
// 2. Clone() 内部调用 New(),链式调用会重置 Transport
|
||||
// 3. 必须用原生 http.Client
|
||||
|
||||
var (
|
||||
// globalClient 全局 RAGFlow 客户端(单例,延迟初始化)
|
||||
globalClient *Client
|
||||
@@ -51,15 +57,15 @@ func initClient() {
|
||||
ResponseHeaderTimeout: 180 * time.Second, // 等待响应头超时(关键!)
|
||||
}
|
||||
|
||||
// 初始化全局客户端
|
||||
httpClient := gclient.New()
|
||||
httpClient.SetBrowserMode(false)
|
||||
httpClient.SetHeader("Authorization", "Bearer "+apiKey)
|
||||
httpClient.SetHeader("Content-Type", "application/json")
|
||||
httpClient.SetTimeout(180 * time.Second) // RAGFlow AI 推理需要较长时间
|
||||
// 使用原生 http.Client(gclient 完全不能用,Clone() 内部调用 New() 会重置 Transport)
|
||||
httpClient := &http.Client{
|
||||
Transport: transport,
|
||||
Timeout: 0, // 不设置全局超时,由 context 控制
|
||||
}
|
||||
|
||||
// 设置自定义 Transport
|
||||
httpClient.Client.Transport = transport
|
||||
// 验证 Transport 设置
|
||||
g.Log().Infof(ctx, "✅ Transport 配置: ResponseHeaderTimeout=%v, MaxIdleConnsPerHost=%d, DisableKeepAlives=%v",
|
||||
transport.ResponseHeaderTimeout, transport.MaxIdleConnsPerHost, transport.DisableKeepAlives)
|
||||
|
||||
globalClient = &Client{
|
||||
BaseURL: strings.TrimSuffix(baseURL, "/"),
|
||||
@@ -90,7 +96,7 @@ func GetGlobalClient() *Client {
|
||||
type Client struct {
|
||||
BaseURL string
|
||||
APIKey string
|
||||
HTTPClient *gclient.Client // HTTP 客户端
|
||||
HTTPClient *http.Client // 原生 HTTP 客户端(gclient 不能用)
|
||||
}
|
||||
|
||||
// CommonResponse 通用响应结构
|
||||
@@ -118,39 +124,44 @@ func (c *Client) request(ctx context.Context, method, path string, body interfac
|
||||
reqBody = string(jsonData)
|
||||
}
|
||||
|
||||
// 设置 180 秒超时(RAGFlow AI 推理需要较长时间)
|
||||
reqCtx, cancel := context.WithTimeout(ctx, 180*time.Second)
|
||||
// 使用独立的 context 设置 300 秒超时(RAGFlow 高并发时响应较慢)
|
||||
reqCtx, cancel := context.WithTimeout(context.Background(), 300*time.Second)
|
||||
defer cancel()
|
||||
startTime := time.Now()
|
||||
|
||||
var resp *gclient.Response
|
||||
|
||||
switch method {
|
||||
case "GET":
|
||||
resp, err = c.HTTPClient.Get(reqCtx, fullURL)
|
||||
case "POST":
|
||||
resp, err = c.HTTPClient.Post(reqCtx, fullURL, reqBody)
|
||||
case "PUT":
|
||||
resp, err = c.HTTPClient.Put(reqCtx, fullURL, reqBody)
|
||||
case "DELETE":
|
||||
resp, err = c.HTTPClient.Delete(reqCtx, fullURL, reqBody)
|
||||
default:
|
||||
return gerror.Newf("unsupported method: %s", method)
|
||||
// 创建请求
|
||||
req, err := http.NewRequestWithContext(reqCtx, method, fullURL, bytes.NewReader([]byte(reqBody)))
|
||||
if err != nil {
|
||||
return gerror.Newf("create request failed: %v", err)
|
||||
}
|
||||
|
||||
// 设置请求头
|
||||
req.Header.Set("Authorization", "Bearer "+c.APIKey)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
// 发送请求
|
||||
g.Log().Infof(ctx, "[RAGFlow HTTP] 发送请求: method=%s, url=%s", method, fullURL)
|
||||
resp, err := c.HTTPClient.Do(req)
|
||||
elapsed := time.Since(startTime)
|
||||
if err != nil {
|
||||
g.Log().Errorf(ctx, "[RAGFlow HTTP] 请求失败: method=%s, url=%s, error=%v", method, fullURL, err)
|
||||
g.Log().Errorf(ctx, "[RAGFlow HTTP] 请求失败(耗时 %v): method=%s, url=%s, error=%v", elapsed, method, fullURL, err)
|
||||
return gerror.Newf("request failed: %v", err)
|
||||
}
|
||||
defer resp.Close()
|
||||
g.Log().Infof(ctx, "[RAGFlow HTTP] 收到响应(耗时 %v): status=%d, url=%s", elapsed, resp.StatusCode, fullURL)
|
||||
defer resp.Body.Close()
|
||||
|
||||
respBody := resp.ReadAll()
|
||||
// 读取响应
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return gerror.Newf("read response failed: %v", err)
|
||||
}
|
||||
|
||||
// 打印响应详情
|
||||
g.Log().Debugf(ctx, "[RAGFlow HTTP] 响应: status=%d, body=%s", resp.StatusCode, string(respBody))
|
||||
g.Log().Debugf(ctx, "[RAGFlow HTTP] 响应: status=%d, body=%s", resp.StatusCode, respBody)
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
g.Log().Errorf(ctx, "[RAGFlow HTTP] 非200响应: status=%d, body=%s", resp.StatusCode, string(respBody))
|
||||
return gerror.Newf("http status %d: %s", resp.StatusCode, string(respBody))
|
||||
g.Log().Errorf(ctx, "[RAGFlow HTTP] 非200响应: status=%d, body=%s", resp.StatusCode, respBody)
|
||||
return gerror.Newf("http status %d: %s", resp.StatusCode, respBody)
|
||||
}
|
||||
|
||||
if err = gjson.DecodeTo(respBody, result); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user