fix(healthcheck): handle cases for zero port

This commit is contained in:
yusing 2025-06-03 22:56:00 +08:00
parent b411c6d504
commit bdb3343a7c

View file

@ -31,6 +31,8 @@ type (
checkHealth HealthCheckFunc checkHealth HealthCheckFunc
startTime time.Time startTime time.Time
isZeroPort bool
task *task.Task task *task.Task
} }
) )
@ -63,14 +65,24 @@ func NewMonitor(r routes.Route) health.HealthMonCheck {
return mon return mon
} }
func newMonitor(url *url.URL, config *health.HealthCheckConfig, healthCheckFunc HealthCheckFunc) *monitor { func newMonitor(u *url.URL, config *health.HealthCheckConfig, healthCheckFunc HealthCheckFunc) *monitor {
mon := &monitor{ mon := &monitor{
config: config, config: config,
checkHealth: healthCheckFunc, checkHealth: healthCheckFunc,
startTime: time.Now(), startTime: time.Now(),
} }
mon.url.Store(url) if u == nil {
u = &url.URL{}
}
mon.url.Store(u)
mon.status.Store(health.StatusHealthy) mon.status.Store(health.StatusHealthy)
port := u.Port()
mon.isZeroPort = port == "" || port == "0"
if mon.isZeroPort {
mon.status.Store(health.StatusUnknown)
mon.lastResult.Store(&health.HealthCheckResult{Healthy: false, Detail: "no port detected"})
}
return mon return mon
} }
@ -87,6 +99,10 @@ func (mon *monitor) Start(parent task.Parent) gperr.Error {
return ErrNegativeInterval return ErrNegativeInterval
} }
if mon.isZeroPort {
return nil
}
mon.service = parent.Name() mon.service = parent.Name()
mon.task = parent.Subtask("health_monitor", true) mon.task = parent.Subtask("health_monitor", true)