Skip to content

Commit 1023800

Browse files
authoredJan 22, 2025
fix: health check problem (#4590)
1 parent 030c859 commit 1023800

File tree

6 files changed

+24
-19
lines changed

6 files changed

+24
-19
lines changed
 

‎core/stores/sqlx/metrics.go

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ var (
4141

4242
type (
4343
statGetter struct {
44+
host string
4445
dbName string
4546
hash string
4647
poolStats func() sql.DBStats

‎core/stores/sqlx/sqlmanager.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"time"
99

1010
"github.com/go-sql-driver/mysql"
11-
11+
"github.com/zeromicro/go-zero/core/logx"
1212
"github.com/zeromicro/go-zero/core/syncx"
1313
)
1414

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

30-
if driverName == mysqlDriverName {
31-
if cfg, err := mysql.ParseDSN(server); err != nil {
32-
// do nothing here
33-
} else {
34-
checksum := sha256.Sum256([]byte(server))
35-
connCollector.registerClient(&statGetter{
36-
dbName: cfg.DBName,
37-
hash: hex.EncodeToString(checksum[:]),
38-
poolStats: func() sql.DBStats {
39-
return conn.Stats()
40-
},
41-
})
42-
}
30+
if cfg, e := mysql.ParseDSN(server); e != nil {
31+
// if cannot parse, don't collect the metrics
32+
logx.Error(e)
33+
} else {
34+
checksum := sha256.Sum256([]byte(server))
35+
connCollector.registerClient(&statGetter{
36+
host: cfg.Addr,
37+
dbName: cfg.DBName,
38+
hash: hex.EncodeToString(checksum[:]),
39+
poolStats: func() sql.DBStats {
40+
return conn.Stats()
41+
},
42+
})
4343
}
4444

4545
return conn, nil

‎internal/health/health.go

+4
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ func (p *comboHealthManager) IsReady() bool {
111111
p.mu.Lock()
112112
defer p.mu.Unlock()
113113

114+
if len(p.probes) == 0 {
115+
return false
116+
}
117+
114118
for _, probe := range p.probes {
115119
if !probe.IsReady() {
116120
return false

‎internal/health/health_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func TestComboHealthManager(t *testing.T) {
4343
hm1 := NewHealthManager(probeName)
4444
hm2 := NewHealthManager(probeName + "2")
4545

46-
assert.True(t, chm.IsReady())
46+
assert.False(t, chm.IsReady())
4747
chm.addProbe(hm1)
4848
chm.addProbe(hm2)
4949
assert.False(t, chm.IsReady())
@@ -57,7 +57,7 @@ func TestComboHealthManager(t *testing.T) {
5757
chm := newComboHealthManager()
5858
hm := NewHealthManager(probeName)
5959

60-
assert.True(t, chm.IsReady())
60+
assert.False(t, chm.IsReady())
6161
chm.addProbe(hm)
6262
assert.False(t, chm.IsReady())
6363
hm.MarkReady()
@@ -127,7 +127,7 @@ func TestCreateHttpHandler(t *testing.T) {
127127
resp, err := http.Get(srv.URL)
128128
assert.Nil(t, err)
129129
_ = resp.Body.Close()
130-
assert.Equal(t, http.StatusOK, resp.StatusCode)
130+
assert.Equal(t, http.StatusServiceUnavailable, resp.StatusCode)
131131

132132
hm := NewHealthManager(probeName)
133133
defaultHealthManager.addProbe(hm)

‎tools/goctl/api/gogen/handler_test.tpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,4 @@ func Test{{.HandlerName}}(t *testing.T) {
7878
assert.Contains(t, rr.Body.String(), tt.wantResp)
7979
})
8080
}
81-
}
81+
}

‎tools/goctl/api/gogen/logic_test.tpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,4 @@ func Test{{.logic}}_{{.function}}(t *testing.T) {
6666
tt.checkResp({{if .hasResponse}}resp, {{end}}err)
6767
})
6868
}
69-
}
69+
}

0 commit comments

Comments
 (0)