26 lines
413 B
Go
26 lines
413 B
Go
|
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
|
||
|
}
|
||
|
}
|