Add wait forever capability to EnsureInterface

This commit is contained in:
Adam Harrison committed 2015-07-03 14:01:34 +01:00
1 parent 3bd5ae56f0
commit 6097dc8aaa
1 file changed
+8 -1
+8 -1
View File
@@ -2,6 +2,7 @@ package net
import ( import (
"fmt" "fmt"
"math"
"net" "net"
"time" "time"
) )
@@ -10,11 +11,17 @@ const (
sleepTime = 100 * time.Millisecond sleepTime = 100 * time.Millisecond
) )
// Wait `wait` seconds for an interface to come up. Pass zero to check once
// and return immediately, or a negative value to wait indefinitely.
func EnsureInterface(ifaceName string, wait int) (iface *net.Interface, err error) { func EnsureInterface(ifaceName string, wait int) (iface *net.Interface, err error) {
if iface, err = findInterface(ifaceName); err == nil || wait == 0 { if iface, err = findInterface(ifaceName); err == nil || wait == 0 {
return return
} }
for i := time.Duration(wait) * time.Second / sleepTime; err != nil && i > 0; i-- { i := int64(math.MaxInt64)
if wait > 0 {
i = int64(time.Duration(wait) * time.Second / sleepTime)
}
for ; err != nil && i > 0; i-- {
time.Sleep(sleepTime) time.Sleep(sleepTime)
iface, err = findInterface(ifaceName) iface, err = findInterface(ifaceName)
} }