mirror of
https://github.com/yusing/godoxy.git
synced 2026-01-11 22:30:47 +01:00
refactor(routes): simplify route exclusion check and health check defaults
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -378,7 +378,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
|
||||
}
|
||||
@@ -388,8 +389,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
|
||||
}
|
||||
@@ -577,30 +580,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
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -52,6 +52,10 @@ type (
|
||||
HTTPRoute
|
||||
ReverseProxy() *reverseproxy.ReverseProxy
|
||||
}
|
||||
FileServerRoute interface {
|
||||
HTTPRoute
|
||||
RootPath() string
|
||||
}
|
||||
StreamRoute interface {
|
||||
Route
|
||||
nettypes.Stream
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user