From 9e4af7404c7a622550f8b2588bf589ab7b73cd24 Mon Sep 17 00:00:00 2001 From: Matthias Bertschy Date: Fri, 1 Nov 2024 08:12:40 +0100 Subject: [PATCH] don't close resp body in HttpPost helpers Signed-off-by: Matthias Bertschy --- go.mod | 4 ++-- go.sum | 8 ++++---- httputils/httphelpers.go | 18 ++++++++++-------- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/go.mod b/go.mod index 3158704..88ae8b0 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,8 @@ module github.com/armosec/utils-go go 1.21 require ( - github.com/cenkalti/backoff v2.2.1+incompatible - github.com/stretchr/testify v1.8.4 + github.com/cenkalti/backoff/v4 v4.3.0 + github.com/stretchr/testify v1.9.0 ) require ( diff --git a/go.sum b/go.sum index 2059d19..75ef5d4 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4= -github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= +github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8= +github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -16,8 +16,8 @@ github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= -github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= diff --git a/httputils/httphelpers.go b/httputils/httphelpers.go index f5dcc61..85e472b 100644 --- a/httputils/httphelpers.go +++ b/httputils/httphelpers.go @@ -11,7 +11,7 @@ import ( "sync" "time" - "github.com/cenkalti/backoff" + "github.com/cenkalti/backoff/v4" ) type IHttpClient interface { @@ -94,11 +94,12 @@ func HttpPostWithContext(ctx context.Context, httpClient IHttpClient, fullURL st if err != nil { return err } - defer resp.Body.Close() // If the status code is not 200, we will retry if resp.StatusCode != http.StatusOK { if shouldRetry(resp) { + // only close the body if we are going to retry + _ = resp.Body.Close() return fmt.Errorf("received status code: %d", resp.StatusCode) } return backoff.Permanent(err) @@ -118,6 +119,7 @@ func HttpPostWithContext(ctx context.Context, httpClient IHttpClient, fullURL st return resp, nil } + func defaultShouldRetry(resp *http.Response) bool { // If received codes 401/403/404/500 should return false return resp.StatusCode != http.StatusUnauthorized && @@ -177,9 +179,9 @@ func Split2Chunks[T any](maxNumOfChunks int, slice []T) [][]T { } for i := 0; i < maxNumOfChunks; i++ { - min := (i * len(slice) / maxNumOfChunks) - max := ((i + 1) * len(slice)) / maxNumOfChunks - divided = append(divided, slice[min:max]) + myMin := i * len(slice) / maxNumOfChunks + myMax := ((i + 1) * len(slice)) / maxNumOfChunks + divided = append(divided, slice[myMin:myMax]) } return divided } @@ -227,16 +229,16 @@ func splitSlice2Chunks[T any](slice []T, maxSize int, chunks chan<- []T, wg *syn //slice is bigger than max size //split the slice to slices smaller than max size index := 0 - for i, _ := range slice { + for i := range slice { jsonSize = JSONSize(slice[index : i+1]) if jsonSize > maxSize { //send the part of the slice that is smaller than max size - splitSlice2Chunks(slice[index:i], maxSize, chunks, wg) + splitSlice2Chunks[T](slice[index:i], maxSize, chunks, wg) index = i } } //send the last part of the slice - splitSlice2Chunks(slice[index:], maxSize, chunks, wg) + splitSlice2Chunks[T](slice[index:], maxSize, chunks, wg) }(slice, maxSize, chunks, wg) }