mirror of
https://github.com/yusing/godoxy.git
synced 2026-01-11 22:30:47 +01:00
Moved non-agent-specific logic from agent/pkg/agent/ to internal/agentpool/: - pool.go: Agent pool management (Get, Add, Remove, List, Iter, etc.) - http_requests.go: HTTP utilities (health checks, forwarding, websockets, reverse proxy) - agent.go: Agent struct with HTTP client management This separates general-purpose pool management from agent-specific configuration, improving code organization and making the agent package focused on agent config only.
72 lines
1.6 KiB
Go
72 lines
1.6 KiB
Go
package monitor
|
|
|
|
import (
|
|
"net/url"
|
|
|
|
"github.com/yusing/godoxy/internal/agentpool"
|
|
"github.com/yusing/godoxy/internal/types"
|
|
"github.com/yusing/goutils/synk"
|
|
)
|
|
|
|
type (
|
|
AgentProxiedMonitor struct {
|
|
agent *agentpool.Agent
|
|
query synk.Value[string]
|
|
*monitor
|
|
}
|
|
AgentCheckHealthTarget struct {
|
|
Scheme string
|
|
Host string
|
|
Path string
|
|
}
|
|
)
|
|
|
|
func AgentTargetFromURL(url *url.URL) *AgentCheckHealthTarget {
|
|
return &AgentCheckHealthTarget{
|
|
Scheme: url.Scheme,
|
|
Host: url.Host,
|
|
Path: url.Path,
|
|
}
|
|
}
|
|
|
|
func (target *AgentCheckHealthTarget) buildQuery() string {
|
|
query := make(url.Values, 3)
|
|
query.Set("scheme", target.Scheme)
|
|
query.Set("host", target.Host)
|
|
query.Set("path", target.Path)
|
|
return query.Encode()
|
|
}
|
|
|
|
func (target *AgentCheckHealthTarget) displayURL() *url.URL {
|
|
return &url.URL{
|
|
Scheme: target.Scheme,
|
|
Host: target.Host,
|
|
Path: target.Path,
|
|
}
|
|
}
|
|
|
|
func NewAgentProxiedMonitor(agent *agentpool.Agent, config types.HealthCheckConfig, target *AgentCheckHealthTarget) *AgentProxiedMonitor {
|
|
mon := &AgentProxiedMonitor{
|
|
agent: agent,
|
|
}
|
|
mon.monitor = newMonitor(target.displayURL(), config, mon.CheckHealth)
|
|
mon.query.Store(target.buildQuery())
|
|
return mon
|
|
}
|
|
|
|
func (mon *AgentProxiedMonitor) CheckHealth() (types.HealthCheckResult, error) {
|
|
resp, err := mon.agent.DoHealthCheck(mon.config.Timeout, mon.query.Load())
|
|
result := types.HealthCheckResult{
|
|
Healthy: resp.Healthy,
|
|
Detail: resp.Detail,
|
|
Latency: resp.Latency,
|
|
}
|
|
return result, err
|
|
}
|
|
|
|
func (mon *AgentProxiedMonitor) UpdateURL(url *url.URL) {
|
|
mon.monitor.UpdateURL(url)
|
|
newTarget := AgentTargetFromURL(url)
|
|
mon.query.Store(newTarget.buildQuery())
|
|
}
|