func waitForUrl()

in cmd/hub/lifecycle/ready.go [130:181]


func waitForUrl(ctx context.Context, url string, waitSeconds int) error {
	if !strings.HasPrefix(url, "http://") && !strings.HasPrefix(url, "https://") {
		return fmt.Errorf("Only HTTP and HTTPS is supported in lifecycle.readyCondition.URL, expanded to `%s`", url)
	}
	if config.Verbose {
		log.Printf("Waiting for `%s` to respond", url)
	}
	interval := time.Duration(10) * time.Second
	client := util.RobustHttpClient(interval, true)
	client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
		return http.ErrUseLastResponse
	}
	start := time.Now()
	lastMsg := ""
	for time.Since(start) < time.Duration(waitSeconds)*time.Second {
		req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
		if err != nil {
			return err
		}
		response, err := client.Do(req)
		if config.Verbose {
			msg := ""
			if err != nil {
				msg = fmt.Sprintf("%v", err)
			} else {
				if config.Trace {
					msg = fmt.Sprintf("`%s` responded with:\n\t%+v", url, response)
				} else {
					msg = fmt.Sprintf("`%s` responded with: %s", url, response.Status)
				}
			}
			if config.Debug || (config.Verbose && lastMsg != msg) {
				log.Print(msg)
				lastMsg = msg
			}
		}
		if util.ContextCanceled(err) {
			return err
		}
		if err == nil {
			response.Body.Close()
			if response.StatusCode >= 100 && response.StatusCode < 500 {
				return nil
			}
			if config.Verbose {
				log.Printf("`%s` responded with: %s", url, response.Status)
			}
		}
		time.Sleep(interval)
	}
	return fmt.Errorf("Timeout waiting for `%s` to respond", url)
}