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 <aerosound161@gmail.com>
  • Loading branch information
aerosouund committed Aug 23, 2024
1 parent 4081f58 commit b54838d
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 b54838d

Please sign in to comment.