yanic/runtime/nodes_ping.go

40 lines
977 B
Go
Raw Permalink Normal View History

2019-01-19 17:45:59 +00:00
package runtime
import (
2019-01-20 00:28:57 +00:00
"net"
2019-01-19 17:45:59 +00: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 00:28:57 +00:00
var addr *net.IPAddr
2019-01-19 20:59:43 +00:00
if node.Address != nil {
2019-01-20 00:28:57 +00:00
addr = &net.IPAddr{IP:node.Address.IP, Zone: node.Address.Zone}
2019-01-19 20:59:43 +00:00
} else {
2019-01-19 17:45:59 +00:00
logNode.Debug("error no address found")
2019-01-19 20:59:43 +00:00
if node.Nodeinfo != nil {
2019-01-20 00:28:57 +00: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 20:59:43 +00:00
}
}
}
2019-01-19 17:45:59 +00:00
}
2019-01-20 00:28:57 +00:00
logAddr := logNode.WithField("addr", addr.String())
_, err := nodes.pinger.PingAttempts(addr, nodes.config.PingTimeout.Duration, nodes.config.PingCount)
2019-01-19 17:45:59 +00:00
logAddr.WithFields(map[string]interface{}{
2019-01-20 00:28:57 +00:00
"success": err == nil,
2019-01-19 17:45:59 +00:00
}).Debug("pong")
2019-01-20 00:28:57 +00:00
return err == nil
2019-01-19 17:45:59 +00:00
}