diff --git a/gttp/http.go b/gttp/http.go index 1446697..473655a 100644 --- a/gttp/http.go +++ b/gttp/http.go @@ -3,6 +3,7 @@ package gttp import ( "bytes" "context" + "crypto/tls" "io" "net/http" "time" @@ -19,6 +20,23 @@ type Response struct { Body []byte } +// getClient ... +func getClient(timeout ...time.Duration) *http.Client { + client := &http.Client{ + Timeout: getTimeout(timeout...), + Transport: &http.Transport{ + MaxIdleConns: 100, + MaxIdleConnsPerHost: 100, + IdleConnTimeout: time.Second * 90, + TLSClientConfig: &tls.Config{ + InsecureSkipVerify: true, + }, + }, + } + + return client +} + func getTimeout(timeout ...time.Duration) time.Duration { if len(timeout) == 0 { return 10 * time.Second @@ -34,7 +52,6 @@ func Post(url string, headers M, body any, timeout ...time.Duration) (res *Respo return } - req.Close = true for k, v := range headers { req.Header.Set(k, v) } @@ -42,20 +59,19 @@ func Post(url string, headers M, body any, timeout ...time.Duration) (res *Respo ctx, cancel := context.WithTimeout(context.Background(), getTimeout(timeout...)) defer cancel() - client := &http.Client{} - response, err := client.Do(req.WithContext(ctx)) + reqDo, err := getClient(timeout...).Do(req.WithContext(ctx)) if err != nil { return } - defer response.Body.Close() + defer reqDo.Body.Close() - bodyBytes, err := io.ReadAll(response.Body) + bodyBytes, err := io.ReadAll(reqDo.Body) if err != nil { return } - return &Response{response.StatusCode, bodyBytes}, nil + return &Response{reqDo.StatusCode, bodyBytes}, nil } // Get sends a GET request to the specified URL. @@ -65,7 +81,6 @@ func Get(url string, headers M, timeout ...time.Duration) (res *Response, err er return } - req.Close = true for k, v := range headers { req.Header.Set(k, v) } @@ -73,20 +88,19 @@ func Get(url string, headers M, timeout ...time.Duration) (res *Response, err er ctx, cancel := context.WithTimeout(context.Background(), getTimeout(timeout...)) defer cancel() - client := &http.Client{} - response, err := client.Do(req.WithContext(ctx)) + reqDo, err := getClient(timeout...).Do(req.WithContext(ctx)) if err != nil { return } - defer response.Body.Close() + defer reqDo.Body.Close() - bodyBytes, err := io.ReadAll(response.Body) + bodyBytes, err := io.ReadAll(reqDo.Body) if err != nil { return } - return &Response{response.StatusCode, bodyBytes}, nil + return &Response{reqDo.StatusCode, bodyBytes}, nil } // Custom sends a custom request to the specified URL. @@ -96,7 +110,6 @@ func Custom(url, method string, headers M, body any, timeout ...time.Duration) ( return } - req.Close = true for k, v := range headers { req.Header.Set(k, v) } @@ -104,18 +117,17 @@ func Custom(url, method string, headers M, body any, timeout ...time.Duration) ( ctx, cancel := context.WithTimeout(context.Background(), getTimeout(timeout...)) defer cancel() - client := &http.Client{} - response, err := client.Do(req.WithContext(ctx)) + reqDo, err := getClient(timeout...).Do(req.WithContext(ctx)) if err != nil { return } - defer response.Body.Close() + defer reqDo.Body.Close() - bodyBytes, err := io.ReadAll(response.Body) + bodyBytes, err := io.ReadAll(reqDo.Body) if err != nil { return } - return &Response{response.StatusCode, bodyBytes}, nil + return &Response{reqDo.StatusCode, bodyBytes}, nil }