From 049f9cca4fd79df8e8c9a95e7dbb660dfb2cd360 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 1 Jul 2022 16:44:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96body=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rpc.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/rpc.go b/rpc.go index efd5e87..8ff7ba1 100644 --- a/rpc.go +++ b/rpc.go @@ -16,7 +16,6 @@ import ( "net/http" "strings" "sync" - "time" "github.com/tidwall/gjson" @@ -236,7 +235,13 @@ func (r *request) Send(ctx *gin.Context, serviceFlag string, apiFlag string, par // 完整的请求地址 fullURL, body = r.getFullURLAndBody(serviceConfig, apiConfig, parameter) - if response, err = client.Do(apiConfig.Method, fullURL, apiConfig.Header, bytes.NewReader(body)); nil != err { + // 获取客户端 + client = r.GetHttpClient(apiConfig.Header, apiConfig.Timeout) + var bodyReader io.Reader + if nil != body { + bodyReader = bytes.NewReader(body) + } + if response, err = client.Do(apiConfig.Method, fullURL, apiConfig.Header, bodyReader); nil != err { return err } @@ -270,8 +275,8 @@ func (r *request) GetHttpClient(header map[string]string, timeout ApiTimeout) *h timeout.Read = DefaultReadTimeout } client.WithHeaders(header) - client.WithOption(httpclient.OPT_CONNECTTIMEOUT_MS, time.Duration(timeout.Connect)*time.Millisecond) - client.WithOption(httpclient.OPT_TIMEOUT_MS, time.Duration(timeout.Read)*time.Millisecond) + client.WithOption(httpclient.OPT_CONNECTTIMEOUT_MS, timeout.Connect) + client.WithOption(httpclient.OPT_TIMEOUT_MS, timeout.Read) return client } @@ -311,9 +316,6 @@ func (r *request) getFullURLAndBody(serviceConfig *Service, apiConfig *Api, para case http.MethodDelete: body, _ = json.Marshal(parameter) } - if nil == body { - body = []byte("{}") - } query := strings.Join(parameterPair, "&") if len(query) == 0 { return fullURL, body