mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-20 12:42:34 +02:00
refactor and properly set idlewaker error in JSON output
This commit is contained in:
parent
2a54aed135
commit
b1f72620dc
3 changed files with 20 additions and 12 deletions
|
@ -22,12 +22,12 @@ type (
|
||||||
waker struct {
|
waker struct {
|
||||||
_ U.NoCopy
|
_ U.NoCopy
|
||||||
|
|
||||||
rp *reverseproxy.ReverseProxy
|
rp *reverseproxy.ReverseProxy
|
||||||
stream net.Stream
|
stream net.Stream
|
||||||
hc health.HealthChecker
|
hc health.HealthChecker
|
||||||
metric *metrics.Gauge
|
metric *metrics.Gauge
|
||||||
|
lastErr error
|
||||||
ready atomic.Bool
|
ready atomic.Bool
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -54,7 +54,7 @@ func newWaker(parent task.Parent, route route.Route, rp *reverseproxy.ReversePro
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case route.IsAgent():
|
case route.IsAgent():
|
||||||
waker.hc = monitor.NewAgentRouteMonitor(route.Agent(), hcCfg, monitor.AgentTargetFromURL(route.TargetURL()))
|
waker.hc = monitor.NewAgentProxiedMonitor(route.Agent(), hcCfg, monitor.AgentTargetFromURL(route.TargetURL()))
|
||||||
case rp != nil:
|
case rp != nil:
|
||||||
waker.hc = monitor.NewHTTPHealthChecker(route.TargetURL(), hcCfg)
|
waker.hc = monitor.NewHTTPHealthChecker(route.TargetURL(), hcCfg)
|
||||||
case stream != nil:
|
case stream != nil:
|
||||||
|
@ -145,12 +145,15 @@ func (w *Watcher) getStatusUpdateReady() health.Status {
|
||||||
result, err := w.hc.CheckHealth()
|
result, err := w.hc.CheckHealth()
|
||||||
switch {
|
switch {
|
||||||
case err != nil:
|
case err != nil:
|
||||||
|
w.lastErr = err
|
||||||
w.ready.Store(false)
|
w.ready.Store(false)
|
||||||
return health.StatusError
|
return health.StatusError
|
||||||
case result.Healthy:
|
case result.Healthy:
|
||||||
|
w.lastErr = nil
|
||||||
w.ready.Store(true)
|
w.ready.Store(true)
|
||||||
return health.StatusHealthy
|
return health.StatusHealthy
|
||||||
default:
|
default:
|
||||||
|
w.lastErr = nil
|
||||||
return health.StatusStarting
|
return health.StatusStarting
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -161,10 +164,15 @@ func (w *Watcher) MarshalJSON() ([]byte, error) {
|
||||||
if w.hc.URL().Port() != "0" {
|
if w.hc.URL().Port() != "0" {
|
||||||
url = w.hc.URL()
|
url = w.hc.URL()
|
||||||
}
|
}
|
||||||
|
var detail string
|
||||||
|
if w.lastErr != nil {
|
||||||
|
detail = w.lastErr.Error()
|
||||||
|
}
|
||||||
return (&monitor.JSONRepresentation{
|
return (&monitor.JSONRepresentation{
|
||||||
Name: w.Name(),
|
Name: w.Name(),
|
||||||
Status: w.Status(),
|
Status: w.Status(),
|
||||||
Config: w.hc.Config(),
|
Config: w.hc.Config(),
|
||||||
URL: url,
|
URL: url,
|
||||||
|
Detail: detail,
|
||||||
}).MarshalJSON()
|
}).MarshalJSON()
|
||||||
}
|
}
|
||||||
|
|
|
@ -207,7 +207,7 @@ func (r *ReveseProxyRoute) newHealthMonitor() interface {
|
||||||
} {
|
} {
|
||||||
if a := r.Agent(); a != nil {
|
if a := r.Agent(); a != nil {
|
||||||
target := monitor.AgentTargetFromURL(r.ProxyURL)
|
target := monitor.AgentTargetFromURL(r.ProxyURL)
|
||||||
return monitor.NewAgentRouteMonitor(a, r.HealthCheck, target)
|
return monitor.NewAgentProxiedMonitor(a, r.HealthCheck, target)
|
||||||
}
|
}
|
||||||
return monitor.NewHTTPHealthMonitor(r.ProxyURL, r.HealthCheck)
|
return monitor.NewHTTPHealthMonitor(r.ProxyURL, r.HealthCheck)
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
AgentRouteMonior struct {
|
AgentProxiedMonitor struct {
|
||||||
agent *agentPkg.AgentConfig
|
agent *agentPkg.AgentConfig
|
||||||
endpointURL string
|
endpointURL string
|
||||||
*monitor
|
*monitor
|
||||||
|
@ -48,8 +48,8 @@ func (target *AgentCheckHealthTarget) displayURL() *types.URL {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAgentRouteMonitor(agent *agentPkg.AgentConfig, config *health.HealthCheckConfig, target *AgentCheckHealthTarget) *AgentRouteMonior {
|
func NewAgentProxiedMonitor(agent *agentPkg.AgentConfig, config *health.HealthCheckConfig, target *AgentCheckHealthTarget) *AgentProxiedMonitor {
|
||||||
mon := &AgentRouteMonior{
|
mon := &AgentProxiedMonitor{
|
||||||
agent: agent,
|
agent: agent,
|
||||||
endpointURL: agentPkg.EndpointHealth + "?" + target.buildQuery(),
|
endpointURL: agentPkg.EndpointHealth + "?" + target.buildQuery(),
|
||||||
}
|
}
|
||||||
|
@ -57,7 +57,7 @@ func NewAgentRouteMonitor(agent *agentPkg.AgentConfig, config *health.HealthChec
|
||||||
return mon
|
return mon
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mon *AgentRouteMonior) CheckHealth() (result *health.HealthCheckResult, err error) {
|
func (mon *AgentProxiedMonitor) CheckHealth() (result *health.HealthCheckResult, err error) {
|
||||||
result = new(health.HealthCheckResult)
|
result = new(health.HealthCheckResult)
|
||||||
ctx, cancel := mon.ContextWithTimeout("timeout querying agent")
|
ctx, cancel := mon.ContextWithTimeout("timeout querying agent")
|
||||||
defer cancel()
|
defer cancel()
|
Loading…
Add table
Reference in a new issue