Skip to content

Commit

Permalink
fix: health check problem (#4590)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan authored Jan 22, 2025
1 parent 030c859 commit 1023800
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 19 deletions.
1 change: 1 addition & 0 deletions core/stores/sqlx/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var (

type (
statGetter struct {
host string
dbName string
hash string
poolStats func() sql.DBStats
Expand Down
28 changes: 14 additions & 14 deletions core/stores/sqlx/sqlmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"time"

"github.com/go-sql-driver/mysql"

"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/syncx"
)

Expand All @@ -27,19 +27,19 @@ func getCachedSqlConn(driverName, server string) (*sql.DB, error) {
return nil, err
}

if driverName == mysqlDriverName {
if cfg, err := mysql.ParseDSN(server); err != nil {
// do nothing here
} else {
checksum := sha256.Sum256([]byte(server))
connCollector.registerClient(&statGetter{
dbName: cfg.DBName,
hash: hex.EncodeToString(checksum[:]),
poolStats: func() sql.DBStats {
return conn.Stats()
},
})
}
if cfg, e := mysql.ParseDSN(server); e != nil {
// if cannot parse, don't collect the metrics
logx.Error(e)
} else {
checksum := sha256.Sum256([]byte(server))
connCollector.registerClient(&statGetter{
host: cfg.Addr,
dbName: cfg.DBName,
hash: hex.EncodeToString(checksum[:]),
poolStats: func() sql.DBStats {
return conn.Stats()
},
})
}

return conn, nil
Expand Down
4 changes: 4 additions & 0 deletions internal/health/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ func (p *comboHealthManager) IsReady() bool {
p.mu.Lock()
defer p.mu.Unlock()

if len(p.probes) == 0 {
return false
}

for _, probe := range p.probes {
if !probe.IsReady() {
return false
Expand Down
6 changes: 3 additions & 3 deletions internal/health/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestComboHealthManager(t *testing.T) {
hm1 := NewHealthManager(probeName)
hm2 := NewHealthManager(probeName + "2")

assert.True(t, chm.IsReady())
assert.False(t, chm.IsReady())
chm.addProbe(hm1)
chm.addProbe(hm2)
assert.False(t, chm.IsReady())
Expand All @@ -57,7 +57,7 @@ func TestComboHealthManager(t *testing.T) {
chm := newComboHealthManager()
hm := NewHealthManager(probeName)

assert.True(t, chm.IsReady())
assert.False(t, chm.IsReady())
chm.addProbe(hm)
assert.False(t, chm.IsReady())
hm.MarkReady()
Expand Down Expand Up @@ -127,7 +127,7 @@ func TestCreateHttpHandler(t *testing.T) {
resp, err := http.Get(srv.URL)
assert.Nil(t, err)
_ = resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, http.StatusServiceUnavailable, resp.StatusCode)

hm := NewHealthManager(probeName)
defaultHealthManager.addProbe(hm)
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/api/gogen/handler_test.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,4 @@ func Test{{.HandlerName}}(t *testing.T) {
assert.Contains(t, rr.Body.String(), tt.wantResp)
})
}
}
}
2 changes: 1 addition & 1 deletion tools/goctl/api/gogen/logic_test.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ func Test{{.logic}}_{{.function}}(t *testing.T) {
tt.checkResp({{if .hasResponse}}resp, {{end}}err)
})
}
}
}

0 comments on commit 1023800

Please sign in to comment.