From 091d7f84c1b881f2a72ac273bc9e83cefd297a23 Mon Sep 17 00:00:00 2001 From: Tsuyoshi Ushio Date: Fri, 23 Oct 2020 01:58:16 -0700 Subject: [PATCH] fix:memory leak (#1284) Signed-off-by: Tsuyoshi Ushio --- pkg/scalers/gcp_pub_sub_scaler.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/pkg/scalers/gcp_pub_sub_scaler.go b/pkg/scalers/gcp_pub_sub_scaler.go index 7b4e371e5db..58b004cc0b7 100644 --- a/pkg/scalers/gcp_pub_sub_scaler.go +++ b/pkg/scalers/gcp_pub_sub_scaler.go @@ -94,6 +94,7 @@ func (s *pubsubScaler) IsActive(ctx context.Context) (bool, error) { func (s *pubsubScaler) Close() error { if s.client != nil { err := s.client.metricsClient.Close() + s.client = nil if err != nil { gcpPubSubLog.Error(err, "error closing StackDriver client") } @@ -147,13 +148,15 @@ func (s *pubsubScaler) GetMetrics(ctx context.Context, metricName string, metric // GetSubscriptionSize gets the number of messages in a subscription by calling the // Stackdriver api func (s *pubsubScaler) GetSubscriptionSize(ctx context.Context) (int64, error) { - client, err := NewStackDriverClient(ctx, s.metadata.credentials) - if err != nil { - return -1, err + if s.client == nil { + client, err := NewStackDriverClient(ctx, s.metadata.credentials) + if err != nil { + return -1, err + } + s.client = client } - s.client = client filter := `metric.type="` + pubSubStackDriverMetricName + `" AND resource.labels.subscription_id="` + s.metadata.subscriptionName + `"` - return client.GetMetrics(ctx, filter) + return s.client.GetMetrics(ctx, filter) }