diff --git a/health/check_block.go b/health/check_block.go index a68b560100..cea10738e8 100644 --- a/health/check_block.go +++ b/health/check_block.go @@ -5,6 +5,7 @@ import ( "math/big" ) +// checkBlockNumber confirms this node is aware of a specific block. func checkBlockNumber(ec ethClient, blockNumber *big.Int) error { _, err := ec.BlockByNumber(context.TODO(), blockNumber) if err != nil { diff --git a/health/check_peers.go b/health/check_peers.go index e20973348c..a2ddc840c1 100644 --- a/health/check_peers.go +++ b/health/check_peers.go @@ -10,6 +10,7 @@ var ( errNotEnoughPeers = errors.New("not enough peers") ) +// checkMinPeers returns 'errNotEnoughPeers' if the current peer count its lower than 'minPeerCount' func checkMinPeers(ec ethClient, minPeerCount uint) error { peerCount, err := ec.PeerCount(context.TODO()) if err != nil { diff --git a/health/check_synced.go b/health/check_synced.go index 5f068debae..2a06e769f2 100644 --- a/health/check_synced.go +++ b/health/check_synced.go @@ -12,6 +12,7 @@ var ( errNotSynced = errors.New("not synced") ) +// checkSynced returns 'errNotSynced' if the node is in the syncing state. func checkSynced(ec ethClient, r *http.Request) error { i, err := ec.SyncProgress(context.TODO()) if err != nil { diff --git a/health/check_time.go b/health/check_time.go index ae0df8dad4..dab5736223 100644 --- a/health/check_time.go +++ b/health/check_time.go @@ -11,18 +11,19 @@ var ( errTimestampTooOld = errors.New("timestamp too old") ) +// checkTime fetches the timestamp of the most recent block and returns an error if it is earlier than 'minTimestamp'. func checkTime( ec ethClient, r *http.Request, - seconds int, + minTimestamp int, ) error { i, err := ec.BlockByNumber(context.TODO(), nil) if err != nil { return err } timestamp := i.Time() - if timestamp < uint64(seconds) { - return fmt.Errorf("%w: got ts: %d, need: %d", errTimestampTooOld, timestamp, seconds) + if timestamp < uint64(minTimestamp) { + return fmt.Errorf("%w: got ts: %d, need: %d", errTimestampTooOld, timestamp, minTimestamp) } return nil diff --git a/health/health.go b/health/health.go index 402c02686a..c1cda74688 100644 --- a/health/health.go +++ b/health/health.go @@ -35,6 +35,7 @@ type requestBody struct { MaxSecondsBehind *int `json:"max_seconds_behind"` } +// processFromHeaders handles requests when 'X-GETH-HEALTHCHECK' header labels are present. func processFromHeaders(ec ethClient, headers []string, w http.ResponseWriter, r *http.Request) { var ( errCheckSynced = errCheckDisabled @@ -82,6 +83,7 @@ func processFromHeaders(ec ethClient, headers []string, w http.ResponseWriter, r reportHealth(nil, errCheckSynced, errCheckPeer, errCheckBlock, errCheckSeconds, w) } +// processFromBody handles requests when 'X-GETH-HEALTHCHECK' headers are not present. func processFromBody(ec ethClient, w http.ResponseWriter, r *http.Request) { body, errParse := parseHealthCheckBody(r.Body) defer r.Body.Close() @@ -125,6 +127,7 @@ func processFromBody(ec ethClient, w http.ResponseWriter, r *http.Request) { } } +// reportHealth builds the response body, sets the status code and calls for it to be written. func reportHealth(errParse, errCheckSynced, errCheckPeer, errCheckBlock, errCheckSeconds error, w http.ResponseWriter) error { statusCode := http.StatusOK errs := make(map[string]string) @@ -157,6 +160,7 @@ func reportHealth(errParse, errCheckSynced, errCheckPeer, errCheckBlock, errChec return writeResponse(w, errs, statusCode) } +// parseHealthCheckBody parses and type checks the request body when 'X-GETH-HEALTHCHECK' headers are not present. func parseHealthCheckBody(reader io.Reader) (requestBody, error) { var body requestBody @@ -173,6 +177,7 @@ func parseHealthCheckBody(reader io.Reader) (requestBody, error) { return body, nil } +// writeResponse delivers the status and body to the response writer. func writeResponse(w http.ResponseWriter, errs map[string]string, statusCode int) error { w.WriteHeader(statusCode) @@ -189,10 +194,12 @@ func writeResponse(w http.ResponseWriter, errs map[string]string, statusCode int return nil } +// shouldChangeStatusCode returns 'true' if an error exists and is not 'errCheckDisabled'. func shouldChangeStatusCode(err error) bool { return err != nil && !errors.Is(err, errCheckDisabled) } +// errorStringOrOK returns "OK", "DISABLED" or the error message based on the output of the check. func errorStringOrOK(err error) string { if err == nil { return "OK" diff --git a/health/service.go b/health/service.go index 6279dfd2a8..76232f15a2 100644 --- a/health/service.go +++ b/health/service.go @@ -11,6 +11,7 @@ type handler struct { ec *ethclient.Client } +// ServeHTTP implements the http.Handler interface. func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { headers := r.Header.Values(healthHeader) if len(headers) != 0 {