A library to wait for a network connection
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

25 lines
413 B

package waitfor
import (
"net"
"time"
)
func Try(network, address string, interval, limit time.Duration) bool {
start := time.Now()
for {
if limit >= 0 && time.Since(start) > limit {
return false
}
attempt := time.Now()
conn, err := net.DialTimeout(network, address, interval)
if err != nil {
time.Sleep(time.Until(attempt.Add(interval)))
continue
}
conn.Close()
return true
}
}