forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.go
35 lines (32 loc) · 883 Bytes
/
net.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package util
import (
"crypto/tls"
"net"
"time"
"github.com/golang/glog"
)
// WaitForDial attempts to connect to the given address, closing and returning nil on the first successful connection.
func WaitForSuccessfulDial(https bool, network, address string, timeout, interval time.Duration, retries int) error {
var (
conn net.Conn
err error
)
for i := 0; i <= retries; i++ {
dialer := net.Dialer{Timeout: timeout}
if https {
conn, err = tls.DialWithDialer(&dialer, network, address, &tls.Config{InsecureSkipVerify: true})
} else {
conn, err = dialer.Dial(network, address)
}
if err != nil {
glog.V(5).Infof("Got error %#v, trying again: %#v\n", err, address)
time.Sleep(interval)
continue
}
conn.Close()
glog.V(4).Infof("Got success: %#v\n", address)
return nil
}
glog.V(4).Infof("Got error, failing: %#v\n", address)
return err
}