diff --git a/internal/docker/idlewatcher/waker.go b/internal/docker/idlewatcher/waker.go index b734b64..49b2f07 100644 --- a/internal/docker/idlewatcher/waker.go +++ b/internal/docker/idlewatcher/waker.go @@ -15,6 +15,7 @@ import ( "github.com/yusing/go-proxy/internal/task" U "github.com/yusing/go-proxy/internal/utils" "github.com/yusing/go-proxy/internal/watcher/health" + "github.com/yusing/go-proxy/internal/watcher/health/monitor" ) type waker struct { @@ -51,9 +52,9 @@ func newWaker(providerSubTask task.Task, entry entry.Entry, rp *gphttp.ReversePr switch { case rp != nil: - waker.hc = health.NewHTTPHealthChecker(entry.TargetURL(), hcCfg) + waker.hc = monitor.NewHTTPHealthChecker(entry.TargetURL(), hcCfg) case stream != nil: - waker.hc = health.NewRawHealthChecker(entry.TargetURL(), hcCfg) + waker.hc = monitor.NewRawHealthChecker(entry.TargetURL(), hcCfg) default: panic("both nil") } @@ -147,7 +148,7 @@ func (w *Watcher) MarshalJSON() ([]byte, error) { if w.hc.URL().Port() != "0" { url = w.hc.URL() } - return (&health.JSONRepresentation{ + return (&monitor.JSONRepresentation{ Name: w.Name(), Status: w.Status(), Config: w.hc.Config(), diff --git a/internal/net/http/loadbalancer/loadbalancer.go b/internal/net/http/loadbalancer/loadbalancer.go index e8f38fe..c7cb836 100644 --- a/internal/net/http/loadbalancer/loadbalancer.go +++ b/internal/net/http/loadbalancer/loadbalancer.go @@ -12,6 +12,7 @@ import ( "github.com/yusing/go-proxy/internal/net/http/middleware" "github.com/yusing/go-proxy/internal/task" "github.com/yusing/go-proxy/internal/watcher/health" + "github.com/yusing/go-proxy/internal/watcher/health/monitor" ) // TODO: stats of each server. @@ -248,7 +249,7 @@ func (lb *LoadBalancer) MarshalJSON() ([]byte, error) { extra[v.Name] = v.healthMon }) - return (&health.JSONRepresentation{ + return (&monitor.JSONRepresentation{ Name: lb.Name(), Status: lb.Status(), Started: lb.startTime, diff --git a/internal/route/http.go b/internal/route/http.go index 1d34edc..d9ce305 100755 --- a/internal/route/http.go +++ b/internal/route/http.go @@ -19,6 +19,7 @@ import ( "github.com/yusing/go-proxy/internal/task" F "github.com/yusing/go-proxy/internal/utils/functional" "github.com/yusing/go-proxy/internal/watcher/health" + "github.com/yusing/go-proxy/internal/watcher/health/monitor" ) type ( @@ -111,7 +112,7 @@ func (r *HTTPRoute) Start(providerSubtask task.Task) E.Error { r.handler = waker r.HealthMon = waker case entry.UseHealthCheck(r): - r.HealthMon = health.NewHTTPHealthMonitor(r.rp.TargetURL, r.HealthCheck) + r.HealthMon = monitor.NewHTTPHealthMonitor(r.rp.TargetURL, r.HealthCheck) } if r.handler == nil { diff --git a/internal/route/stream.go b/internal/route/stream.go index 76cbd41..4884ae7 100755 --- a/internal/route/stream.go +++ b/internal/route/stream.go @@ -13,6 +13,7 @@ import ( "github.com/yusing/go-proxy/internal/task" F "github.com/yusing/go-proxy/internal/utils/functional" "github.com/yusing/go-proxy/internal/watcher/health" + "github.com/yusing/go-proxy/internal/watcher/health/monitor" ) type StreamRoute struct { @@ -72,7 +73,7 @@ func (r *StreamRoute) Start(providerSubtask task.Task) E.Error { r.stream = waker r.HealthMon = waker case entry.UseHealthCheck(r): - r.HealthMon = health.NewRawHealthMonitor(r.TargetURL(), r.HealthCheck) + r.HealthMon = monitor.NewRawHealthMonitor(r.TargetURL(), r.HealthCheck) } if err := r.stream.Setup(); err != nil { diff --git a/internal/watcher/health/healthcheck_config.go b/internal/watcher/health/config.go similarity index 100% rename from internal/watcher/health/healthcheck_config.go rename to internal/watcher/health/config.go diff --git a/internal/watcher/health/http.go b/internal/watcher/health/monitor/http.go similarity index 84% rename from internal/watcher/health/http.go rename to internal/watcher/health/monitor/http.go index fca995a..c025f5c 100644 --- a/internal/watcher/health/http.go +++ b/internal/watcher/health/monitor/http.go @@ -1,4 +1,4 @@ -package health +package monitor import ( "crypto/tls" @@ -6,6 +6,7 @@ import ( "net/http" "github.com/yusing/go-proxy/internal/net/types" + "github.com/yusing/go-proxy/internal/watcher/health" "github.com/yusing/go-proxy/pkg" ) @@ -24,7 +25,7 @@ var pinger = &http.Client{ }, } -func NewHTTPHealthMonitor(url types.URL, config *HealthCheckConfig) *HTTPHealthMonitor { +func NewHTTPHealthMonitor(url types.URL, config *health.HealthCheckConfig) *HTTPHealthMonitor { mon := new(HTTPHealthMonitor) mon.monitor = newMonitor(url, config, mon.CheckHealth) if config.UseGet { @@ -35,7 +36,7 @@ func NewHTTPHealthMonitor(url types.URL, config *HealthCheckConfig) *HTTPHealthM return mon } -func NewHTTPHealthChecker(url types.URL, config *HealthCheckConfig) HealthChecker { +func NewHTTPHealthChecker(url types.URL, config *health.HealthCheckConfig) health.HealthChecker { return NewHTTPHealthMonitor(url, config) } diff --git a/internal/watcher/health/json.go b/internal/watcher/health/monitor/json.go similarity index 86% rename from internal/watcher/health/json.go rename to internal/watcher/health/monitor/json.go index 9123423..cce8c15 100644 --- a/internal/watcher/health/json.go +++ b/internal/watcher/health/monitor/json.go @@ -1,4 +1,4 @@ -package health +package monitor import ( "encoding/json" @@ -6,12 +6,13 @@ import ( "github.com/yusing/go-proxy/internal/net/types" "github.com/yusing/go-proxy/internal/utils/strutils" + "github.com/yusing/go-proxy/internal/watcher/health" ) type JSONRepresentation struct { Name string - Config *HealthCheckConfig - Status Status + Config *health.HealthCheckConfig + Status health.Status Started time.Time Uptime time.Duration URL types.URL diff --git a/internal/watcher/health/monitor.go b/internal/watcher/health/monitor/monitor.go similarity index 85% rename from internal/watcher/health/monitor.go rename to internal/watcher/health/monitor/monitor.go index 7116d0e..309a748 100644 --- a/internal/watcher/health/monitor.go +++ b/internal/watcher/health/monitor/monitor.go @@ -1,4 +1,4 @@ -package health +package monitor import ( "context" @@ -16,16 +16,17 @@ import ( "github.com/yusing/go-proxy/internal/notif" "github.com/yusing/go-proxy/internal/task" U "github.com/yusing/go-proxy/internal/utils" + "github.com/yusing/go-proxy/internal/watcher/health" ) type ( HealthCheckFunc func() (healthy bool, detail string, err error) monitor struct { service string - config *HealthCheckConfig + config *health.HealthCheckConfig url U.AtomicValue[types.URL] - status U.AtomicValue[Status] + status U.AtomicValue[health.Status] checkHealth HealthCheckFunc startTime time.Time @@ -37,14 +38,14 @@ type ( var ErrNegativeInterval = errors.New("negative interval") -func newMonitor(url types.URL, config *HealthCheckConfig, healthCheckFunc HealthCheckFunc) *monitor { +func newMonitor(url types.URL, config *health.HealthCheckConfig, healthCheckFunc HealthCheckFunc) *monitor { mon := &monitor{ config: config, checkHealth: healthCheckFunc, startTime: time.Now(), } mon.url.Store(url) - mon.status.Store(StatusHealthy) + mon.status.Store(health.StatusHealthy) return mon } @@ -72,8 +73,8 @@ func (mon *monitor) Start(routeSubtask task.Task) E.Error { logger := logging.With().Str("name", mon.service).Logger() defer func() { - if mon.status.Load() != StatusError { - mon.status.Store(StatusUnknown) + if mon.status.Load() != health.StatusError { + mon.status.Store(health.StatusUnknown) } mon.task.Finish(nil) if mon.metric != nil { @@ -121,12 +122,12 @@ func (mon *monitor) URL() types.URL { } // Config implements HealthChecker. -func (mon *monitor) Config() *HealthCheckConfig { +func (mon *monitor) Config() *health.HealthCheckConfig { return mon.config } // Status implements HealthMonitor. -func (mon *monitor) Status() Status { +func (mon *monitor) Status() health.Status { return mon.status.Load() } @@ -163,19 +164,19 @@ func (mon *monitor) checkUpdateHealth() error { healthy, detail, err := mon.checkHealth() if err != nil { defer mon.task.Finish(err) - mon.status.Store(StatusError) + mon.status.Store(health.StatusError) if !errors.Is(err, context.Canceled) { return fmt.Errorf("check health: %w", err) } return nil } - var status Status + var status health.Status if healthy { - status = StatusHealthy + status = health.StatusHealthy } else { - status = StatusUnhealthy + status = health.StatusUnhealthy } - if healthy != (mon.status.Swap(status) == StatusHealthy) { + if healthy != (mon.status.Swap(status) == health.StatusHealthy) { if healthy { logger.Info().Msg("server is up") notif.Notify(mon.service, "server is up") diff --git a/internal/watcher/health/raw.go b/internal/watcher/health/monitor/raw.go similarity index 74% rename from internal/watcher/health/raw.go rename to internal/watcher/health/monitor/raw.go index 588cd76..e0dd7f6 100644 --- a/internal/watcher/health/raw.go +++ b/internal/watcher/health/monitor/raw.go @@ -1,9 +1,10 @@ -package health +package monitor import ( "net" "github.com/yusing/go-proxy/internal/net/types" + "github.com/yusing/go-proxy/internal/watcher/health" ) type ( @@ -13,7 +14,7 @@ type ( } ) -func NewRawHealthMonitor(url types.URL, config *HealthCheckConfig) *RawHealthMonitor { +func NewRawHealthMonitor(url types.URL, config *health.HealthCheckConfig) *RawHealthMonitor { mon := new(RawHealthMonitor) mon.monitor = newMonitor(url, config, mon.CheckHealth) mon.dialer = &net.Dialer{ @@ -23,7 +24,7 @@ func NewRawHealthMonitor(url types.URL, config *HealthCheckConfig) *RawHealthMon return mon } -func NewRawHealthChecker(url types.URL, config *HealthCheckConfig) HealthChecker { +func NewRawHealthChecker(url types.URL, config *health.HealthCheckConfig) health.HealthChecker { return NewRawHealthMonitor(url, config) }