From 29bef5e129087e30bbde8b56cb59d48445e38e9e Mon Sep 17 00:00:00 2001 From: Medcl Date: Mon, 30 Dec 2024 10:33:23 +0800 Subject: [PATCH] chore: enhanced http_client to support customizable configurations. (#61) * chore: update http client * chore: update release notes --- docs/content.en/docs/release-notes/_index.md | 3 ++- modules/elastic/api/proxy.go | 26 +------------------- modules/elastic/api/test_connection.go | 4 +-- plugin/managed/server/manager.go | 2 +- 4 files changed, 6 insertions(+), 29 deletions(-) diff --git a/docs/content.en/docs/release-notes/_index.md b/docs/content.en/docs/release-notes/_index.md index ab68e2cd..1256e26e 100644 --- a/docs/content.en/docs/release-notes/_index.md +++ b/docs/content.en/docs/release-notes/_index.md @@ -23,7 +23,8 @@ Information about release notes of INFINI Console is provided here. - Adapter metrics query with cluster id and cluster uuid - Optimize metric query bucket size (#59) - Add suggestion to chart in monitor if is no data because the time interval is less than the collection interval. -- check if the cluster version supports metric transport_outbound_comnections in monitor. +- Check if the cluster version supports metric transport_outbound_comnections in monitor. +- Enhanced http_client to support customizable configurations. ## 1.27.0 (2024-12-09) diff --git a/modules/elastic/api/proxy.go b/modules/elastic/api/proxy.go index 6de64c4b..a8b2c29f 100644 --- a/modules/elastic/api/proxy.go +++ b/modules/elastic/api/proxy.go @@ -26,7 +26,6 @@ package api import ( "bytes" "context" - "crypto/tls" "fmt" "github.com/buger/jsonparser" log "github.com/cihub/seelog" @@ -34,15 +33,12 @@ import ( "infini.sh/framework/core/api" httprouter "infini.sh/framework/core/api/router" "infini.sh/framework/core/elastic" - "infini.sh/framework/core/global" "infini.sh/framework/core/util" "infini.sh/framework/lib/fasthttp" "io" "net/http" "net/url" "strings" - "sync" - "time" ) var httpPool = fasthttp.NewRequestResponsePool("proxy_search") @@ -211,7 +207,7 @@ func (h *APIHandler) HandleProxyAction(w http.ResponseWriter, req *http.Request, } defer req.Body.Close() - err = getHttpClient().Do(freq, fres) + err = api.GetFastHttpClient("elasticsearch_proxy").Do(freq, fres) if err != nil { resBody["error"] = err.Error() h.WriteJSON(w, resBody, http.StatusInternalServerError) @@ -283,24 +279,4 @@ func rewriteTableNamesOfSqlRequest(req *http.Request, distribution string) (stri return strings.Join(unescapedTableNames, ","), nil } -var ( - client *fasthttp.Client - clientOnce sync.Once -) -func getHttpClient() *fasthttp.Client { - clientOnce.Do(func() { - clientCfg := global.Env().SystemConfig.HTTPClientConfig - client = &fasthttp.Client{ - MaxConnsPerHost: clientCfg.MaxConnectionPerHost, - TLSConfig: &tls.Config{InsecureSkipVerify: clientCfg.TLSConfig.TLSInsecureSkipVerify}, - ReadTimeout: util.GetDurationOrDefault(clientCfg.ReadTimeout, 60*time.Second), - WriteTimeout: util.GetDurationOrDefault(clientCfg.ReadTimeout, 60*time.Second), - DialDualStack: true, - ReadBufferSize: clientCfg.ReadBufferSize, - WriteBufferSize: clientCfg.WriteBufferSize, - //Dial: fasthttpproxy.FasthttpProxyHTTPDialerTimeout(time.Second * 2), - } - }) - return client -} diff --git a/modules/elastic/api/test_connection.go b/modules/elastic/api/test_connection.go index a1455342..2778445b 100644 --- a/modules/elastic/api/test_connection.go +++ b/modules/elastic/api/test_connection.go @@ -117,7 +117,7 @@ func (h TestAPI) HandleTestConnectionAction(w http.ResponseWriter, req *http.Req freq.SetBasicAuth(config.BasicAuth.Username, config.BasicAuth.Password.Get()) } - err = getHttpClient().DoTimeout(freq, fres, 10*time.Second) + err = api.GetFastHttpClient("elasticsearch_test_connection").DoTimeout(freq, fres, 10*time.Second) if err != nil { panic(err) @@ -145,7 +145,7 @@ func (h TestAPI) HandleTestConnectionAction(w http.ResponseWriter, req *http.Req //fetch cluster health info freq.SetRequestURI(fmt.Sprintf("%s/_cluster/health", config.Endpoint)) fres.Reset() - err = getHttpClient().Do(freq, fres) + err = api.GetFastHttpClient().Do(freq, fres) if err != nil { resBody["error"] = fmt.Sprintf("error on get cluster health: %v", err) h.WriteJSON(w, resBody, http.StatusInternalServerError) diff --git a/plugin/managed/server/manager.go b/plugin/managed/server/manager.go index 986a6633..d8aaaf73 100644 --- a/plugin/managed/server/manager.go +++ b/plugin/managed/server/manager.go @@ -96,7 +96,7 @@ func ProxyAgentRequest(tag, endpoint string, req *util.Request, responseObjectTo var res *util.Result initOnce.Do(func() { - cfg := global.Env().GetClientConfigByEndpoint(tag, endpoint) + cfg := global.Env().GetHTTPClientConfig(tag, endpoint) if cfg != nil { hClient, err := api.NewHTTPClient(cfg) if err != nil {