Skip to content

Commit

Permalink
Close Rabbitmq connections properly
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmelsayed committed Sep 25, 2019
1 parent 4bd1784 commit 2e59532
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 36 deletions.
48 changes: 19 additions & 29 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,20 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/main.go",
"env": {
},
"args": [

"--lister-kubeconfig",
"${env:HOME}/.kube/config"

],
"windows": {

"args": [
"--lister-kubeconfig",
"${env:USERPROFILE}\\.kube\\config"
]
}
}
]
}
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/main.go",
"env": {
"CONFIG": "${env:HOME}/.kube/config"
},
"args": [],
"windows": {}
}
]
}
2 changes: 1 addition & 1 deletion pkg/handler/scale_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ func (h *ScaleHandler) handleScale(ctx context.Context, scaledObject *keda_v1alp
isScaledObjectActive := false

for _, scaler := range scalers {
defer scaler.Close()
isTriggerActive, err := scaler.IsActive(ctx)

if err != nil {
Expand All @@ -337,7 +338,6 @@ func (h *ScaleHandler) handleScale(ctx context.Context, scaledObject *keda_v1alp
isScaledObjectActive = true
log.Debugf("Scaler %s for scaledObject %s/%s is active", scaler, scaledObject.GetNamespace(), scaledObject.GetName())
}
scaler.Close()
}

h.scaleDeployment(deployment, scaledObject, isScaledObjectActive)
Expand Down
8 changes: 2 additions & 6 deletions pkg/scalers/rabbitmq_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"strconv"

log "github.com/sirupsen/logrus"
"github.com/streadway/amqp"
v2beta1 "k8s.io/api/autoscaling/v2beta1"
"k8s.io/apimachinery/pkg/api/resource"
Expand Down Expand Up @@ -102,14 +103,9 @@ func getConnectionAndChannel(host string) (*amqp.Connection, *amqp.Channel, erro
func (s *rabbitMQScaler) Close() error {
err := s.connection.Close()
if err != nil {
log.Errorf("Error closing rabbitmq connection: %v", err)
return err
}

err = s.channel.Close()
if err != nil {
return err
}

return nil
}

Expand Down

0 comments on commit 2e59532

Please sign in to comment.