Skip to content

Commit

Permalink
Adds timeout when waiting for IP and stopped
Browse files Browse the repository at this point in the history
Signed-off-by: Zhongcheng Lao <Zhongcheng.Lao@microsoft.com>
  • Loading branch information
laozc committed Nov 6, 2019
1 parent cfb21a3 commit e5b2079
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions drivers/hyperv/hyperv.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package hyperv

import (
"encoding/json"
"errors"
"fmt"
"net"
"os"
Expand Down Expand Up @@ -42,6 +43,7 @@ const (
defaultVLanID = 0
defaultDisableDynamicMemory = false
defaultSwitchID = "c08cb7b8-9b3c-408e-8e30-5e16a3aeb444"
defaultTimeout = time.Minute * 10
)

// NewDriver creates a new Hyper-v driver with default settings.
Expand Down Expand Up @@ -340,12 +342,17 @@ func (d *Driver) chooseVirtualSwitch() (string, error) {
func (d *Driver) waitForIP() (string, error) {
log.Infof("Waiting for host to start...")

start := time.Now()
for {
ip, _ := d.GetIP()
if ip != "" {
return ip, nil
}

if time.Since(start) >= defaultTimeout {
return "", errors.New("timeout waiting for IP")
}

time.Sleep(1 * time.Second)
}
}
Expand All @@ -354,6 +361,7 @@ func (d *Driver) waitForIP() (string, error) {
func (d *Driver) waitStopped() error {
log.Infof("Waiting for host to stop...")

start := time.Now()
for {
s, err := d.GetState()
if err != nil {
Expand All @@ -364,6 +372,10 @@ func (d *Driver) waitStopped() error {
return nil
}

if time.Since(start) >= defaultTimeout {
return errors.New("timeout waiting for stopped")
}

time.Sleep(1 * time.Second)
}
}
Expand Down

0 comments on commit e5b2079

Please sign in to comment.