|
|
@ -1,7 +1,9 @@
|
|
|
|
package health
|
|
|
|
package health
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/syncx"
|
|
|
|
"github.com/zeromicro/go-zero/core/syncx"
|
|
|
@ -41,6 +43,18 @@ func AddProbe(probe Probe) {
|
|
|
|
defaultHealthManager.addProbe(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.
|
|
|
|
// NewHealthManager returns a new healthManager.
|
|
|
|
func NewHealthManager(name string) Probe {
|
|
|
|
func NewHealthManager(name string) Probe {
|
|
|
|
return &healthManager{
|
|
|
|
return &healthManager{
|
|
|
@ -102,6 +116,7 @@ func (p *comboHealthManager) IsReady() bool {
|
|
|
|
return false
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -109,15 +124,16 @@ func (p *comboHealthManager) verboseInfo() string {
|
|
|
|
p.mu.Lock()
|
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
|
|
|
|
|
|
|
|
var info string
|
|
|
|
var info strings.Builder
|
|
|
|
for _, probe := range p.probes {
|
|
|
|
for _, probe := range p.probes {
|
|
|
|
if probe.IsReady() {
|
|
|
|
if probe.IsReady() {
|
|
|
|
info += probe.Name() + " is ready; \n"
|
|
|
|
info.WriteString(fmt.Sprintf("%s is ready\n", probe.Name()))
|
|
|
|
} else {
|
|
|
|
} 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.
|
|
|
|
// addProbe add components probe to comboHealthManager.
|
|
|
@ -127,14 +143,3 @@ func (p *comboHealthManager) addProbe(probe Probe) {
|
|
|
|
|
|
|
|
|
|
|
|
p.probes = append(p.probes, 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)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|