From a33e1b5faed319993f811fc3ebfcfcdd0e393fb6 Mon Sep 17 00:00:00 2001 From: Grigorii Sokolik Date: Thu, 5 Mar 2020 14:47:46 +0200 Subject: [PATCH] etcdserver/api/etcdhttp: checkHealth refactoring Small refactoring of `go.etcd.io/etcd/etcdserver/api/etcdhttp/metrics.go.checkHealth` function just to avoid anoying repeatings of `if h.Health == "true" {` --- etcdserver/api/etcdhttp/metrics.go | 40 +++++++++++++++--------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/etcdserver/api/etcdhttp/metrics.go b/etcdserver/api/etcdhttp/metrics.go index f455e40a7..0fa2f75c2 100644 --- a/etcdserver/api/etcdhttp/metrics.go +++ b/etcdserver/api/etcdhttp/metrics.go @@ -91,33 +91,33 @@ type Health struct { // TODO: server NOSPACE, etcdserver.ErrNoLeader in health API -func checkHealth(srv etcdserver.ServerV2) Health { - h := Health{Health: "true"} +func checkHealth(srv etcdserver.ServerV2) (h Health) { + h.Health = "true" + + defer func() { + if h.Health == "true" { + healthSuccess.Inc() + } else { + healthFailed.Inc() + } + }() as := srv.Alarms() if len(as) > 0 { h.Health = "false" + return } - if h.Health == "true" { - if uint64(srv.Leader()) == raft.None { - h.Health = "false" - } + if uint64(srv.Leader()) == raft.None { + h.Health = "false" + return } - if h.Health == "true" { - ctx, cancel := context.WithTimeout(context.Background(), time.Second) - _, err := srv.Do(ctx, etcdserverpb.Request{Method: "QGET"}) - cancel() - if err != nil { - h.Health = "false" - } + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + _, err := srv.Do(ctx, etcdserverpb.Request{Method: "QGET"}) + cancel() + if err != nil { + h.Health = "false" } - - if h.Health == "true" { - healthSuccess.Inc() - } else { - healthFailed.Inc() - } - return h + return }