refactor: remove net.URL and net.CIDR types, improved unmarshal handling

This commit is contained in:
yusing
2025-04-13 07:06:21 +08:00
parent 1eac48e899
commit fce96ff3be
37 changed files with 236 additions and 292 deletions

View File

@@ -1,34 +1,32 @@
package monitor
package health
import (
"encoding/json"
"net/url"
"strconv"
"time"
net "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 *health.HealthCheckConfig
Status health.Status
Config *HealthCheckConfig
Status Status
Started time.Time
Uptime time.Duration
Latency time.Duration
LastSeen time.Time
Detail string
URL *net.URL
URL *url.URL
Extra map[string]any
}
func (jsonRepr *JSONRepresentation) MarshalJSON() ([]byte, error) {
func (jsonRepr *JSONRepresentation) MarshalMap() map[string]any {
url := jsonRepr.URL.String()
if url == "http://:0" {
url = ""
}
return json.Marshal(map[string]any{
return map[string]any{
"name": jsonRepr.Name,
"config": jsonRepr.Config,
"started": jsonRepr.Started.Unix(),
@@ -43,5 +41,5 @@ func (jsonRepr *JSONRepresentation) MarshalJSON() ([]byte, error) {
"detail": jsonRepr.Detail,
"url": url,
"extra": jsonRepr.Extra,
})
}
}

View File

@@ -7,7 +7,6 @@ import (
"net/url"
agentPkg "github.com/yusing/go-proxy/agent/pkg/agent"
"github.com/yusing/go-proxy/internal/net/types"
"github.com/yusing/go-proxy/internal/watcher/health"
)
@@ -24,7 +23,7 @@ type (
}
)
func AgentTargetFromURL(url *types.URL) *AgentCheckHealthTarget {
func AgentTargetFromURL(url *url.URL) *AgentCheckHealthTarget {
return &AgentCheckHealthTarget{
Scheme: url.Scheme,
Host: url.Host,
@@ -40,12 +39,12 @@ func (target *AgentCheckHealthTarget) buildQuery() string {
return query.Encode()
}
func (target *AgentCheckHealthTarget) displayURL() *types.URL {
return types.NewURL(&url.URL{
func (target *AgentCheckHealthTarget) displayURL() *url.URL {
return &url.URL{
Scheme: target.Scheme,
Host: target.Host,
Path: target.Path,
})
}
}
func NewAgentProxiedMonitor(agent *agentPkg.AgentConfig, config *health.HealthCheckConfig, target *AgentCheckHealthTarget) *AgentProxiedMonitor {

View File

@@ -4,9 +4,9 @@ import (
"crypto/tls"
"errors"
"net/http"
"net/url"
"time"
"github.com/yusing/go-proxy/internal/net/types"
"github.com/yusing/go-proxy/internal/watcher/health"
"github.com/yusing/go-proxy/pkg"
)
@@ -26,7 +26,7 @@ var pinger = &http.Client{
},
}
func NewHTTPHealthMonitor(url *types.URL, config *health.HealthCheckConfig) *HTTPHealthMonitor {
func NewHTTPHealthMonitor(url *url.URL, config *health.HealthCheckConfig) *HTTPHealthMonitor {
mon := new(HTTPHealthMonitor)
mon.monitor = newMonitor(url, config, mon.CheckHealth)
if config.UseGet {

View File

@@ -4,12 +4,12 @@ import (
"context"
"errors"
"fmt"
"net/url"
"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"
@@ -23,7 +23,7 @@ type (
monitor struct {
service string
config *health.HealthCheckConfig
url atomic.Value[*types.URL]
url atomic.Value[*url.URL]
status atomic.Value[health.Status]
lastResult atomic.Value[*health.HealthCheckResult]
@@ -63,7 +63,7 @@ func NewMonitor(r route.Route) health.HealthMonCheck {
return mon
}
func newMonitor(url *types.URL, config *health.HealthCheckConfig, healthCheckFunc HealthCheckFunc) *monitor {
func newMonitor(url *url.URL, config *health.HealthCheckConfig, healthCheckFunc HealthCheckFunc) *monitor {
mon := &monitor{
config: config,
checkHealth: healthCheckFunc,
@@ -135,12 +135,12 @@ func (mon *monitor) Finish(reason any) {
}
// UpdateURL implements HealthChecker.
func (mon *monitor) UpdateURL(url *types.URL) {
func (mon *monitor) UpdateURL(url *url.URL) {
mon.url.Store(url)
}
// URL implements HealthChecker.
func (mon *monitor) URL() *types.URL {
func (mon *monitor) URL() *url.URL {
return mon.url.Load()
}
@@ -179,8 +179,8 @@ func (mon *monitor) String() string {
return mon.Name()
}
// MarshalJSON implements json.Marshaler of HealthMonitor.
func (mon *monitor) MarshalJSON() ([]byte, error) {
// MarshalMap implements health.HealthMonitor.
func (mon *monitor) MarshalMap() map[string]any {
res := mon.lastResult.Load()
if res == nil {
res = &health.HealthCheckResult{
@@ -188,7 +188,7 @@ func (mon *monitor) MarshalJSON() ([]byte, error) {
}
}
return (&JSONRepresentation{
return (&health.JSONRepresentation{
Name: mon.service,
Config: mon.config,
Status: mon.status.Load(),
@@ -198,7 +198,7 @@ func (mon *monitor) MarshalJSON() ([]byte, error) {
LastSeen: GetLastSeen(mon.service),
Detail: res.Detail,
URL: mon.url.Load(),
}).MarshalJSON()
}).MarshalMap()
}
func (mon *monitor) checkUpdateHealth() error {

View File

@@ -2,9 +2,9 @@ package monitor
import (
"net"
"net/url"
"time"
"github.com/yusing/go-proxy/internal/net/types"
"github.com/yusing/go-proxy/internal/watcher/health"
)
@@ -15,7 +15,7 @@ type (
}
)
func NewRawHealthMonitor(url *types.URL, config *health.HealthCheckConfig) *RawHealthMonitor {
func NewRawHealthMonitor(url *url.URL, config *health.HealthCheckConfig) *RawHealthMonitor {
mon := new(RawHealthMonitor)
mon.monitor = newMonitor(url, config, mon.CheckHealth)
mon.dialer = &net.Dialer{

View File

@@ -1,12 +1,12 @@
package health
import (
"encoding/json"
"fmt"
"net/url"
"time"
"github.com/yusing/go-proxy/internal/net/types"
"github.com/yusing/go-proxy/internal/task"
"github.com/yusing/go-proxy/internal/utils"
)
type (
@@ -24,15 +24,15 @@ type (
task.TaskStarter
task.TaskFinisher
fmt.Stringer
json.Marshaler
utils.MapMarshaller
WithHealthInfo
Name() string
}
HealthChecker interface {
CheckHealth() (result *HealthCheckResult, err error)
URL() *types.URL
URL() *url.URL
Config() *HealthCheckConfig
UpdateURL(url *types.URL)
UpdateURL(url *url.URL)
}
HealthMonCheck interface {
HealthMonitor