1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Optimize test daemon startup

This adds some logs, handles timers better, and sets a request timeout
for the ping request.

I'm not sure the ticker in that loop is what we really want since the
ticker keeps ticking while we are (attempting) to make a request... but
I opted to not change that for now.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
This commit is contained in:
Brian Goff 2019-04-17 16:04:25 -07:00
parent 619df5a8f6
commit 20ea8942b8

View file

@ -278,41 +278,46 @@ func (d *Daemon) StartWithLogFile(out *os.File, providedArgs ...string) error {
d.Wait = wait d.Wait = wait
clientConfig, err := d.getClientConfig()
if err != nil {
return err
}
client := &http.Client{
Transport: clientConfig.transport,
}
req, err := http.NewRequest("GET", "/_ping", nil)
if err != nil {
return errors.Wrapf(err, "[%s] could not create new request", d.id)
}
req.URL.Host = clientConfig.addr
req.URL.Scheme = clientConfig.scheme
ticker := time.NewTicker(500 * time.Millisecond) ticker := time.NewTicker(500 * time.Millisecond)
defer ticker.Stop() defer ticker.Stop()
tick := ticker.C tick := ticker.C
timeout := time.NewTimer(60 * time.Second) // timeout for the whole loop
defer timeout.Stop()
// make sure daemon is ready to receive requests // make sure daemon is ready to receive requests
startTime := time.Now().Unix()
for { for {
d.log.Logf("[%s] waiting for daemon to start", d.id) d.log.Logf("[%s] waiting for daemon to start", d.id)
if time.Now().Unix()-startTime > 5 {
// After 5 seconds, give up
return errors.Errorf("[%s] Daemon exited and never started", d.id)
}
select { select {
case <-time.After(2 * time.Second): case <-timeout.C:
return errors.Errorf("[%s] timeout: daemon does not respond", d.id) return errors.Errorf("[%s] Daemon exited and never started", d.id)
case <-tick: case <-tick:
clientConfig, err := d.getClientConfig() ctx, cancel := context.WithTimeout(context.TODO(), 2*time.Second)
if err != nil { req := req.WithContext(ctx)
return err
}
client := &http.Client{
Transport: clientConfig.transport,
}
req, err := http.NewRequest("GET", "/_ping", nil)
if err != nil {
return errors.Wrapf(err, "[%s] could not create new request", d.id)
}
req.URL.Host = clientConfig.addr
req.URL.Scheme = clientConfig.scheme
resp, err := client.Do(req) resp, err := client.Do(req)
cancel()
if err != nil { if err != nil {
d.log.Logf("[%s] error pinging daemon on start: %v", d.id, err)
continue continue
} }
resp.Body.Close() resp.Body.Close()
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
d.log.Logf("[%s] received status != 200 OK: %s\n", d.id, resp.Status) d.log.Logf("[%s] received status != 200 OK: %s\n", d.id, resp.Status)
@ -412,8 +417,8 @@ func (d *Daemon) StopWithError() error {
if d.cmd == nil || d.Wait == nil { if d.cmd == nil || d.Wait == nil {
return errDaemonNotStarted return errDaemonNotStarted
} }
defer func() { defer func() {
d.log.Logf("[%s] Daemon stopped", d.id)
d.logFile.Close() d.logFile.Close()
d.cmd = nil d.cmd = nil
}() }()
@ -423,12 +428,15 @@ func (d *Daemon) StopWithError() error {
defer ticker.Stop() defer ticker.Stop()
tick := ticker.C tick := ticker.C
d.log.Logf("[%s] Stopping daemon", d.id)
if err := d.cmd.Process.Signal(os.Interrupt); err != nil { if err := d.cmd.Process.Signal(os.Interrupt); err != nil {
if strings.Contains(err.Error(), "os: process already finished") { if strings.Contains(err.Error(), "os: process already finished") {
return errDaemonNotStarted return errDaemonNotStarted
} }
return errors.Errorf("could not send signal: %v", err) return errors.Errorf("could not send signal: %v", err)
} }
out1: out1:
for { for {
select { select {
@ -482,6 +490,7 @@ func (d *Daemon) Restart(t testingT, args ...string) {
// RestartWithError will restart the daemon by first stopping it and then starting it. // RestartWithError will restart the daemon by first stopping it and then starting it.
func (d *Daemon) RestartWithError(arg ...string) error { func (d *Daemon) RestartWithError(arg ...string) error {
if err := d.StopWithError(); err != nil { if err := d.StopWithError(); err != nil {
d.log.Logf("[%s] Error when stopping daemon: %v", d.id, err)
return err return err
} }
return d.StartWithError(arg...) return d.StartWithError(arg...)