From bdb3343a7c236d2e57f1d88f74b5f5b6cb7e3137 Mon Sep 17 00:00:00 2001 From: yusing Date: Tue, 3 Jun 2025 22:56:00 +0800 Subject: [PATCH] fix(healthcheck): handle cases for zero port --- internal/watcher/health/monitor/monitor.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/internal/watcher/health/monitor/monitor.go b/internal/watcher/health/monitor/monitor.go index e7930da..c7567b2 100644 --- a/internal/watcher/health/monitor/monitor.go +++ b/internal/watcher/health/monitor/monitor.go @@ -31,6 +31,8 @@ type ( checkHealth HealthCheckFunc startTime time.Time + isZeroPort bool + task *task.Task } ) @@ -63,14 +65,24 @@ func NewMonitor(r routes.Route) health.HealthMonCheck { 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{ config: config, checkHealth: healthCheckFunc, startTime: time.Now(), } - mon.url.Store(url) + if u == nil { + u = &url.URL{} + } + mon.url.Store(u) 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 } @@ -87,6 +99,10 @@ func (mon *monitor) Start(parent task.Parent) gperr.Error { return ErrNegativeInterval } + if mon.isZeroPort { + return nil + } + mon.service = parent.Name() mon.task = parent.Subtask("health_monitor", true)