Skip to content

Commit

Permalink
Adding HTTP timeout to the IBM MQ Scaler (#1758)
Browse files Browse the repository at this point in the history
* Adding HTTP timeout to the IBM MQ Scaler

Signed-off-by: Aaron Schlesinger <aaron@ecomaz.net>

* adding to changelog

Signed-off-by: Aaron Schlesinger <aaron@ecomaz.net>

* fixing tests

Signed-off-by: Aaron Schlesinger <aaron@ecomaz.net>
  • Loading branch information
arschles committed Apr 28, 2021
1 parent 69b69ba commit c7e25fa
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- Add Azure Pipelines Scaler ([#1706](https://github.com/kedacore/keda/pull/1706))
- Add OpenStack Metrics Scaler ([#1382](https://github.com/kedacore/keda/issues/1382))
- Fixed goroutine leaks in usage of timers ([#1704](https://github.com/kedacore/keda/pull/1704) | [#1739](https://github.com/kedacore/keda/pull/1739))
- Setting timeouts in the HTTP client used by the IBM MQ scaler ([#1758](https://github.com/kedacore/keda/pull/1758))

### New

Expand Down
12 changes: 9 additions & 3 deletions pkg/scalers/ibmmq_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
"net/url"
"strconv"
"time"

v2beta2 "k8s.io/api/autoscaling/v2beta2"
"k8s.io/apimachinery/pkg/api/resource"
Expand All @@ -29,7 +30,8 @@ const (

// IBMMQScaler assigns struct data pointer to metadata variable
type IBMMQScaler struct {
metadata *IBMMQMetadata
metadata *IBMMQMetadata
defaultHTTPTimeout time.Duration
}

// IBMMQMetadata Metadata used by KEDA to query IBM MQ queue depth and scale
Expand Down Expand Up @@ -65,7 +67,10 @@ func NewIBMMQScaler(config *ScalerConfig) (Scaler, error) {
return nil, fmt.Errorf("error parsing IBM MQ metadata: %s", err)
}

return &IBMMQScaler{metadata: meta}, nil
return &IBMMQScaler{
metadata: meta,
defaultHTTPTimeout: config.GlobalHTTPTimeout,
}, nil
}

// Close closes and returns nil
Expand Down Expand Up @@ -168,7 +173,8 @@ func (s *IBMMQScaler) getQueueDepthViaHTTP() (int, error) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: s.metadata.tlsDisabled},
}
client := &http.Client{Transport: tr}
client := kedautil.CreateHTTPClient(s.defaultHTTPTimeout)
client.Transport = tr

resp, err := client.Do(req)
if err != nil {
Expand Down
7 changes: 6 additions & 1 deletion pkg/scalers/ibmmq_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package scalers
import (
"fmt"
"testing"
"time"
)

// Test host URLs for validation
Expand Down Expand Up @@ -98,11 +99,15 @@ func TestParseDefaultQueueDepth(t *testing.T) {
func TestIBMMQGetMetricSpecForScaling(t *testing.T) {
for _, testData := range IBMMQMetricIdentifiers {
metadata, err := parseIBMMQMetadata(&ScalerConfig{ResolvedEnv: sampleIBMMQResolvedEnv, TriggerMetadata: testData.metadataTestData.metadata, AuthParams: testData.metadataTestData.authParams})
httpTimeout := 100 * time.Millisecond

if err != nil {
t.Fatal("Could not parse metadata:", err)
}
mockIBMMQScaler := IBMMQScaler{metadata}
mockIBMMQScaler := IBMMQScaler{
metadata: metadata,
defaultHTTPTimeout: httpTimeout,
}
metricSpec := mockIBMMQScaler.GetMetricSpecForScaling()
metricName := metricSpec[0].External.Metric.Name

Expand Down

0 comments on commit c7e25fa

Please sign in to comment.