feat: add dev server and health (#2665)
* feat: add dev server and health * fix: fix ci * fix: fix comment. * feat: add enabled * remove no need test * feat: mv devServer to internal * feat: default enable pprof Co-authored-by: dylan.wang <dylan.wang@yijinin.com>
This commit is contained in:
140
internal/health/health.go
Normal file
140
internal/health/health.go
Normal file
@@ -0,0 +1,140 @@
|
||||
package health
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/syncx"
|
||||
)
|
||||
|
||||
// defaultHealthManager is global comboHealthManager for byone self.
|
||||
var defaultHealthManager = newComboHealthManager()
|
||||
|
||||
type (
|
||||
// Probe represents readiness status of given component.
|
||||
Probe interface {
|
||||
// MarkReady sets a ready state for the endpoint handlers.
|
||||
MarkReady()
|
||||
// MarkNotReady sets a not ready state for the endpoint handlers.
|
||||
MarkNotReady()
|
||||
// IsReady return inner state for the component.
|
||||
IsReady() bool
|
||||
// Name return probe name identifier
|
||||
Name() string
|
||||
}
|
||||
|
||||
// healthManager manage app healthy.
|
||||
healthManager struct {
|
||||
ready syncx.AtomicBool
|
||||
name string
|
||||
}
|
||||
|
||||
// comboHealthManager folds given probes into one, reflects their statuses in a thread-safe way.
|
||||
comboHealthManager struct {
|
||||
mu sync.Mutex
|
||||
probes []Probe
|
||||
}
|
||||
)
|
||||
|
||||
// AddProbe add components probe to global comboHealthManager.
|
||||
func AddProbe(probe Probe) {
|
||||
defaultHealthManager.addProbe(probe)
|
||||
}
|
||||
|
||||
// NewHealthManager returns a new healthManager.
|
||||
func NewHealthManager(name string) Probe {
|
||||
return &healthManager{
|
||||
name: name,
|
||||
}
|
||||
}
|
||||
|
||||
// MarkReady sets a ready state for the endpoint handlers.
|
||||
func (h *healthManager) MarkReady() {
|
||||
h.ready.Set(true)
|
||||
}
|
||||
|
||||
// MarkNotReady sets a not ready state for the endpoint handlers.
|
||||
func (h *healthManager) MarkNotReady() {
|
||||
h.ready.Set(false)
|
||||
}
|
||||
|
||||
// IsReady return inner state for the component.
|
||||
func (h *healthManager) IsReady() bool {
|
||||
return h.ready.True()
|
||||
}
|
||||
|
||||
// Name return probe name identifier
|
||||
func (h *healthManager) Name() string {
|
||||
return h.name
|
||||
}
|
||||
|
||||
func newComboHealthManager() *comboHealthManager {
|
||||
return &comboHealthManager{}
|
||||
}
|
||||
|
||||
// MarkReady sets components status to ready.
|
||||
func (p *comboHealthManager) MarkReady() {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
|
||||
for _, probe := range p.probes {
|
||||
probe.MarkReady()
|
||||
}
|
||||
}
|
||||
|
||||
// MarkNotReady sets components status to not ready with given error as a cause.
|
||||
func (p *comboHealthManager) MarkNotReady() {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
|
||||
for _, probe := range p.probes {
|
||||
probe.MarkNotReady()
|
||||
}
|
||||
}
|
||||
|
||||
// IsReady return composed status of all components.
|
||||
func (p *comboHealthManager) IsReady() bool {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
|
||||
for _, probe := range p.probes {
|
||||
if !probe.IsReady() {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (p *comboHealthManager) verboseInfo() string {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
|
||||
var info string
|
||||
for _, probe := range p.probes {
|
||||
if probe.IsReady() {
|
||||
info += probe.Name() + " is ready; \n"
|
||||
} else {
|
||||
info += probe.Name() + " is not ready; \n"
|
||||
}
|
||||
}
|
||||
return info
|
||||
}
|
||||
|
||||
// addProbe add components probe to comboHealthManager.
|
||||
func (p *comboHealthManager) addProbe(probe Probe) {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
140
internal/health/health_test.go
Normal file
140
internal/health/health_test.go
Normal file
@@ -0,0 +1,140 @@
|
||||
package health
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
const probeName = "probe"
|
||||
|
||||
func TestHealthManager(t *testing.T) {
|
||||
hm := NewHealthManager(probeName)
|
||||
assert.False(t, hm.IsReady())
|
||||
|
||||
hm.MarkReady()
|
||||
assert.True(t, hm.IsReady())
|
||||
|
||||
hm.MarkNotReady()
|
||||
assert.False(t, hm.IsReady())
|
||||
|
||||
t.Run("concurrent should works", func(t *testing.T) {
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(10)
|
||||
for i := 0; i < 10; i++ {
|
||||
go func() {
|
||||
hm.MarkReady()
|
||||
wg.Done()
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
assert.True(t, hm.IsReady())
|
||||
})
|
||||
}
|
||||
|
||||
func TestComboHealthManager(t *testing.T) {
|
||||
t.Run("base", func(t *testing.T) {
|
||||
chm := newComboHealthManager()
|
||||
hm1 := NewHealthManager(probeName)
|
||||
hm2 := NewHealthManager(probeName + "2")
|
||||
|
||||
assert.True(t, chm.IsReady())
|
||||
chm.addProbe(hm1)
|
||||
chm.addProbe(hm2)
|
||||
assert.False(t, chm.IsReady())
|
||||
hm1.MarkReady()
|
||||
assert.False(t, chm.IsReady())
|
||||
hm2.MarkReady()
|
||||
assert.True(t, chm.IsReady())
|
||||
})
|
||||
|
||||
t.Run("concurrent add probes", func(t *testing.T) {
|
||||
chm2 := newComboHealthManager()
|
||||
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(10)
|
||||
for i := 0; i < 10; i++ {
|
||||
go func() {
|
||||
hm := NewHealthManager(probeName)
|
||||
hm.MarkReady()
|
||||
chm2.addProbe(hm)
|
||||
wg.Done()
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
assert.True(t, chm2.IsReady())
|
||||
})
|
||||
|
||||
t.Run("markReady and markNotReady", func(t *testing.T) {
|
||||
chm2 := newComboHealthManager()
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
hm := NewHealthManager(probeName)
|
||||
chm2.addProbe(hm)
|
||||
}
|
||||
assert.False(t, chm2.IsReady())
|
||||
|
||||
chm2.MarkReady()
|
||||
assert.True(t, chm2.IsReady())
|
||||
|
||||
chm2.MarkNotReady()
|
||||
assert.False(t, chm2.IsReady())
|
||||
})
|
||||
}
|
||||
|
||||
func TestAddGlobalProbes(t *testing.T) {
|
||||
cleanupForTest(t)
|
||||
|
||||
t.Run("concurrent add probes", func(t *testing.T) {
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(10)
|
||||
for i := 0; i < 10; i++ {
|
||||
go func() {
|
||||
hm := NewHealthManager(probeName)
|
||||
hm.MarkReady()
|
||||
AddProbe(hm)
|
||||
wg.Done()
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
assert.True(t, defaultHealthManager.IsReady())
|
||||
})
|
||||
}
|
||||
|
||||
func TestCreateHttpHandler(t *testing.T) {
|
||||
cleanupForTest(t)
|
||||
srv := httptest.NewServer(CreateHttpHandler())
|
||||
defer srv.Close()
|
||||
|
||||
resp, err := http.Get(srv.URL)
|
||||
assert.Nil(t, err)
|
||||
_ = resp.Body.Close()
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
|
||||
hm := NewHealthManager(probeName)
|
||||
defaultHealthManager.addProbe(hm)
|
||||
|
||||
resp, err = http.Get(srv.URL)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, http.StatusServiceUnavailable, resp.StatusCode)
|
||||
content, _ := io.ReadAll(resp.Body)
|
||||
assert.True(t, strings.HasPrefix(string(content), "Service Unavailable"))
|
||||
_ = resp.Body.Close()
|
||||
|
||||
hm.MarkReady()
|
||||
resp, err = http.Get(srv.URL)
|
||||
assert.Nil(t, err)
|
||||
_ = resp.Body.Close()
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
}
|
||||
|
||||
func cleanupForTest(t *testing.T) {
|
||||
t.Cleanup(func() {
|
||||
defaultHealthManager = &comboHealthManager{}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user