接口主备检测

main
wangbin 4 days ago
parent 62a6242dd6
commit bd85502368

@ -1025,26 +1025,24 @@ func FormatTimes(now time.Time, format string) string {
return now.Format(format) return now.Format(format)
} }
func CheckIPReachable(ip string, port int, timeout time.Duration) bool {
conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", ip, port), timeout)
if err != nil {
return false
}
conn.Close()
return true
}
func GetAvailableEndpoint() string { func GetAvailableEndpoint() string {
primaryIP := LoadConfig().SeverData.ServerIP primaryIP := LoadConfig().SeverData.ServerIP
backupIP := LoadConfig().SeverData.BackUpServerIP // 替换为实际备选IP backupIP := LoadConfig().SeverData.BackUpServerIP // 替换为实际备选IP
port := 9800
timeout := 3 * time.Second
if CheckIPReachable(primaryIP, port, timeout) { if IsIPReachable(primaryIP) {
return primaryIP return primaryIP
} }
if CheckIPReachable(backupIP, port, timeout) { if IsIPReachable(backupIP) {
return backupIP return backupIP
} }
return "" return ""
}
func IsIPReachable(ip string) bool {
conn, err := net.DialTimeout("ip4:icmp", ip, 10*time.Millisecond)
if err != nil {
return false
}
conn.Close()
return true
} }

Loading…
Cancel
Save