From 100eac1f3c50a025514da562d7fb266b8f95914b Mon Sep 17 00:00:00 2001 From: yusing Date: Thu, 4 Dec 2025 15:12:35 +0800 Subject: [PATCH] refactor(routes): simplify route exclusion check and health check defaults --- internal/route/fileserver.go | 12 ++++---- internal/route/provider/provider.go | 9 ++++-- internal/route/reverse_proxy.go | 4 --- internal/route/route.go | 33 ++++++---------------- internal/route/stream.go | 4 --- internal/types/routes.go | 4 +++ internal/watcher/health/monitor/monitor.go | 14 ++++----- 7 files changed, 30 insertions(+), 50 deletions(-) diff --git a/internal/route/fileserver.go b/internal/route/fileserver.go index 3b020474..bc15d21f 100644 --- a/internal/route/fileserver.go +++ b/internal/route/fileserver.go @@ -24,6 +24,8 @@ type ( } ) +var _ types.FileServerRoute = (*FileServer)(nil) + func handler(root string) http.Handler { return http.FileServer(http.Dir(root)) } @@ -91,16 +93,12 @@ func (s *FileServer) Start(parent task.Parent) gperr.Error { } if s.UseHealthCheck() { - s.HealthMon = monitor.NewFileServerHealthMonitor(s.HealthCheck, s.Root) + s.HealthMon = monitor.NewMonitor(s) if err := s.HealthMon.Start(s.task); err != nil { return err } } - if s.ShouldExclude() { - return nil - } - routes.HTTP.Add(s) s.task.OnFinished("remove_route_from_http", func() { routes.HTTP.Del(s) @@ -108,6 +106,10 @@ func (s *FileServer) Start(parent task.Parent) gperr.Error { return nil } +func (s *FileServer) RootPath() string { + return s.Root +} + // ServeHTTP implements http.Handler. func (s *FileServer) ServeHTTP(w http.ResponseWriter, req *http.Request) { s.handler.ServeHTTP(w, req) diff --git a/internal/route/provider/provider.go b/internal/route/provider/provider.go index 85dd2da0..aca701bb 100644 --- a/internal/route/provider/provider.go +++ b/internal/route/provider/provider.go @@ -224,10 +224,13 @@ func (p *Provider) startRoute(parent task.Parent, r *route.Route) gperr.Error { p.lockDeleteRoute(r.Alias) return err.Subject(r.Alias) } + p.lockAddRoute(r) - r.Task().OnCancel("remove_route_from_provider", func() { - p.lockDeleteRoute(r.Alias) - }) + if !r.ShouldExclude() { + r.Task().OnCancel("remove_route_from_provider", func() { + p.lockDeleteRoute(r.Alias) + }) + } return nil } diff --git a/internal/route/reverse_proxy.go b/internal/route/reverse_proxy.go index aaeaed90..fba3bc4e 100755 --- a/internal/route/reverse_proxy.go +++ b/internal/route/reverse_proxy.go @@ -137,10 +137,6 @@ func (r *ReveseProxyRoute) Start(parent task.Parent) gperr.Error { } } - if r.ShouldExclude() { - return nil - } - if r.UseLoadBalance() { r.addToLoadBalancer(parent) } else { diff --git a/internal/route/route.go b/internal/route/route.go index b618f0c8..83841a37 100644 --- a/internal/route/route.go +++ b/internal/route/route.go @@ -379,7 +379,8 @@ func (r *Route) start(parent task.Parent) gperr.Error { defer close(r.started) // skip checking for excluded routes - if !r.ShouldExclude() { + excluded := r.ShouldExclude() + if !excluded { if err := checkExists(r); err != nil { return err } @@ -389,8 +390,10 @@ func (r *Route) start(parent task.Parent) gperr.Error { docker.SetDockerHostByContainerID(cont.ContainerID, cont.DockerHost) } - if err := r.impl.Start(parent); err != nil { - return err + if !excluded { + if err := r.impl.Start(parent); err != nil { + return err + } } return nil } @@ -578,30 +581,10 @@ func (r *Route) IsZeroPort() bool { } func (r *Route) ShouldExclude() bool { - if r.valErr.Get() != nil { + if r.ExcludedReason != ExcludedReasonNone { return true } - if r.Excluded { - return true - } - if r.Container != nil { - switch { - case r.Container.IsExcluded: - return true - case r.IsZeroPort() && !r.UseIdleWatcher(): - return true - case !r.Container.IsExplicit && docker.IsBlacklisted(r.Container): - return true - case strings.HasPrefix(r.Container.ContainerName, "buildx_"): - return true - } - } else if r.IsZeroPort() && r.Scheme != route.SchemeFileServer { - return true - } - if strings.HasSuffix(r.Alias, "-old") { - return true - } - return false + return r.findExcludedReason() != ExcludedReasonNone } type ExcludedReason uint8 diff --git a/internal/route/stream.go b/internal/route/stream.go index a1f98f0c..0c38ef74 100755 --- a/internal/route/stream.go +++ b/internal/route/stream.go @@ -69,10 +69,6 @@ func (r *StreamRoute) Start(parent task.Parent) gperr.Error { } } - if r.ShouldExclude() { - return nil - } - r.ListenAndServe(r.task.Context(), nil, nil) r.l = r.l.With().Stringer("rurl", r.ProxyURL).Stringer("laddr", r.LocalAddr()).Logger() r.l.Info().Msg("stream started") diff --git a/internal/types/routes.go b/internal/types/routes.go index 1609c490..cc9d482d 100644 --- a/internal/types/routes.go +++ b/internal/types/routes.go @@ -52,6 +52,10 @@ type ( HTTPRoute ReverseProxy() *reverseproxy.ReverseProxy } + FileServerRoute interface { + HTTPRoute + RootPath() string + } StreamRoute interface { Route nettypes.Stream diff --git a/internal/watcher/health/monitor/monitor.go b/internal/watcher/health/monitor/monitor.go index f1b29556..e1fbf908 100644 --- a/internal/watcher/health/monitor/monitor.go +++ b/internal/watcher/health/monitor/monitor.go @@ -51,8 +51,10 @@ func NewMonitor(r types.Route) types.HealthMonCheck { mon = NewAgentProxiedMonitor(r.GetAgent(), r.HealthCheckConfig(), AgentTargetFromURL(&r.TargetURL().URL)) } else { switch r := r.(type) { - case types.HTTPRoute: + case types.ReverseProxyRoute: mon = NewHTTPHealthMonitor(&r.TargetURL().URL, r.HealthCheckConfig()) + case types.FileServerRoute: + mon = NewFileServerHealthMonitor(r.HealthCheckConfig(), r.RootPath()) case types.StreamRoute: mon = NewRawHealthMonitor(&r.TargetURL().URL, r.HealthCheckConfig()) default: @@ -71,15 +73,9 @@ func NewMonitor(r types.Route) types.HealthMonCheck { return mon } -func newMonitor(u *url.URL, config *types.HealthCheckConfig, healthCheckFunc HealthCheckFunc) *monitor { - if config.Retries == 0 { - if config.Interval == 0 { - config.Interval = common.HealthCheckIntervalDefault - } - config.Retries = int64(common.HealthCheckDownNotifyDelayDefault / config.Interval) - } + cfg.ApplyDefaults(config.DefaultConfig().Defaults.HealthCheck) mon := &monitor{ - config: config, + config: cfg, checkHealth: healthCheckFunc, startTime: time.Now(), notifyFunc: notif.Notify,