Files
common/ragflow/client.go

187 lines
5.7 KiB
Go
Raw Normal View History

2025-11-27 17:38:42 +08:00
package ragflow
import (
"bytes"
2025-11-27 17:38:42 +08:00
"context"
"io"
2025-12-10 09:50:54 +08:00
"net"
2025-11-27 17:38:42 +08:00
"net/http"
"net/url"
"strings"
2025-12-09 17:55:08 +08:00
"sync"
2025-12-09 09:20:44 +08:00
"time"
2025-11-27 17:38:42 +08:00
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/errors/gerror"
2025-12-03 09:59:40 +08:00
"github.com/gogf/gf/v2/frame/g"
2025-11-27 17:38:42 +08:00
)
// gclient 完全不能用!
// 1. New() 默认 ResponseHeaderTimeout=30s
// 2. Clone() 内部调用 New(),链式调用会重置 Transport
// 3. 必须用原生 http.Client
2025-12-03 09:59:40 +08:00
var (
2025-12-09 17:55:08 +08:00
// globalClient 全局 RAGFlow 客户端(单例,延迟初始化)
2025-12-03 09:59:40 +08:00
globalClient *Client
2025-12-09 17:55:08 +08:00
clientOnce sync.Once
2025-12-03 09:59:40 +08:00
)
2025-12-09 17:55:08 +08:00
// initClient 延迟初始化客户端
func initClient() {
clientOnce.Do(func() {
ctx := context.Background()
2025-11-27 17:38:42 +08:00
2025-12-09 17:55:08 +08:00
// 读取配置
baseURL, apiKey := loadConfig(ctx)
2025-11-27 18:03:01 +08:00
2025-12-09 17:55:08 +08:00
// 如果配置不完整,跳过初始化
if baseURL == "" || apiKey == "" {
g.Log().Warning(ctx, "⚠️ RAGFlow 配置未找到,请在项目 config.yml 中添加 ragflow.base_url 和 ragflow.api_key")
return
}
2025-12-03 09:59:40 +08:00
2025-12-10 09:50:54 +08:00
// 自定义 Transport增大连接池解决并发连接不足导致的超时
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
MaxIdleConns: 200, // 最大空闲连接数
MaxIdleConnsPerHost: 100, // 每个 host 最大空闲连接数(关键!默认只有 2
MaxConnsPerHost: 100, // 每个 host 最大连接数
IdleConnTimeout: 90 * time.Second, // 空闲连接超时
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
ResponseHeaderTimeout: 180 * time.Second, // 等待响应头超时(关键!)
}
// 使用原生 http.Clientgclient 完全不能用Clone() 内部调用 New() 会重置 Transport
httpClient := &http.Client{
Transport: transport,
Timeout: 0, // 不设置全局超时,由 context 控制
}
2025-12-03 09:59:40 +08:00
// 验证 Transport 设置
g.Log().Infof(ctx, "✅ Transport 配置: ResponseHeaderTimeout=%v, MaxIdleConnsPerHost=%d, DisableKeepAlives=%v",
transport.ResponseHeaderTimeout, transport.MaxIdleConnsPerHost, transport.DisableKeepAlives)
2025-12-10 09:50:54 +08:00
2025-12-09 17:55:08 +08:00
globalClient = &Client{
BaseURL: strings.TrimSuffix(baseURL, "/"),
APIKey: apiKey,
HTTPClient: httpClient,
}
2025-12-03 09:59:40 +08:00
2025-12-09 17:55:08 +08:00
g.Log().Infof(ctx, "✅ RAGFlow 全局客户端初始化成功: baseURL=%s", baseURL)
})
2025-12-03 09:59:40 +08:00
}
// loadConfig 从配置文件加载 RAGFlow 配置
func loadConfig(ctx context.Context) (baseURL, apiKey string) {
2025-12-03 10:09:00 +08:00
// 使用 GoFrame 全局配置(从项目的 config.yml 读取)
baseURL = g.Cfg().MustGet(ctx, "ragflow.base_url", "").String()
apiKey = g.Cfg().MustGet(ctx, "ragflow.api_key", "").String()
return
2025-12-03 09:59:40 +08:00
}
2025-12-09 17:55:08 +08:00
// GetGlobalClient 获取全局客户端(延迟初始化)
2025-12-03 09:59:40 +08:00
// 使用示例client := ragflow.GetGlobalClient()
func GetGlobalClient() *Client {
2025-12-09 17:55:08 +08:00
initClient()
2025-12-03 09:59:40 +08:00
return globalClient
}
// Client RAGFlow API 客户端
type Client struct {
BaseURL string
APIKey string
HTTPClient *http.Client // 原生 HTTP 客户端gclient 不能用)
2025-11-27 17:38:42 +08:00
}
// CommonResponse 通用响应结构
type CommonResponse struct {
2025-11-27 18:03:01 +08:00
Code int `json:"code"`
Message string `json:"message"`
2025-11-27 17:38:42 +08:00
Data interface{} `json:"data,omitempty"`
}
// IsSuccess 检查响应是否成功
func (r *CommonResponse) IsSuccess() bool {
return r.Code == 0
}
// request 发送 HTTP 请求
func (c *Client) request(ctx context.Context, method, path string, body interface{}, result interface{}) (err error) {
2025-11-27 17:38:42 +08:00
fullURL := c.BaseURL + path
2025-11-27 18:03:01 +08:00
var reqBody string
2025-11-27 17:38:42 +08:00
if body != nil {
jsonData, err := gjson.Encode(body)
2025-11-27 17:38:42 +08:00
if err != nil {
return gerror.Newf("marshal request body failed: %v", err)
2025-11-27 17:38:42 +08:00
}
reqBody = string(jsonData)
2025-11-27 17:38:42 +08:00
}
2025-11-27 18:03:01 +08:00
// 使用独立的 context 设置 300 秒超时RAGFlow 高并发时响应较慢)
reqCtx, cancel := context.WithTimeout(context.Background(), 300*time.Second)
2025-12-10 09:50:54 +08:00
defer cancel()
startTime := time.Now()
2025-12-10 09:50:54 +08:00
// 创建请求
req, err := http.NewRequestWithContext(reqCtx, method, fullURL, bytes.NewReader([]byte(reqBody)))
if err != nil {
return gerror.Newf("create request failed: %v", err)
2025-11-27 17:38:42 +08:00
}
2025-11-27 18:03:01 +08:00
// 设置请求头
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)
2025-11-27 17:38:42 +08:00
if err != nil {
g.Log().Errorf(ctx, "[RAGFlow HTTP] 请求失败(耗时 %v): method=%s, url=%s, error=%v", elapsed, method, fullURL, err)
2025-12-10 09:50:54 +08:00
return gerror.Newf("request failed: %v", err)
2025-11-27 17:38:42 +08:00
}
g.Log().Infof(ctx, "[RAGFlow HTTP] 收到响应(耗时 %v): status=%d, url=%s", elapsed, resp.StatusCode, fullURL)
defer resp.Body.Close()
2025-11-27 18:03:01 +08:00
// 读取响应
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return gerror.Newf("read response failed: %v", err)
}
2025-12-10 09:50:54 +08:00
// 打印响应详情
g.Log().Debugf(ctx, "[RAGFlow HTTP] 响应: status=%d, body=%s", resp.StatusCode, respBody)
2025-12-10 09:50:54 +08:00
2025-11-27 17:38:42 +08:00
if resp.StatusCode != http.StatusOK {
g.Log().Errorf(ctx, "[RAGFlow HTTP] 非200响应: status=%d, body=%s", resp.StatusCode, respBody)
return gerror.Newf("http status %d: %s", resp.StatusCode, respBody)
2025-11-27 17:38:42 +08:00
}
2025-11-27 18:03:01 +08:00
if err = gjson.DecodeTo(respBody, result); err != nil {
2025-12-10 09:50:54 +08:00
g.Log().Errorf(ctx, "[RAGFlow HTTP] 解析响应失败: body=%s, error=%v", string(respBody), err)
return gerror.Newf("unmarshal response failed: %v", err)
2025-11-27 17:38:42 +08:00
}
2025-11-27 18:03:01 +08:00
return
2025-11-27 17:38:42 +08:00
}
// buildQueryString 构建查询字符串
func buildQueryString(params map[string]interface{}) string {
if len(params) == 0 {
return ""
}
2025-11-27 18:03:01 +08:00
parts := make([]string, 0, len(params))
2025-11-27 17:38:42 +08:00
for k, v := range params {
parts = append(parts, url.QueryEscape(k)+"="+url.QueryEscape(g.NewVar(v).String()))
2025-11-27 17:38:42 +08:00
}
return strings.Join(parts, "&")
}