Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't close resp body in HttpPost helpers #40

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand All @@ -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=
Expand Down
18 changes: 10 additions & 8 deletions httputils/httphelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"sync"
"time"

"github.com/cenkalti/backoff"
"github.com/cenkalti/backoff/v4"
)

type IHttpClient interface {
Expand Down Expand Up @@ -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)
Expand All @@ -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 &&
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}

Expand Down
Loading