communicator/ssh: retry connection in ConnectFunc forawhile

This commit is contained in:
Mitchell Hashimoto 2013-07-14 21:02:47 +09:00
parent 2fff555e7f
commit ebd3742e3e
1 changed files with 18 additions and 2 deletions

View File

@ -1,6 +1,7 @@
package ssh package ssh
import ( import (
"errors"
"log" "log"
"net" "net"
"time" "time"
@ -11,7 +12,22 @@ import (
// is suitable for use with the SSH communicator configuration. // is suitable for use with the SSH communicator configuration.
func ConnectFunc(network, addr string) func() (net.Conn, error) { func ConnectFunc(network, addr string) func() (net.Conn, error) {
return func() (net.Conn, error) { return func() (net.Conn, error) {
log.Printf("Opening conn for SSH to %s %s", network, addr) timeout := time.After(5 * time.Minute)
return net.DialTimeout(network, addr, 15*time.Second)
for {
select {
case <-timeout:
return nil, errors.New("timeout connecting to remote machine")
default:
}
log.Printf("Opening conn for SSH to %s %s", network, addr)
nc, err := net.DialTimeout(network, addr, 15*time.Second)
if err == nil {
return nc, nil
}
time.Sleep(500 * time.Millisecond)
}
} }
} }