mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-05-24 16:59:26 +00:00
health: Add comments
This commit is contained in:
parent
6fc5fbee21
commit
b1597736d7
6 changed files with 15 additions and 3 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Reference in a new issue