Skip to content

Commit

Permalink
feat: Introduce retries in ssh connection in the ssh client
Browse files Browse the repository at this point in the history
Signed-off-by: aerosouund <[email protected]>
  • Loading branch information
aerosouund committed Aug 23, 2024
1 parent 7a3f851 commit 28752d9
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions cluster-provision/gocli/pkg/libssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 28752d9

Please sign in to comment.