From ebd3742e3eef9ad5880365a8a60eb6abb7f87bdd Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 14 Jul 2013 21:02:47 +0900 Subject: [PATCH] communicator/ssh: retry connection in ConnectFunc forawhile --- communicator/ssh/connect.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/communicator/ssh/connect.go b/communicator/ssh/connect.go index 3ea91ce97..7c04d9655 100644 --- a/communicator/ssh/connect.go +++ b/communicator/ssh/connect.go @@ -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) + } } }