* fix: #2672

* chore: fix more cases

* chore: update deps

* chore: update deps

* chore: refactor

* chore: refactor

* chore: refactor
This commit is contained in:
Kevin Wan
2022-12-11 00:41:50 +08:00
committed by GitHub
parent ef22042f4d
commit fdc57d07d7
14 changed files with 157 additions and 65 deletions

View File

@@ -1,7 +1,9 @@
package health
import (
"fmt"
"net/http"
"strings"
"sync"
"github.com/zeromicro/go-zero/core/syncx"
@@ -41,6 +43,18 @@ func AddProbe(probe Probe) {
defaultHealthManager.addProbe(probe)
}
// CreateHttpHandler create health http handler base on given probe.
func CreateHttpHandler() http.HandlerFunc {
return func(w http.ResponseWriter, _ *http.Request) {
if defaultHealthManager.IsReady() {
_, _ = w.Write([]byte("OK"))
} else {
http.Error(w, "Service Unavailable\n"+defaultHealthManager.verboseInfo(),
http.StatusServiceUnavailable)
}
}
}
// NewHealthManager returns a new healthManager.
func NewHealthManager(name string) Probe {
return &healthManager{
@@ -102,6 +116,7 @@ func (p *comboHealthManager) IsReady() bool {
return false
}
}
return true
}
@@ -109,15 +124,16 @@ func (p *comboHealthManager) verboseInfo() string {
p.mu.Lock()
defer p.mu.Unlock()
var info string
var info strings.Builder
for _, probe := range p.probes {
if probe.IsReady() {
info += probe.Name() + " is ready; \n"
info.WriteString(fmt.Sprintf("%s is ready\n", probe.Name()))
} else {
info += probe.Name() + " is not ready; \n"
info.WriteString(fmt.Sprintf("%s is not ready\n", probe.Name()))
}
}
return info
return info.String()
}
// addProbe add components probe to comboHealthManager.
@@ -127,14 +143,3 @@ func (p *comboHealthManager) addProbe(probe Probe) {
p.probes = append(p.probes, probe)
}
// CreateHttpHandler create health http handler base on given probe.
func CreateHttpHandler() http.HandlerFunc {
return func(w http.ResponseWriter, request *http.Request) {
if defaultHealthManager.IsReady() {
_, _ = w.Write([]byte("OK"))
} else {
http.Error(w, "Service Unavailable\n"+defaultHealthManager.verboseInfo(), http.StatusServiceUnavailable)
}
}
}