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
import (
"errors"
"log"
"net"
"time"
@ -11,7 +12,22 @@ import (
// is suitable for use with the SSH communicator configuration.
func ConnectFunc(network, addr string) func() (net.Conn, error) {
return func() (net.Conn, error) {
log.Printf("Opening conn for SSH to %s %s", network, addr)
return net.DialTimeout(network, addr, 15*time.Second)
timeout := time.After(5 * time.Minute)
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)
}
}
}