Files
godoxy-yusing/internal/watcher/health/monitor/raw.go
yusing 35a3e3fef6 refactor(api): restructured API for type safety, maintainability and docs generation
- These changes makes the API incombatible with previous versions
- Added new types for error handling, success responses, and health checks.
- Updated health check logic to utilize the new types for better clarity and structure.
- Refactored existing handlers to improve response consistency and error handling.
- Updated Makefile to include a new target for generating API types from Swagger.
- Updated "new agent" API to respond an encrypted cert pair
2025-08-16 13:04:05 +08:00

44 lines
868 B
Go

package monitor
import (
"net"
"net/url"
"time"
"github.com/yusing/go-proxy/internal/types"
)
type (
RawHealthMonitor struct {
*monitor
dialer *net.Dialer
}
)
func NewRawHealthMonitor(url *url.URL, config *types.HealthCheckConfig) *RawHealthMonitor {
mon := new(RawHealthMonitor)
mon.monitor = newMonitor(url, config, mon.CheckHealth)
mon.dialer = &net.Dialer{
Timeout: config.Timeout,
FallbackDelay: -1,
}
return mon
}
func (mon *RawHealthMonitor) CheckHealth() (*types.HealthCheckResult, error) {
ctx, cancel := mon.ContextWithTimeout("ping request timed out")
defer cancel()
url := mon.url.Load()
start := time.Now()
conn, err := mon.dialer.DialContext(ctx, url.Scheme, url.Host)
if err != nil {
return nil, err
}
defer conn.Close()
return &types.HealthCheckResult{
Latency: time.Since(start),
Healthy: true,
}, nil
}