Skip to content

Commit

Permalink
Fix bugs in common package
Browse files Browse the repository at this point in the history
Signed-off-by: Harsh Thakur <harshthakur9030@gmail.com>
  • Loading branch information
RealHarshThakur committed Oct 14, 2020
1 parent 49ff49b commit 63e8166
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
4 changes: 2 additions & 2 deletions common/go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module github.com/fission/keda-connectors/common

go 1.12
go 1.15

require (
github.com/aws/aws-sdk-go v1.34.25
github.com/pkg/errors v0.9.1
go.uber.org/zap v1.15.0
go.uber.org/zap v1.16.0
)
5 changes: 3 additions & 2 deletions common/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
Expand All @@ -33,8 +34,8 @@ go.uber.org/multierr v1.5.0 h1:KCa4XfM8CWFCpxXRGok+Q0SS/0XBhMDbHHGABQLvD2A=
go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU=
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee h1:0mgffUl7nfd+FpvXMVz4IDEaUSmT1ysygQC7qYo7sG4=
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA=
go.uber.org/zap v1.15.0 h1:ZZCA22JRF2gQE5FoNmhmrf7jeJJ2uhqDUNRYKm8dvmM=
go.uber.org/zap v1.15.0/go.mod h1:Mb2vm2krFEG5DV0W9qcHBYFtp/Wku1cvYaqPsS/WYfc=
go.uber.org/zap v1.16.0 h1:uFRZXykJGK9lLY4HtgSw44DnIcAM+kRBP7x5m+NpAOM=
go.uber.org/zap v1.16.0/go.mod h1:MA8QOfq0BHJwdXa996Y4dYkAqRKB8/1K1QMMZVaNZjQ=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de h1:5hukYrvBGR8/eNkX5mdUezrA6JiaEZDtJb9Ei+1LlBs=
Expand Down
31 changes: 16 additions & 15 deletions common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,22 @@ func ParseConnectorMetadata() (ConnectorMetadata, error) {

// HandleHTTPRequest sends message and headers data to HTTP endpoint using POST method and returns response on success or error in case of failure
func HandleHTTPRequest(message string, headers http.Header, data ConnectorMetadata, logger *zap.Logger) (*http.Response, error) {
// Create request
req, err := http.NewRequest("POST", data.HTTPEndpoint, strings.NewReader(message))
if err != nil {
return nil, errors.Wrapf(err, "failed to create HTTP request to invoke function. http_endpoint: %v, source: %v", data.HTTPEndpoint, data.SourceName)
}

// Add headers
for key, vals := range headers {
for _, val := range vals {
req.Header.Add(key, val)
}
}

var resp *http.Response
for attempt := 0; attempt <= data.MaxRetries; attempt++ {
// Create request
req, err := http.NewRequest("POST", data.HTTPEndpoint, strings.NewReader(message))
if err != nil {
return nil, errors.Wrapf(err, "failed to create HTTP request to invoke function. http_endpoint: %v, source: %v", data.HTTPEndpoint, data.SourceName)
}

// Add headers
for key, vals := range headers {
for _, val := range vals {
req.Header.Add(key, val)
}
}

// Make the request
resp, err = http.DefaultClient.Do(req)
if err != nil {
Expand All @@ -80,17 +81,17 @@ func HandleHTTPRequest(message string, headers http.Header, data ConnectorMetada
if resp == nil {
continue
}
if err == nil && resp.StatusCode == http.StatusOK {
if err == nil && resp.StatusCode >= 200 && resp.StatusCode < 300 {
// Success, quit retrying
break
return resp, nil
}
}

if resp == nil {
return nil, fmt.Errorf("every function invocation retry failed; final retry gave empty response. http_endpoint: %v, source: %v", data.HTTPEndpoint, data.SourceName)
}

if resp.StatusCode != 200 {
if resp.StatusCode < 200 && resp.StatusCode > 300 {
return nil, fmt.Errorf("request returned failure: %v. http_endpoint: %v, source: %v", resp.StatusCode, data.HTTPEndpoint, data.SourceName)
}
return resp, nil
Expand Down

0 comments on commit 63e8166

Please sign in to comment.