Jack Johnson
Jack Johnson

Reputation: 55

Golang custom timeout solution

I need to create a custom timeout solution for ssh.Dial. I tried setting the timeout in the sshConfig, but it just doesn't work sometimes which causes the whole program to hang.

connection, err := ssh.Dial("tcp", "X.X.X.X:22", sshConfig)

In some cases it times out, it works in other cases. But it stumbles on specific IPs and doesn't do anything. It just hangs the entire program.

So how would I go about coding "my own" timeout solution for this line of code?

Upvotes: 0

Views: 676

Answers (2)

dustinevan
dustinevan

Reputation: 977

It looks like the config takes a timeout. See: https://godoc.org/golang.org/x/crypto/ssh#ClientConfig

in other words:

conn, err := ssh.Dail("tcp", "X.X.X.X:22", ssh.ClientConfig{Timeout: time.Second * 4})

Upvotes: 1

Jack Johnson
Jack Johnson

Reputation: 55

Did this and it worked:

    c1 := make(chan bool, 1)
    go RunRequest(element, authInfo, c1)
    select {
        case <-c1:
        case <-time.After(time.Second * 4):
    }

The ssh.Dial was inside the RunRequest GoRoutine.

Upvotes: 0

Related Questions