Compare commits

..
2 Commits
2 changed files with 27 additions and 5 deletions
+22 -4
View File
@@ -2,6 +2,7 @@ package gql
import ( import (
"bytes" "bytes"
"compress/gzip"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
@@ -45,14 +46,32 @@ func (qe *QueryExecutor) executeQuery() ([]byte, error) {
qe.Logger.Debug("Error while executing http request", map[string]interface{}{"error": err.Error()}) qe.Logger.Debug("Error while executing http request", map[string]interface{}{"error": err.Error()})
return err return err
} }
defer io.Copy(io.Discard, httpResponse.Body) defer func() {
defer httpResponse.Body.Close() _, err := io.Copy(io.Discard, httpResponse.Body)
if err != nil {
qe.Logger.Debug("Error while discarding http response body;", map[string]interface{}{"error": err.Error()})
}
httpResponse.Body.Close()
}()
if httpResponse.StatusCode < 200 || httpResponse.StatusCode >= 204 { if httpResponse.StatusCode < 200 || httpResponse.StatusCode >= 204 {
return fmt.Errorf("HTTP error - unacceptable status code: \"%s\" for \"%s\"", httpResponse.Status, httpRequest.URL) return fmt.Errorf("HTTP error - unacceptable status code: \"%s\" for \"%s\"", httpResponse.Status, httpRequest.URL)
} }
body, err := io.ReadAll(httpResponse.Body) var reader io.ReadCloser
encoding := httpResponse.Header.Get("Content-Encoding")
if encoding == "gzip" {
reader, err = gzip.NewReader(httpResponse.Body)
if err != nil {
qe.Logger.Debug("Error while creating gzip reader;", map[string]interface{}{"error": err.Error()})
return fmt.Errorf("Error while creating gzip reader: %s", err.Error())
}
defer reader.Close()
} else {
reader = httpResponse.Body
}
body, err := io.ReadAll(reader)
if err != nil { if err != nil {
qe.Logger.Debug("Error while reading http response;", map[string]interface{}{"error": err.Error()}) qe.Logger.Debug("Error while reading http response;", map[string]interface{}{"error": err.Error()})
return fmt.Errorf("Error while reading http response: %s", err.Error()) return fmt.Errorf("Error while reading http response: %s", err.Error())
@@ -63,7 +82,6 @@ func (qe *QueryExecutor) executeQuery() ([]byte, error) {
qe.Logger.Debug("Error while unmarshalling http response;", map[string]interface{}{"error": err.Error()}) qe.Logger.Debug("Error while unmarshalling http response;", map[string]interface{}{"error": err.Error()})
return fmt.Errorf("Error while unmarshalling http response: %s", err.Error()) return fmt.Errorf("Error while unmarshalling http response: %s", err.Error())
} }
return nil return nil
}, },
retry.OnRetry(func(n uint, err error) { retry.OnRetry(func(n uint, err error) {
+5 -1
View File
@@ -11,14 +11,18 @@ import (
func (b *BaseClient) createHttpClient() (http_client *http.Client) { func (b *BaseClient) createHttpClient() (http_client *http.Client) {
httpTransport := &http.Transport{ httpTransport := &http.Transport{
MaxIdleConns: 10, MaxIdleConns: 512,
MaxConnsPerHost: 512,
MaxIdleConnsPerHost: 512,
IdleConnTimeout: 15 * time.Second, IdleConnTimeout: 15 * time.Second,
ResponseHeaderTimeout: 15 * time.Second, ResponseHeaderTimeout: 15 * time.Second,
DisableKeepAlives: false, DisableKeepAlives: false,
DisableCompression: false,
} }
if strings.HasPrefix(b.endpoint, "http://") { if strings.HasPrefix(b.endpoint, "http://") {
http_client = &http.Client{ http_client = &http.Client{
Timeout: 15 * time.Second,
Transport: httpTransport, Transport: httpTransport,
CheckRedirect: func(req *http.Request, via []*http.Request) error { CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse return http.ErrUseLastResponse