fix: health check problem (#4590)

This commit is contained in:
Kevin Wan 2025-01-22 19:32:28 +08:00 committed by GitHub
parent 030c859171
commit 1023800b02
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 24 additions and 19 deletions

View File

@ -41,6 +41,7 @@ var (
type ( type (
statGetter struct { statGetter struct {
host string
dbName string dbName string
hash string hash string
poolStats func() sql.DBStats poolStats func() sql.DBStats

View File

@ -8,7 +8,7 @@ import (
"time" "time"
"github.com/go-sql-driver/mysql" "github.com/go-sql-driver/mysql"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/syncx" "github.com/zeromicro/go-zero/core/syncx"
) )
@ -27,19 +27,19 @@ func getCachedSqlConn(driverName, server string) (*sql.DB, error) {
return nil, err return nil, err
} }
if driverName == mysqlDriverName { if cfg, e := mysql.ParseDSN(server); e != nil {
if cfg, err := mysql.ParseDSN(server); err != nil { // if cannot parse, don't collect the metrics
// do nothing here logx.Error(e)
} else { } else {
checksum := sha256.Sum256([]byte(server)) checksum := sha256.Sum256([]byte(server))
connCollector.registerClient(&statGetter{ connCollector.registerClient(&statGetter{
dbName: cfg.DBName, host: cfg.Addr,
hash: hex.EncodeToString(checksum[:]), dbName: cfg.DBName,
poolStats: func() sql.DBStats { hash: hex.EncodeToString(checksum[:]),
return conn.Stats() poolStats: func() sql.DBStats {
}, return conn.Stats()
}) },
} })
} }
return conn, nil return conn, nil

View File

@ -111,6 +111,10 @@ func (p *comboHealthManager) IsReady() bool {
p.mu.Lock() p.mu.Lock()
defer p.mu.Unlock() defer p.mu.Unlock()
if len(p.probes) == 0 {
return false
}
for _, probe := range p.probes { for _, probe := range p.probes {
if !probe.IsReady() { if !probe.IsReady() {
return false return false

View File

@ -43,7 +43,7 @@ func TestComboHealthManager(t *testing.T) {
hm1 := NewHealthManager(probeName) hm1 := NewHealthManager(probeName)
hm2 := NewHealthManager(probeName + "2") hm2 := NewHealthManager(probeName + "2")
assert.True(t, chm.IsReady()) assert.False(t, chm.IsReady())
chm.addProbe(hm1) chm.addProbe(hm1)
chm.addProbe(hm2) chm.addProbe(hm2)
assert.False(t, chm.IsReady()) assert.False(t, chm.IsReady())
@ -57,7 +57,7 @@ func TestComboHealthManager(t *testing.T) {
chm := newComboHealthManager() chm := newComboHealthManager()
hm := NewHealthManager(probeName) hm := NewHealthManager(probeName)
assert.True(t, chm.IsReady()) assert.False(t, chm.IsReady())
chm.addProbe(hm) chm.addProbe(hm)
assert.False(t, chm.IsReady()) assert.False(t, chm.IsReady())
hm.MarkReady() hm.MarkReady()
@ -127,7 +127,7 @@ func TestCreateHttpHandler(t *testing.T) {
resp, err := http.Get(srv.URL) resp, err := http.Get(srv.URL)
assert.Nil(t, err) assert.Nil(t, err)
_ = resp.Body.Close() _ = resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode) assert.Equal(t, http.StatusServiceUnavailable, resp.StatusCode)
hm := NewHealthManager(probeName) hm := NewHealthManager(probeName)
defaultHealthManager.addProbe(hm) defaultHealthManager.addProbe(hm)

View File

@ -78,4 +78,4 @@ func Test{{.HandlerName}}(t *testing.T) {
assert.Contains(t, rr.Body.String(), tt.wantResp) assert.Contains(t, rr.Body.String(), tt.wantResp)
}) })
} }
} }

View File

@ -66,4 +66,4 @@ func Test{{.logic}}_{{.function}}(t *testing.T) {
tt.checkResp({{if .hasResponse}}resp, {{end}}err) tt.checkResp({{if .hasResponse}}resp, {{end}}err)
}) })
} }
} }