Skip to content

Commit

Permalink
Micro improvements on connection handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszraczylo committed Dec 13, 2023
1 parent a107ee9 commit 29afbd0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
10 changes: 7 additions & 3 deletions execute_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,13 @@ func (qe *QueryExecutor) executeQuery() ([]byte, error) {
qe.Logger.Debug("Error while executing http request", map[string]interface{}{"error": err.Error()})
return err
}
defer io.Copy(io.Discard, httpResponse.Body)
defer httpResponse.Body.Close()
defer func() {
_, 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 {
return fmt.Errorf("HTTP error - unacceptable status code: \"%s\" for \"%s\"", httpResponse.Status, httpRequest.URL)
Expand All @@ -63,7 +68,6 @@ func (qe *QueryExecutor) executeQuery() ([]byte, 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 nil
},
retry.OnRetry(func(n uint, err error) {
Expand Down
6 changes: 5 additions & 1 deletion http.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ import (

func (b *BaseClient) createHttpClient() (http_client *http.Client) {
httpTransport := &http.Transport{
MaxIdleConns: 10,
MaxIdleConns: 512,
MaxConnsPerHost: 512,
MaxIdleConnsPerHost: 512,
IdleConnTimeout: 15 * time.Second,
ResponseHeaderTimeout: 15 * time.Second,
DisableKeepAlives: false,
DisableCompression: false,
}

if strings.HasPrefix(b.endpoint, "http://") {
http_client = &http.Client{
Timeout: 15 * time.Second,
Transport: httpTransport,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
Expand Down

0 comments on commit 29afbd0

Please sign in to comment.