更改client, 更改为gclient方法,弃用链式调用方式
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -4,3 +4,5 @@ ragflow/agent文档.md
|
|||||||
ragflow/README_GLOBAL.md
|
ragflow/README_GLOBAL.md
|
||||||
redis/stream使用示例.md
|
redis/stream使用示例.md
|
||||||
ragflow/client_backup.go.bak
|
ragflow/client_backup.go.bak
|
||||||
|
ragflow/为什么不能使用gclient.md
|
||||||
|
ragflow/agent文档.md
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
package ragflow
|
package ragflow
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"io"
|
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
@@ -14,13 +12,9 @@ import (
|
|||||||
"github.com/gogf/gf/v2/encoding/gjson"
|
"github.com/gogf/gf/v2/encoding/gjson"
|
||||||
"github.com/gogf/gf/v2/errors/gerror"
|
"github.com/gogf/gf/v2/errors/gerror"
|
||||||
"github.com/gogf/gf/v2/frame/g"
|
"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 (
|
var (
|
||||||
// globalClient 全局 RAGFlow 客户端(单例,延迟初始化)
|
// globalClient 全局 RAGFlow 客户端(单例,延迟初始化)
|
||||||
globalClient *Client
|
globalClient *Client
|
||||||
@@ -41,7 +35,7 @@ func initClient() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 自定义 Transport,增大连接池(解决并发连接不足导致的超时)
|
// 自定义 Transport(增大连接池,设置超时)
|
||||||
transport := &http.Transport{
|
transport := &http.Transport{
|
||||||
Proxy: http.ProxyFromEnvironment,
|
Proxy: http.ProxyFromEnvironment,
|
||||||
DialContext: (&net.Dialer{
|
DialContext: (&net.Dialer{
|
||||||
@@ -49,23 +43,18 @@ func initClient() {
|
|||||||
KeepAlive: 30 * time.Second,
|
KeepAlive: 30 * time.Second,
|
||||||
}).DialContext,
|
}).DialContext,
|
||||||
MaxIdleConns: 200, // 最大空闲连接数
|
MaxIdleConns: 200, // 最大空闲连接数
|
||||||
MaxIdleConnsPerHost: 100, // 每个 host 最大空闲连接数(关键!默认只有 2)
|
MaxIdleConnsPerHost: 100, // 每个 host 最大空闲连接数
|
||||||
MaxConnsPerHost: 100, // 每个 host 最大连接数
|
MaxConnsPerHost: 100, // 每个 host 最大连接数
|
||||||
IdleConnTimeout: 90 * time.Second, // 空闲连接超时
|
IdleConnTimeout: 90 * time.Second, // 空闲连接超时
|
||||||
TLSHandshakeTimeout: 10 * time.Second,
|
TLSHandshakeTimeout: 10 * time.Second,
|
||||||
ExpectContinueTimeout: 1 * time.Second,
|
ExpectContinueTimeout: 1 * time.Second,
|
||||||
ResponseHeaderTimeout: 180 * time.Second, // 等待响应头超时(关键!)
|
ResponseHeaderTimeout: 180 * time.Second, // 等待响应头超时
|
||||||
}
|
}
|
||||||
|
|
||||||
// 使用原生 http.Client(gclient 完全不能用,Clone() 内部调用 New() 会重置 Transport)
|
// 初始化 gclient(不使用链式调用,避免 Transport 被重置)
|
||||||
httpClient := &http.Client{
|
httpClient := gclient.New()
|
||||||
Transport: transport,
|
httpClient.Client.Transport = transport
|
||||||
Timeout: 0, // 不设置全局超时,由 context 控制
|
httpClient.Client.Timeout = 180 * time.Second
|
||||||
}
|
|
||||||
|
|
||||||
// 验证 Transport 设置
|
|
||||||
g.Log().Infof(ctx, "✅ Transport 配置: ResponseHeaderTimeout=%v, MaxIdleConnsPerHost=%d, DisableKeepAlives=%v",
|
|
||||||
transport.ResponseHeaderTimeout, transport.MaxIdleConnsPerHost, transport.DisableKeepAlives)
|
|
||||||
|
|
||||||
globalClient = &Client{
|
globalClient = &Client{
|
||||||
BaseURL: strings.TrimSuffix(baseURL, "/"),
|
BaseURL: strings.TrimSuffix(baseURL, "/"),
|
||||||
@@ -73,7 +62,7 @@ func initClient() {
|
|||||||
HTTPClient: httpClient,
|
HTTPClient: httpClient,
|
||||||
}
|
}
|
||||||
|
|
||||||
g.Log().Infof(ctx, "✅ RAGFlow 全局客户端初始化成功: baseURL=%s", baseURL)
|
g.Log().Infof(ctx, "✅ RAGFlow 客户端初始化成功: baseURL=%s, timeout=180s", baseURL)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,7 +85,7 @@ func GetGlobalClient() *Client {
|
|||||||
type Client struct {
|
type Client struct {
|
||||||
BaseURL string
|
BaseURL string
|
||||||
APIKey string
|
APIKey string
|
||||||
HTTPClient *http.Client // 原生 HTTP 客户端(gclient 不能用)
|
HTTPClient *gclient.Client // HTTP 客户端
|
||||||
}
|
}
|
||||||
|
|
||||||
// CommonResponse 通用响应结构
|
// CommonResponse 通用响应结构
|
||||||
@@ -111,61 +100,51 @@ func (r *CommonResponse) IsSuccess() bool {
|
|||||||
return r.Code == 0
|
return r.Code == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// request 发送 HTTP 请求
|
// request 发送 HTTP 请求(不使用链式调用)
|
||||||
func (c *Client) request(ctx context.Context, method, path string, body interface{}, result interface{}) (err error) {
|
func (c *Client) request(ctx context.Context, method, path string, body interface{}, result interface{}) (err error) {
|
||||||
fullURL := c.BaseURL + path
|
fullURL := c.BaseURL + path
|
||||||
|
|
||||||
|
// 序列化请求体
|
||||||
var reqBody string
|
var reqBody string
|
||||||
if body != nil {
|
if body != nil {
|
||||||
jsonData, err := gjson.Encode(body)
|
jsonData, jsonErr := gjson.Encode(body)
|
||||||
if err != nil {
|
if jsonErr != nil {
|
||||||
return gerror.Newf("marshal request body failed: %v", err)
|
return gerror.Newf("marshal request body failed: %v", jsonErr)
|
||||||
}
|
}
|
||||||
reqBody = string(jsonData)
|
reqBody = string(jsonData)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 使用独立的 context 设置 300 秒超时(RAGFlow 高并发时响应较慢)
|
|
||||||
reqCtx, cancel := context.WithTimeout(context.Background(), 300*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
startTime := time.Now()
|
|
||||||
|
|
||||||
// 创建请求
|
|
||||||
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)
|
c.HTTPClient.SetHeader("Authorization", "Bearer "+c.APIKey)
|
||||||
req.Header.Set("Content-Type", "application/json")
|
c.HTTPClient.SetHeader("Content-Type", "application/json")
|
||||||
|
|
||||||
// 发送请求
|
// 发送请求
|
||||||
g.Log().Infof(ctx, "[RAGFlow HTTP] 发送请求: method=%s, url=%s", method, fullURL)
|
var resp *gclient.Response
|
||||||
resp, err := c.HTTPClient.Do(req)
|
switch method {
|
||||||
elapsed := time.Since(startTime)
|
case "GET":
|
||||||
|
resp, err = c.HTTPClient.Get(ctx, fullURL)
|
||||||
|
case "POST":
|
||||||
|
resp, err = c.HTTPClient.Post(ctx, fullURL, reqBody)
|
||||||
|
case "PUT":
|
||||||
|
resp, err = c.HTTPClient.Put(ctx, fullURL, reqBody)
|
||||||
|
case "DELETE":
|
||||||
|
resp, err = c.HTTPClient.Delete(ctx, fullURL, reqBody)
|
||||||
|
default:
|
||||||
|
return gerror.Newf("unsupported method: %s", method)
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
g.Log().Errorf(ctx, "[RAGFlow HTTP] 请求失败(耗时 %v): method=%s, url=%s, error=%v", elapsed, method, fullURL, err)
|
|
||||||
return gerror.Newf("request failed: %v", err)
|
return gerror.Newf("request failed: %v", err)
|
||||||
}
|
}
|
||||||
g.Log().Infof(ctx, "[RAGFlow HTTP] 收到响应(耗时 %v): status=%d, url=%s", elapsed, resp.StatusCode, fullURL)
|
defer resp.Close()
|
||||||
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, respBody)
|
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
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, string(respBody))
|
||||||
return gerror.Newf("http status %d: %s", resp.StatusCode, respBody)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = gjson.DecodeTo(respBody, result); err != nil {
|
if err = gjson.DecodeTo(respBody, result); err != nil {
|
||||||
g.Log().Errorf(ctx, "[RAGFlow HTTP] 解析响应失败: body=%s, error=%v", string(respBody), err)
|
|
||||||
return gerror.Newf("unmarshal response failed: %v", err)
|
return gerror.Newf("unmarshal response failed: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user