mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-10 10:53:36 +02:00
refactor: simplify health monitor code
This commit is contained in:
@@ -37,10 +37,6 @@ func NewHTTPHealthMonitor(url *types.URL, config *health.HealthCheckConfig) *HTT
|
||||
return mon
|
||||
}
|
||||
|
||||
func NewHTTPHealthChecker(url *types.URL, config *health.HealthCheckConfig) health.HealthChecker {
|
||||
return NewHTTPHealthMonitor(url, config)
|
||||
}
|
||||
|
||||
func (mon *HTTPHealthMonitor) CheckHealth() (result *health.HealthCheckResult, err error) {
|
||||
ctx, cancel := mon.ContextWithTimeout("ping request timed out")
|
||||
defer cancel()
|
||||
|
||||
@@ -6,10 +6,12 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/yusing/go-proxy/internal/docker"
|
||||
"github.com/yusing/go-proxy/internal/gperr"
|
||||
"github.com/yusing/go-proxy/internal/logging"
|
||||
"github.com/yusing/go-proxy/internal/net/types"
|
||||
"github.com/yusing/go-proxy/internal/notif"
|
||||
route "github.com/yusing/go-proxy/internal/route/types"
|
||||
"github.com/yusing/go-proxy/internal/task"
|
||||
"github.com/yusing/go-proxy/internal/utils/atomic"
|
||||
"github.com/yusing/go-proxy/internal/utils/strutils"
|
||||
@@ -35,6 +37,32 @@ type (
|
||||
|
||||
var ErrNegativeInterval = errors.New("negative interval")
|
||||
|
||||
func NewMonitor(r route.Route) health.HealthMonCheck {
|
||||
var mon health.HealthMonCheck
|
||||
if r.IsAgent() {
|
||||
mon = NewAgentProxiedMonitor(r.Agent(), r.HealthCheckConfig(), AgentTargetFromURL(r.TargetURL()))
|
||||
} else {
|
||||
switch r := r.(type) {
|
||||
case route.HTTPRoute:
|
||||
mon = NewHTTPHealthMonitor(r.TargetURL(), r.HealthCheckConfig())
|
||||
case route.StreamRoute:
|
||||
mon = NewRawHealthMonitor(r.TargetURL(), r.HealthCheckConfig())
|
||||
default:
|
||||
logging.Panic().Msgf("unexpected route type: %T", r)
|
||||
}
|
||||
}
|
||||
if r.IsDocker() {
|
||||
cont := r.DockerContainer()
|
||||
client, err := docker.NewClient(cont.DockerHost)
|
||||
if err != nil {
|
||||
return mon
|
||||
}
|
||||
r.Task().OnCancel("close_docker_client", client.Close)
|
||||
return NewDockerHealthMonitor(client, cont.ContainerID, r.TargetName(), r.HealthCheckConfig(), mon)
|
||||
}
|
||||
return mon
|
||||
}
|
||||
|
||||
func newMonitor(url *types.URL, config *health.HealthCheckConfig, healthCheckFunc HealthCheckFunc) *monitor {
|
||||
mon := &monitor{
|
||||
config: config,
|
||||
|
||||
@@ -25,10 +25,6 @@ func NewRawHealthMonitor(url *types.URL, config *health.HealthCheckConfig) *RawH
|
||||
return mon
|
||||
}
|
||||
|
||||
func NewRawHealthChecker(url *types.URL, config *health.HealthCheckConfig) health.HealthChecker {
|
||||
return NewRawHealthMonitor(url, config)
|
||||
}
|
||||
|
||||
func (mon *RawHealthMonitor) CheckHealth() (result *health.HealthCheckResult, err error) {
|
||||
ctx, cancel := mon.ContextWithTimeout("ping request timed out")
|
||||
defer cancel()
|
||||
|
||||
@@ -34,4 +34,8 @@ type (
|
||||
Config() *HealthCheckConfig
|
||||
UpdateURL(url *types.URL)
|
||||
}
|
||||
HealthMonCheck interface {
|
||||
HealthMonitor
|
||||
HealthChecker
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user