mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-20 07:21:26 +02:00
refactor health module
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
@@ -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")
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user