refactor(routes): simplify route exclusion check and health check defaults

This commit is contained in:
yusing
2025-12-04 15:12:35 +08:00
parent 09b514393d
commit 48627753d6
7 changed files with 30 additions and 50 deletions

View File

@@ -24,6 +24,8 @@ type (
} }
) )
var _ types.FileServerRoute = (*FileServer)(nil)
func handler(root string) http.Handler { func handler(root string) http.Handler {
return http.FileServer(http.Dir(root)) return http.FileServer(http.Dir(root))
} }
@@ -91,16 +93,12 @@ func (s *FileServer) Start(parent task.Parent) gperr.Error {
} }
if s.UseHealthCheck() { 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 { if err := s.HealthMon.Start(s.task); err != nil {
return err return err
} }
} }
if s.ShouldExclude() {
return nil
}
routes.HTTP.Add(s) routes.HTTP.Add(s)
s.task.OnFinished("remove_route_from_http", func() { s.task.OnFinished("remove_route_from_http", func() {
routes.HTTP.Del(s) routes.HTTP.Del(s)
@@ -108,6 +106,10 @@ func (s *FileServer) Start(parent task.Parent) gperr.Error {
return nil return nil
} }
func (s *FileServer) RootPath() string {
return s.Root
}
// ServeHTTP implements http.Handler. // ServeHTTP implements http.Handler.
func (s *FileServer) ServeHTTP(w http.ResponseWriter, req *http.Request) { func (s *FileServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
s.handler.ServeHTTP(w, req) s.handler.ServeHTTP(w, req)

View File

@@ -224,10 +224,13 @@ func (p *Provider) startRoute(parent task.Parent, r *route.Route) gperr.Error {
p.lockDeleteRoute(r.Alias) p.lockDeleteRoute(r.Alias)
return err.Subject(r.Alias) return err.Subject(r.Alias)
} }
p.lockAddRoute(r) p.lockAddRoute(r)
r.Task().OnCancel("remove_route_from_provider", func() { if !r.ShouldExclude() {
p.lockDeleteRoute(r.Alias) r.Task().OnCancel("remove_route_from_provider", func() {
}) p.lockDeleteRoute(r.Alias)
})
}
return nil return nil
} }

View File

@@ -137,10 +137,6 @@ func (r *ReveseProxyRoute) Start(parent task.Parent) gperr.Error {
} }
} }
if r.ShouldExclude() {
return nil
}
if r.UseLoadBalance() { if r.UseLoadBalance() {
r.addToLoadBalancer(parent) r.addToLoadBalancer(parent)
} else { } else {

View File

@@ -378,7 +378,8 @@ func (r *Route) start(parent task.Parent) gperr.Error {
defer close(r.started) defer close(r.started)
// skip checking for excluded routes // skip checking for excluded routes
if !r.ShouldExclude() { excluded := r.ShouldExclude()
if !excluded {
if err := checkExists(r); err != nil { if err := checkExists(r); err != nil {
return err return err
} }
@@ -388,8 +389,10 @@ func (r *Route) start(parent task.Parent) gperr.Error {
docker.SetDockerHostByContainerID(cont.ContainerID, cont.DockerHost) docker.SetDockerHostByContainerID(cont.ContainerID, cont.DockerHost)
} }
if err := r.impl.Start(parent); err != nil { if !excluded {
return err if err := r.impl.Start(parent); err != nil {
return err
}
} }
return nil return nil
} }
@@ -577,30 +580,10 @@ func (r *Route) IsZeroPort() bool {
} }
func (r *Route) ShouldExclude() bool { func (r *Route) ShouldExclude() bool {
if r.valErr.Get() != nil { if r.ExcludedReason != ExcludedReasonNone {
return true return true
} }
if r.Excluded { return r.findExcludedReason() != ExcludedReasonNone
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
} }
type ExcludedReason uint8 type ExcludedReason uint8

View File

@@ -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.ListenAndServe(r.task.Context(), nil, nil)
r.l = r.l.With().Stringer("rurl", r.ProxyURL).Stringer("laddr", r.LocalAddr()).Logger() r.l = r.l.With().Stringer("rurl", r.ProxyURL).Stringer("laddr", r.LocalAddr()).Logger()
r.l.Info().Msg("stream started") r.l.Info().Msg("stream started")

View File

@@ -52,6 +52,10 @@ type (
HTTPRoute HTTPRoute
ReverseProxy() *reverseproxy.ReverseProxy ReverseProxy() *reverseproxy.ReverseProxy
} }
FileServerRoute interface {
HTTPRoute
RootPath() string
}
StreamRoute interface { StreamRoute interface {
Route Route
nettypes.Stream nettypes.Stream

View File

@@ -51,8 +51,10 @@ func NewMonitor(r types.Route) types.HealthMonCheck {
mon = NewAgentProxiedMonitor(r.GetAgent(), r.HealthCheckConfig(), AgentTargetFromURL(&r.TargetURL().URL)) mon = NewAgentProxiedMonitor(r.GetAgent(), r.HealthCheckConfig(), AgentTargetFromURL(&r.TargetURL().URL))
} else { } else {
switch r := r.(type) { switch r := r.(type) {
case types.HTTPRoute: case types.ReverseProxyRoute:
mon = NewHTTPHealthMonitor(&r.TargetURL().URL, r.HealthCheckConfig()) mon = NewHTTPHealthMonitor(&r.TargetURL().URL, r.HealthCheckConfig())
case types.FileServerRoute:
mon = NewFileServerHealthMonitor(r.HealthCheckConfig(), r.RootPath())
case types.StreamRoute: case types.StreamRoute:
mon = NewRawHealthMonitor(&r.TargetURL().URL, r.HealthCheckConfig()) mon = NewRawHealthMonitor(&r.TargetURL().URL, r.HealthCheckConfig())
default: default:
@@ -71,15 +73,9 @@ func NewMonitor(r types.Route) types.HealthMonCheck {
return mon return mon
} }
func newMonitor(u *url.URL, config *types.HealthCheckConfig, healthCheckFunc HealthCheckFunc) *monitor { cfg.ApplyDefaults(config.DefaultConfig().Defaults.HealthCheck)
if config.Retries == 0 {
if config.Interval == 0 {
config.Interval = common.HealthCheckIntervalDefault
}
config.Retries = int64(common.HealthCheckDownNotifyDelayDefault / config.Interval)
}
mon := &monitor{ mon := &monitor{
config: config, config: cfg,
checkHealth: healthCheckFunc, checkHealth: healthCheckFunc,
startTime: time.Now(), startTime: time.Now(),
notifyFunc: notif.Notify, notifyFunc: notif.Notify,