From 7bc46ce6b2070894b9144a927c86e2a274566f2e Mon Sep 17 00:00:00 2001 From: Evangelos Foutras Date: Tue, 2 May 2023 12:41:27 +0300 Subject: [PATCH] Fix "Could not connect to x: not connected" errors Tweak SSHConnectionManager.Connect() to return a new connection if the previous one is not connected. (This occurs with expired connections.) Maybe fixes: https://github.com/czerwonk/junos_exporter/issues/56 --- pkg/connector/connection_manager.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/connector/connection_manager.go b/pkg/connector/connection_manager.go index 4a91fe3f..df15db88 100644 --- a/pkg/connector/connection_manager.go +++ b/pkg/connector/connection_manager.go @@ -88,11 +88,9 @@ func (m *SSHConnectionManager) lockForDevice(device *Device) *sync.Mutex { // Connect connects to a device or returns an long living connection func (m *SSHConnectionManager) Connect(device *Device) (*SSHConnection, error) { if connection, found := m.connections[device.Host]; found { - if !connection.isConnected() { - return nil, errors.New("not connected") + if connection.isConnected() { + return connection, nil } - - return connection, nil } mu := m.lockForDevice(device) @@ -100,7 +98,9 @@ func (m *SSHConnectionManager) Connect(device *Device) (*SSHConnection, error) { defer mu.Unlock() if connection, found := m.connections[device.Host]; found { - return connection, nil + if connection.isConnected() { + return connection, nil + } } return m.connect(device)