From 7b382ada9f5ada76e4bd6392589d49194bf1649b Mon Sep 17 00:00:00 2001 From: bakito Date: Wed, 16 Jul 2025 16:59:39 +0200 Subject: [PATCH] fix: Correct version check and refactor healthz check --- pkg/sync/http.go | 16 +++++++++++++++- pkg/sync/sync.go | 25 +------------------------ 2 files changed, 16 insertions(+), 25 deletions(-) diff --git a/pkg/sync/http.go b/pkg/sync/http.go index ed2e160..3803924 100644 --- a/pkg/sync/http.go +++ b/pkg/sync/http.go @@ -94,7 +94,21 @@ func (w *worker) handleStatus(c *gin.Context) { } func (w *worker) handleHealthz(c *gin.Context) { - c.Status(w.healthz()) + status := w.status() + + if status.Origin.Status != "success" { + c.Status(http.StatusServiceUnavailable) + return + } + + for _, replica := range status.Replicas { + if replica.Status != "success" { + c.Status(http.StatusServiceUnavailable) + return + } + } + + c.Status(http.StatusOK) } func (w *worker) listenAndServe() { diff --git a/pkg/sync/sync.go b/pkg/sync/sync.go index 0f1a98e..9eeaa08 100644 --- a/pkg/sync/sync.go +++ b/pkg/sync/sync.go @@ -3,7 +3,6 @@ package sync import ( "errors" "fmt" - "regexp" "runtime" "sort" "time" @@ -21,10 +20,7 @@ import ( "github.com/bakito/adguardhome-sync/version" ) -var ( - l = log.GetLogger("sync") - fixVersionCompareRegExp = regexp.MustCompile(`[^0-9.]`) -) +var l = log.GetLogger("sync") // Sync config from origin to replica. func Sync(cfg *types.Config) error { @@ -124,22 +120,6 @@ func (w *worker) status() *syncStatus { return syncStatus } -func (w *worker) healthz() int { - status := w.status() - - if status.Origin.Status != "success" { - return 503 - } - - for _, replica := range status.Replicas { - if replica.Status != "success" { - return 503 - } - } - - return 200 -} - func (w *worker) getStatus(inst types.AdGuardInstance) replicaStatus { st := replicaStatus{Host: inst.WebHost, URL: inst.WebURL} @@ -313,9 +293,6 @@ func (w *worker) syncTo(l *zap.SugaredLogger, o *origin, replica types.AdGuardIn return } - replicaStatus.Version = fixVersionCompareRegExp.ReplaceAllString(replicaStatus.Version, "") - o.status.Version = fixVersionCompareRegExp.ReplaceAllString(o.status.Version, "") - rl.With("version", replicaStatus.Version).Info("Connected to replica") if versions.IsNewerThan(versions.MinAgh, replicaStatus.Version) {