From b54838d2ce2315bc09d77166b5a36c9c7f68bba5 Mon Sep 17 00:00:00 2001 From: aerosouund Date: Sat, 17 Aug 2024 15:42:15 +0300 Subject: [PATCH] feat: Introduce retries in ssh connection in the ssh client Signed-off-by: aerosouund --- cluster-provision/gocli/pkg/libssh/ssh.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/cluster-provision/gocli/pkg/libssh/ssh.go b/cluster-provision/gocli/pkg/libssh/ssh.go index e6f8f3d10d..06a57285aa 100644 --- a/cluster-provision/gocli/pkg/libssh/ssh.go +++ b/cluster-provision/gocli/pkg/libssh/ssh.go @@ -12,7 +12,10 @@ import ( "strings" "sync" + "time" + "github.com/bramvdbogaerde/go-scp" + "github.com/cenkalti/backoff/v4" "github.com/sirupsen/logrus" "golang.org/x/crypto/ssh" ) @@ -224,9 +227,24 @@ func (s *SSHClientImpl) executeCommand(cmd string, outWriter, errWriter io.Write func (s *SSHClientImpl) initClient() error { s.initMutex.Lock() defer s.initMutex.Unlock() - client, err := ssh.Dial("tcp", net.JoinHostPort("127.0.0.1", fmt.Sprint(s.sshPort)), s.config) + + var ( + client *ssh.Client + err error + ) + + operation := func() error { + client, err = ssh.Dial("tcp", net.JoinHostPort("127.0.0.1", fmt.Sprint(s.sshPort)), s.config) + return err + } + + backoffStrategy := backoff.NewExponentialBackOff() + backoffStrategy.InitialInterval = 3 * time.Second + backoffStrategy.MaxElapsedTime = 1 * time.Minute + + err = backoff.Retry(operation, backoffStrategy) if err != nil { - return fmt.Errorf("Failed to connect to SSH server: %v", err) + return err } conn, err := client.Dial("tcp", fmt.Sprintf("192.168.66.10%d:22", s.nodeIdx))