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" {`
release-3.5
Grigorii Sokolik 2020-03-05 14:47:46 +02:00
parent c4d8bdc881
commit a33e1b5fae
1 changed files with 20 additions and 20 deletions

View File

@ -91,33 +91,33 @@ type Health struct {
// TODO: server NOSPACE, etcdserver.ErrNoLeader in health API // TODO: server NOSPACE, etcdserver.ErrNoLeader in health API
func checkHealth(srv etcdserver.ServerV2) Health { func checkHealth(srv etcdserver.ServerV2) (h Health) {
h := Health{Health: "true"} h.Health = "true"
defer func() {
if h.Health == "true" {
healthSuccess.Inc()
} else {
healthFailed.Inc()
}
}()
as := srv.Alarms() as := srv.Alarms()
if len(as) > 0 { if len(as) > 0 {
h.Health = "false" h.Health = "false"
return
} }
if h.Health == "true" { if uint64(srv.Leader()) == raft.None {
if uint64(srv.Leader()) == raft.None { h.Health = "false"
h.Health = "false" return
}
} }
if h.Health == "true" { ctx, cancel := context.WithTimeout(context.Background(), time.Second)
ctx, cancel := context.WithTimeout(context.Background(), time.Second) _, err := srv.Do(ctx, etcdserverpb.Request{Method: "QGET"})
_, err := srv.Do(ctx, etcdserverpb.Request{Method: "QGET"}) cancel()
cancel() if err != nil {
if err != nil { h.Health = "false"
h.Health = "false"
}
} }
return
if h.Health == "true" {
healthSuccess.Inc()
} else {
healthFailed.Inc()
}
return h
} }