Skip to content

Commit

Permalink
Better error message on startup healthcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
olivere committed Nov 16, 2017
1 parent 14940c7 commit 41b7339
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
6 changes: 6 additions & 0 deletions client.go
Expand Up @@ -1086,6 +1086,7 @@ func (c *Client) startupHealthcheck(timeout time.Duration) error {
c.mu.Unlock()

// If we don't get a connection after "timeout", we bail.
var lastErr error
start := time.Now()
for {
// Make a copy of the HTTP client provided via options to respect
Expand All @@ -1104,13 +1105,18 @@ func (c *Client) startupHealthcheck(timeout time.Duration) error {
res, err := cl.Do(req)
if err == nil && res != nil && res.StatusCode >= 200 && res.StatusCode < 300 {
return nil
} else if err != nil {
lastErr = err
}
}
time.Sleep(1 * time.Second)
if time.Now().Sub(start) > timeout {
break
}
}
if lastErr != nil {
return errors.Wrapf(ErrNoClient, "health check timeout: %v", lastErr)
}
return errors.Wrap(ErrNoClient, "health check timeout")
}

Expand Down
3 changes: 3 additions & 0 deletions client_test.go
Expand Up @@ -279,6 +279,9 @@ func TestClientHealthcheckStartupTimeout(t *testing.T) {
if !IsConnErr(err) {
t.Fatal(err)
}
if !strings.Contains(err.Error(), "connection refused") {
t.Fatalf("expected error to contain %q, have %q", "connection refused", err.Error())
}
if duration < 5*time.Second {
t.Fatalf("expected a timeout in more than 5 seconds; got: %v", duration)
}
Expand Down

0 comments on commit 41b7339

Please sign in to comment.