复用http.go里面的 g.client(),更新config.yml的注册方式, 更新jaeger链路追踪
This commit is contained in:
@@ -2,13 +2,13 @@ package ragflow
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
commonHttp "gitee.com/red-future---jilin-g/common/http"
|
||||
"github.com/gogf/gf/v2/encoding/gjson"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
@@ -35,34 +35,13 @@ func initClient() {
|
||||
return
|
||||
}
|
||||
|
||||
// 自定义 Transport(增大连接池,设置超时)
|
||||
transport := &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
DialContext: (&net.Dialer{
|
||||
Timeout: 30 * time.Second,
|
||||
KeepAlive: 30 * time.Second,
|
||||
}).DialContext,
|
||||
MaxIdleConns: 200, // 最大空闲连接数
|
||||
MaxIdleConnsPerHost: 100, // 每个 host 最大空闲连接数
|
||||
MaxConnsPerHost: 100, // 每个 host 最大连接数
|
||||
IdleConnTimeout: 90 * time.Second, // 空闲连接超时
|
||||
TLSHandshakeTimeout: 10 * time.Second,
|
||||
ExpectContinueTimeout: 1 * time.Second,
|
||||
ResponseHeaderTimeout: 180 * time.Second, // 等待响应头超时
|
||||
}
|
||||
|
||||
// 初始化 gclient(不使用链式调用,避免 Transport 被重置)
|
||||
httpClient := gclient.New()
|
||||
httpClient.Client.Transport = transport
|
||||
httpClient.Client.Timeout = 180 * time.Second
|
||||
|
||||
globalClient = &Client{
|
||||
BaseURL: strings.TrimSuffix(baseURL, "/"),
|
||||
APIKey: apiKey,
|
||||
HTTPClient: httpClient,
|
||||
HTTPClient: commonHttp.Httpclient,
|
||||
}
|
||||
|
||||
g.Log().Infof(ctx, "✅ RAGFlow 客户端初始化成功: baseURL=%s, timeout=180s", baseURL)
|
||||
g.Log().Infof(ctx, "✅ RAGFlow 客户端初始化成功: baseURL=%s", baseURL)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -100,7 +79,7 @@ func (r *CommonResponse) IsSuccess() bool {
|
||||
return r.Code == 0
|
||||
}
|
||||
|
||||
// request 发送 HTTP 请求(不使用链式调用)
|
||||
// request 发送 HTTP 请求
|
||||
func (c *Client) request(ctx context.Context, method, path string, body interface{}, result interface{}) (err error) {
|
||||
fullURL := c.BaseURL + path
|
||||
|
||||
@@ -114,21 +93,24 @@ func (c *Client) request(ctx context.Context, method, path string, body interfac
|
||||
reqBody = string(jsonData)
|
||||
}
|
||||
|
||||
// 设置请求头
|
||||
c.HTTPClient.SetHeader("Authorization", "Bearer "+c.APIKey)
|
||||
c.HTTPClient.SetHeader("Content-Type", "application/json")
|
||||
// 设置请求头和超时
|
||||
// 注意:使用 Chain 模式,避免修改全局 Httpclient
|
||||
client := c.HTTPClient.Timeout(180 * time.Second).Header(map[string]string{
|
||||
"Authorization": "Bearer " + c.APIKey,
|
||||
"Content-Type": "application/json",
|
||||
})
|
||||
|
||||
// 发送请求
|
||||
var resp *gclient.Response
|
||||
switch method {
|
||||
case "GET":
|
||||
resp, err = c.HTTPClient.Get(ctx, fullURL)
|
||||
resp, err = client.Get(ctx, fullURL)
|
||||
case "POST":
|
||||
resp, err = c.HTTPClient.Post(ctx, fullURL, reqBody)
|
||||
resp, err = client.Post(ctx, fullURL, reqBody)
|
||||
case "PUT":
|
||||
resp, err = c.HTTPClient.Put(ctx, fullURL, reqBody)
|
||||
resp, err = client.Put(ctx, fullURL, reqBody)
|
||||
case "DELETE":
|
||||
resp, err = c.HTTPClient.Delete(ctx, fullURL, reqBody)
|
||||
resp, err = client.Delete(ctx, fullURL, reqBody)
|
||||
default:
|
||||
return gerror.Newf("unsupported method: %s", method)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user