From 673d2924d705f3e3a2a580b8fac57e933aac22d0 Mon Sep 17 00:00:00 2001 From: Zbynek Roubalik Date: Fri, 4 Sep 2020 10:23:20 +0200 Subject: [PATCH 1/2] GCP PubSub: close client Signed-off-by: Zbynek Roubalik --- pkg/scalers/gcp_pub_sub_scaler.go | 11 ++++++++++- pkg/scalers/gcp_pubsub_scaler_test.go | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/pkg/scalers/gcp_pub_sub_scaler.go b/pkg/scalers/gcp_pub_sub_scaler.go index 56526a3d1f6..5be3e3a447e 100644 --- a/pkg/scalers/gcp_pub_sub_scaler.go +++ b/pkg/scalers/gcp_pub_sub_scaler.go @@ -19,6 +19,7 @@ const ( ) type pubsubScaler struct { + client *StackDriverClient metadata *pubsubMetadata } @@ -92,6 +93,14 @@ func (s *pubsubScaler) IsActive(ctx context.Context) (bool, error) { } func (s *pubsubScaler) Close() error { + + if s.client != nil { + err := s.client.metricsClient.Close() + if err != nil { + gcpPubSubLog.Error(err, "error closing StackDriver client") + } + } + return nil } @@ -142,10 +151,10 @@ func (s *pubsubScaler) GetMetrics(ctx context.Context, metricName string, metric // Stackdriver api func (s *pubsubScaler) GetSubscriptionSize(ctx context.Context) (int64, error) { client, err := NewStackDriverClient(ctx, s.metadata.credentials) - if err != nil { return -1, err } + s.client = client filter := `metric.type="` + pubSubStackDriverMetricName + `" AND resource.labels.subscription_id="` + s.metadata.subscriptionName + `"` diff --git a/pkg/scalers/gcp_pubsub_scaler_test.go b/pkg/scalers/gcp_pubsub_scaler_test.go index fe73542abc7..8d8d888c06e 100644 --- a/pkg/scalers/gcp_pubsub_scaler_test.go +++ b/pkg/scalers/gcp_pubsub_scaler_test.go @@ -54,7 +54,7 @@ func TestGcpPubSubGetMetricSpecForScaling(t *testing.T) { if err != nil { t.Fatal("Could not parse metadata:", err) } - mockGcpPubSubScaler := pubsubScaler{meta} + mockGcpPubSubScaler := pubsubScaler{nil, meta} metricSpec := mockGcpPubSubScaler.GetMetricSpecForScaling() metricName := metricSpec[0].External.Metric.Name From fe721f870c3380ebecfc8549f8e92930f2f2fd70 Mon Sep 17 00:00:00 2001 From: Zbynek Roubalik Date: Mon, 7 Sep 2020 09:30:43 +0200 Subject: [PATCH 2/2] add to changelog Signed-off-by: Zbynek Roubalik --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c49ecf75e18..d1a16357002 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,7 @@ - Improve `kubectl get triggerauthentication` to show information about configured parameters ([#778](https://github.com/kedacore/keda/issues/778)) - Added ScaledObject Status Conditions to display status of scaling ([#750](https://github.com/kedacore/keda/pull/750)) - Added optional authentication parameters for the Redis Scaler ([#962](https://github.com/kedacore/keda/pull/962)) +- Improved GCP PubSub Scaler performance by closing the client correctly ([#1087](https://github.com/kedacore/keda/pull/1087)) ### Breaking Changes