change lib

This commit is contained in:
Martin/Geno 2019-01-20 01:28:57 +01:00
parent 8b75fb1b04
commit 9b6e9eb300
No known key found for this signature in database
GPG Key ID: 9D7D3C6BFF600C6A
2 changed files with 25 additions and 29 deletions

View File

@ -7,6 +7,7 @@ import (
"time" "time"
"github.com/bdlm/log" "github.com/bdlm/log"
ping "github.com/digineo/go-ping"
"github.com/FreifunkBremen/yanic/data" "github.com/FreifunkBremen/yanic/data"
"github.com/FreifunkBremen/yanic/lib/jsontime" "github.com/FreifunkBremen/yanic/lib/jsontime"
@ -17,6 +18,7 @@ type Nodes struct {
List map[string]*Node `json:"nodes"` // the current nodemap, indexed by node ID List map[string]*Node `json:"nodes"` // the current nodemap, indexed by node ID
ifaceToNodeID map[string]string // mapping from MAC address to NodeID ifaceToNodeID map[string]string // mapping from MAC address to NodeID
config *NodesConfig config *NodesConfig
pinger *ping.Pinger
sync.RWMutex sync.RWMutex
} }
@ -27,6 +29,11 @@ func NewNodes(config *NodesConfig) *Nodes {
ifaceToNodeID: make(map[string]string), ifaceToNodeID: make(map[string]string),
config: config, config: config,
} }
p, err := ping.New("", "::")
if err != nil {
log.Warnf("ping bind failed: %s", err)
}
nodes.pinger = p
if config.StatePath != "" { if config.StatePath != "" {
nodes.load() nodes.load()
@ -83,7 +90,7 @@ func (nodes *Nodes) Update(nodeID string, res *data.ResponseData) *Node {
node.Nodeinfo = res.NodeInfo node.Nodeinfo = res.NodeInfo
node.Statistics = res.Statistics node.Statistics = res.Statistics
node.Neighbours = res.Neighbours node.Neighbours = res.Neighbours
node.NoRespondd = res.Statistics == nil && res.Neighbours == nil node.NoRespondd = false
return node return node
} }
@ -185,9 +192,6 @@ func (nodes *Nodes) expire() {
if nodes.config.PingCount > 0 && nodes.ping(node) { if nodes.config.PingCount > 0 && nodes.ping(node) {
node.Online = true node.Online = true
node.NoRespondd = true node.NoRespondd = true
node.Statistics = nil
node.Neighbours = nil
} else { } else {
node.Online = false node.Online = false
node.NoRespondd = false node.NoRespondd = false
@ -196,7 +200,7 @@ func (nodes *Nodes) expire() {
} }
} }
wg.Wait() wg.Wait()
log.WithField("nodes", "expire").Debug("end") log.WithField("nodes", "expire").Info("end")
} }
// adds the nodes interface addresses to the internal map // adds the nodes interface addresses to the internal map
@ -258,7 +262,7 @@ func (nodes *Nodes) save() {
// serialize nodes // serialize nodes
SaveJSON(nodes, nodes.config.StatePath) SaveJSON(nodes, nodes.config.StatePath)
log.WithField("nodes", "save").Debug("end") log.WithField("nodes", "save").Info("end")
} }
// SaveJSON to path // SaveJSON to path

View File

@ -1,8 +1,9 @@
package runtime package runtime
import ( import (
"net"
"github.com/bdlm/log" "github.com/bdlm/log"
"github.com/sparrc/go-ping"
) )
func (nodes *Nodes) ping(node *Node) bool { func (nodes *Nodes) ping(node *Node) bool {
@ -10,38 +11,29 @@ func (nodes *Nodes) ping(node *Node) bool {
if node.Nodeinfo != nil { if node.Nodeinfo != nil {
logNode = logNode.WithField("node_id", node.Nodeinfo.NodeID) logNode = logNode.WithField("node_id", node.Nodeinfo.NodeID)
} }
var addr string var addr *net.IPAddr
if node.Address != nil { if node.Address != nil {
addr = node.Address.IP.String() addr = &net.IPAddr{IP:node.Address.IP, Zone: node.Address.Zone}
if node.Address.IP.IsLinkLocalUnicast() {
addr += "%" + node.Address.Zone
}
} else { } else {
logNode.Debug("error no address found") logNode.Debug("error no address found")
if node.Nodeinfo != nil { if node.Nodeinfo != nil {
for _, addrMaybe := range node.Nodeinfo.Network.Addresses { for _, addrMaybeString := range node.Nodeinfo.Network.Addresses {
if len(addrMaybe) >= 5 && addrMaybe[:5] != "fe80:" { if len(addrMaybeString) >= 5 && addrMaybeString[:5] != "fe80:" {
addrMaybe, err := net.ResolveIPAddr("ip6", addrMaybeString)
if err == nil {
addr = addrMaybe addr = addrMaybe
} }
} }
} }
} }
logAddr := logNode.WithField("addr", addr)
pinger, err := ping.NewPinger(addr)
if err != nil {
logAddr.Debugf("error during ping: %s", err)
return false
} }
pinger.SetPrivileged(true)
pinger.Count = nodes.config.PingCount logAddr := logNode.WithField("addr", addr.String())
pinger.Timeout = nodes.config.PingTimeout.Duration
pinger.Interval = pinger.Timeout / pinger.Count _, err := nodes.pinger.PingAttempts(addr, nodes.config.PingTimeout.Duration, nodes.config.PingCount)
pinger.Run() // blocks until finished
stats := pinger.Statistics()
logAddr.WithFields(map[string]interface{}{ logAddr.WithFields(map[string]interface{}{
"pkg_lost": stats.PacketLoss, "success": err == nil,
}).Debug("pong") }).Debug("pong")
return stats.PacketLoss < 100 return err == nil
} }