mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-06-02 21:18:40 +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"
|
"math/big"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// checkBlockNumber confirms this node is aware of a specific block.
|
||||||
func checkBlockNumber(ec ethClient, blockNumber *big.Int) error {
|
func checkBlockNumber(ec ethClient, blockNumber *big.Int) error {
|
||||||
_, err := ec.BlockByNumber(context.TODO(), blockNumber)
|
_, err := ec.BlockByNumber(context.TODO(), blockNumber)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ var (
|
||||||
errNotEnoughPeers = errors.New("not enough peers")
|
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 {
|
func checkMinPeers(ec ethClient, minPeerCount uint) error {
|
||||||
peerCount, err := ec.PeerCount(context.TODO())
|
peerCount, err := ec.PeerCount(context.TODO())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ var (
|
||||||
errNotSynced = errors.New("not synced")
|
errNotSynced = errors.New("not synced")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// checkSynced returns 'errNotSynced' if the node is in the syncing state.
|
||||||
func checkSynced(ec ethClient, r *http.Request) error {
|
func checkSynced(ec ethClient, r *http.Request) error {
|
||||||
i, err := ec.SyncProgress(context.TODO())
|
i, err := ec.SyncProgress(context.TODO())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -11,18 +11,19 @@ var (
|
||||||
errTimestampTooOld = errors.New("timestamp too old")
|
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(
|
func checkTime(
|
||||||
ec ethClient,
|
ec ethClient,
|
||||||
r *http.Request,
|
r *http.Request,
|
||||||
seconds int,
|
minTimestamp int,
|
||||||
) error {
|
) error {
|
||||||
i, err := ec.BlockByNumber(context.TODO(), nil)
|
i, err := ec.BlockByNumber(context.TODO(), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
timestamp := i.Time()
|
timestamp := i.Time()
|
||||||
if timestamp < uint64(seconds) {
|
if timestamp < uint64(minTimestamp) {
|
||||||
return fmt.Errorf("%w: got ts: %d, need: %d", errTimestampTooOld, timestamp, seconds)
|
return fmt.Errorf("%w: got ts: %d, need: %d", errTimestampTooOld, timestamp, minTimestamp)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,7 @@ type requestBody struct {
|
||||||
MaxSecondsBehind *int `json:"max_seconds_behind"`
|
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) {
|
func processFromHeaders(ec ethClient, headers []string, w http.ResponseWriter, r *http.Request) {
|
||||||
var (
|
var (
|
||||||
errCheckSynced = errCheckDisabled
|
errCheckSynced = errCheckDisabled
|
||||||
|
|
@ -82,6 +83,7 @@ func processFromHeaders(ec ethClient, headers []string, w http.ResponseWriter, r
|
||||||
reportHealth(nil, errCheckSynced, errCheckPeer, errCheckBlock, errCheckSeconds, w)
|
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) {
|
func processFromBody(ec ethClient, w http.ResponseWriter, r *http.Request) {
|
||||||
body, errParse := parseHealthCheckBody(r.Body)
|
body, errParse := parseHealthCheckBody(r.Body)
|
||||||
defer r.Body.Close()
|
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 {
|
func reportHealth(errParse, errCheckSynced, errCheckPeer, errCheckBlock, errCheckSeconds error, w http.ResponseWriter) error {
|
||||||
statusCode := http.StatusOK
|
statusCode := http.StatusOK
|
||||||
errs := make(map[string]string)
|
errs := make(map[string]string)
|
||||||
|
|
@ -157,6 +160,7 @@ func reportHealth(errParse, errCheckSynced, errCheckPeer, errCheckBlock, errChec
|
||||||
return writeResponse(w, errs, statusCode)
|
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) {
|
func parseHealthCheckBody(reader io.Reader) (requestBody, error) {
|
||||||
var body requestBody
|
var body requestBody
|
||||||
|
|
||||||
|
|
@ -173,6 +177,7 @@ func parseHealthCheckBody(reader io.Reader) (requestBody, error) {
|
||||||
return body, nil
|
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 {
|
func writeResponse(w http.ResponseWriter, errs map[string]string, statusCode int) error {
|
||||||
w.WriteHeader(statusCode)
|
w.WriteHeader(statusCode)
|
||||||
|
|
||||||
|
|
@ -189,10 +194,12 @@ func writeResponse(w http.ResponseWriter, errs map[string]string, statusCode int
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// shouldChangeStatusCode returns 'true' if an error exists and is not 'errCheckDisabled'.
|
||||||
func shouldChangeStatusCode(err error) bool {
|
func shouldChangeStatusCode(err error) bool {
|
||||||
return err != nil && !errors.Is(err, errCheckDisabled)
|
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 {
|
func errorStringOrOK(err error) string {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return "OK"
|
return "OK"
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ type handler struct {
|
||||||
ec *ethclient.Client
|
ec *ethclient.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ServeHTTP implements the http.Handler interface.
|
||||||
func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
headers := r.Header.Values(healthHeader)
|
headers := r.Header.Values(healthHeader)
|
||||||
if len(headers) != 0 {
|
if len(headers) != 0 {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue