* fix: #2672 * chore: fix more cases * chore: update deps * chore: update deps * chore: refactor * chore: refactor * chore: refactor
This commit is contained in:
@@ -9,15 +9,12 @@ import (
|
||||
|
||||
"github.com/felixge/fgprof"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/core/threading"
|
||||
"github.com/zeromicro/go-zero/internal/health"
|
||||
)
|
||||
|
||||
var (
|
||||
once sync.Once
|
||||
)
|
||||
var once sync.Once
|
||||
|
||||
// Server is inner http server, expose some useful observability information of app.
|
||||
// For example health check, metrics and pprof.
|
||||
@@ -68,7 +65,7 @@ func (s *Server) StartAsync() {
|
||||
s.addRoutes()
|
||||
threading.GoSafe(func() {
|
||||
addr := fmt.Sprintf("%s:%d", s.config.Host, s.config.Port)
|
||||
logx.Infof("Starting inner http server at %s", addr)
|
||||
logx.Infof("Starting dev http server at %s", addr)
|
||||
if err := http.ListenAndServe(addr, s.server); err != nil {
|
||||
logx.Error(err)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ func TestComboHealthManager(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("concurrent add probes", func(t *testing.T) {
|
||||
chm2 := newComboHealthManager()
|
||||
chm := newComboHealthManager()
|
||||
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(10)
|
||||
@@ -62,28 +62,28 @@ func TestComboHealthManager(t *testing.T) {
|
||||
go func() {
|
||||
hm := NewHealthManager(probeName)
|
||||
hm.MarkReady()
|
||||
chm2.addProbe(hm)
|
||||
chm.addProbe(hm)
|
||||
wg.Done()
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
assert.True(t, chm2.IsReady())
|
||||
assert.True(t, chm.IsReady())
|
||||
})
|
||||
|
||||
t.Run("markReady and markNotReady", func(t *testing.T) {
|
||||
chm2 := newComboHealthManager()
|
||||
chm := newComboHealthManager()
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
hm := NewHealthManager(probeName)
|
||||
chm2.addProbe(hm)
|
||||
chm.addProbe(hm)
|
||||
}
|
||||
assert.False(t, chm2.IsReady())
|
||||
assert.False(t, chm.IsReady())
|
||||
|
||||
chm2.MarkReady()
|
||||
assert.True(t, chm2.IsReady())
|
||||
chm.MarkReady()
|
||||
assert.True(t, chm.IsReady())
|
||||
|
||||
chm2.MarkNotReady()
|
||||
assert.False(t, chm2.IsReady())
|
||||
chm.MarkNotReady()
|
||||
assert.False(t, chm.IsReady())
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user