yanic/runtime/nodes_ping.go

40 lines
977 B
Go
Raw Normal View History

2019-01-19 18:45:59 +01:00
package runtime
import (
2019-01-20 01:28:57 +01:00
"net"
2019-01-19 18:45:59 +01:00
"github.com/bdlm/log"
)
func (nodes *Nodes) ping(node *Node) bool {
logNode := log.WithField("node_id", "unknown")
if node.Nodeinfo != nil {
logNode = logNode.WithField("node_id", node.Nodeinfo.NodeID)
}
2019-01-20 01:28:57 +01:00
var addr *net.IPAddr
2019-01-19 21:59:43 +01:00
if node.Address != nil {
2019-01-20 01:28:57 +01:00
addr = &net.IPAddr{IP:node.Address.IP, Zone: node.Address.Zone}
2019-01-19 21:59:43 +01:00
} else {
2019-01-19 18:45:59 +01:00
logNode.Debug("error no address found")
2019-01-19 21:59:43 +01:00
if node.Nodeinfo != nil {
2019-01-20 01:28:57 +01:00
for _, addrMaybeString := range node.Nodeinfo.Network.Addresses {
if len(addrMaybeString) >= 5 && addrMaybeString[:5] != "fe80:" {
addrMaybe, err := net.ResolveIPAddr("ip6", addrMaybeString)
if err == nil {
addr = addrMaybe
}
2019-01-19 21:59:43 +01:00
}
}
}
2019-01-19 18:45:59 +01:00
}
2019-01-20 01:28:57 +01:00
logAddr := logNode.WithField("addr", addr.String())
_, err := nodes.pinger.PingAttempts(addr, nodes.config.PingTimeout.Duration, nodes.config.PingCount)
2019-01-19 18:45:59 +01:00
logAddr.WithFields(map[string]interface{}{
2019-01-20 01:28:57 +01:00
"success": err == nil,
2019-01-19 18:45:59 +01:00
}).Debug("pong")
2019-01-20 01:28:57 +01:00
return err == nil
2019-01-19 18:45:59 +01:00
}