waitfor/waitfor.go

26 lines
413 B
Go
Raw Normal View History

2020-08-06 11:41:26 +02:00
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
}
}