Skip to content

Commit

Permalink
Print the last response if we timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
bdwyertech committed Jul 7, 2021
1 parent 524bad0 commit d35dd32
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
package main

import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"flag"
"fmt"
"io"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -190,6 +193,8 @@ func waitUntilHealthy() {
ctx, cancel := context.WithTimeout(context.Background(), healthcheckTimeout)
defer cancel()

var bodyBytes []byte

for {
req, err := http.NewRequestWithContext(ctx, "GET", healthcheckUrl, nil)
if err != nil {
Expand All @@ -201,6 +206,14 @@ func waitUntilHealthy() {
resp, err := client.Do(req.WithContext(rctx))
if err != nil {
if ctxErr := ctx.Err(); ctxErr == context.DeadlineExceeded {
if len(bodyBytes) > 0 {
var prettyJSON bytes.Buffer
if err := json.Indent(&prettyJSON, bodyBytes, "", " "); err != nil {
log.Error(string(bodyBytes))
} else {
log.Error(prettyJSON.String())
}
}
log.Fatal(fmt.Errorf("healthcheck exceeded timeout(%s): %w", healthcheckTimeout, err))
}
if ctxErr := rctx.Err(); ctxErr == context.DeadlineExceeded {
Expand All @@ -217,6 +230,8 @@ func waitUntilHealthy() {
return
default:
log.Warnf("%v :: (%v) %v", healthcheckUrl, resp.StatusCode, resp.Status)
bodyBytes, _ = io.ReadAll(resp.Body)
resp.Body.Close()
time.Sleep(5 * time.Second)
continue
}
Expand Down

0 comments on commit d35dd32

Please sign in to comment.