[BUGFIX] add TimeFilter to Node

This commit is contained in:
Martin/Geno 2019-06-05 23:39:13 +02:00 committed by genofire
parent 63087001d3
commit 44ee0234fa
5 changed files with 61 additions and 15 deletions

View File

@ -56,7 +56,7 @@ func GetWirelessSettings(node *yanicRuntime.Node) *WirelessSettings {
} }
type Node struct { type Node struct {
Lastseen time.Time `json:"lastseen" mapstructure:"-" gorm:"lastseen"` Lastseen *time.Time `json:"lastseen" mapstructure:"-" gorm:"lastseen"`
NodeID string `json:"node_id" gorm:"primary_key" mapstructure:"node_id"` NodeID string `json:"node_id" gorm:"primary_key" mapstructure:"node_id"`
Blacklist *time.Time `json:"-"` Blacklist *time.Time `json:"-"`
Address string `json:"ip"` Address string `json:"ip"`
@ -99,11 +99,19 @@ func NewNode(nodeOrigin *yanicRuntime.Node, ipPrefix string) *Node {
func (n *Node) GetAddress() net.TCPAddr { func (n *Node) GetAddress() net.TCPAddr {
return net.TCPAddr{IP: net.ParseIP(n.Address), Port: 22} return net.TCPAddr{IP: net.ParseIP(n.Address), Port: 22}
} }
func (n *Node) TimeFilter(d time.Duration) bool {
now := time.Now()
before := now.Add(-d)
return n.Lastseen == nil || n.Lastseen.Before(before) || n.Blacklist != nil && n.Blacklist.After(before)
}
func (n *Node) Update(node *yanicRuntime.Node, ipPrefix string) { func (n *Node) Update(node *yanicRuntime.Node, ipPrefix string) {
if node == nil { if node == nil {
return return
} }
n.Lastseen = time.Now() now := time.Now()
n.Lastseen = &now
if nodeinfo := node.Nodeinfo; nodeinfo != nil { if nodeinfo := node.Nodeinfo; nodeinfo != nil {
n.HostnameRespondd = nodeinfo.Hostname n.HostnameRespondd = nodeinfo.Hostname

View File

@ -3,6 +3,7 @@ package runtime
import ( import (
"net" "net"
"testing" "testing"
"time"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -33,3 +34,50 @@ func TestNode(t *testing.T) {
n1.Update(node1, "") n1.Update(node1, "")
assert.False(n1.CheckRespondd()) assert.False(n1.CheckRespondd())
} }
func TestNodeTimeFilter(t *testing.T) {
assert := assert.New(t)
d := time.Minute
now := time.Now()
before := now.Add(-time.Second)
after := before.Add(-d)
node := Node{}
node.Lastseen = nil
node.Blacklist = nil
assert.True(node.TimeFilter(d))
node.Lastseen = &after
node.Blacklist = nil
assert.True(node.TimeFilter(d))
node.Lastseen = &before
node.Blacklist = nil
assert.False(node.TimeFilter(d))
node.Lastseen = nil
node.Blacklist = &after
assert.True(node.TimeFilter(d))
node.Lastseen = &after
node.Blacklist = &after
assert.True(node.TimeFilter(d))
node.Lastseen = &before
node.Blacklist = &after
assert.False(node.TimeFilter(d))
node.Lastseen = nil
node.Blacklist = &before
assert.True(node.TimeFilter(d))
node.Lastseen = &after
node.Blacklist = &before
assert.True(node.TimeFilter(d))
node.Lastseen = &before
node.Blacklist = &before
assert.True(node.TimeFilter(d))
}

View File

@ -67,8 +67,6 @@ func (pinger *Pinger) Stop() {
func (pinger *Pinger) run() { func (pinger *Pinger) run() {
result := &data.PingResult{} result := &data.PingResult{}
now := time.Now()
before := now.Add(-pinger.blacklistFor)
count := 0 count := 0
var nodes []*Node var nodes []*Node
@ -81,7 +79,7 @@ func (pinger *Pinger) run() {
for _, node := range nodes { for _, node := range nodes {
go func(n *Node) { go func(n *Node) {
defer wg.Done() defer wg.Done()
if n.Blacklist != nil && n.Blacklist.After(before) { if n.TimeFilter(pinger.blacklistFor) {
return return
} }

View File

@ -51,7 +51,7 @@ func (conn *YanicDB) InsertNode(n *runtimeYanic.Node) {
"lastseen": now, "lastseen": now,
}) })
if lNode.Blacklist != nil && lNode.Blacklist.After(now.Add(-conn.blacklistFor)) { if lNode.TimeFilter(conn.blacklistFor) {
logger.Debug("on blacklist") logger.Debug("on blacklist")
return return
} }

View File

@ -1,8 +1,6 @@
package websocket package websocket
import ( import (
"time"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
wsLib "dev.sum7.eu/genofire/golang-lib/websocket" wsLib "dev.sum7.eu/genofire/golang-lib/websocket"
@ -17,18 +15,12 @@ func (ws *WebsocketServer) connectHandler(logger *log.Entry, msg *wsLib.Message)
var nodes []*runtime.Node var nodes []*runtime.Node
var count int var count int
now := time.Now()
before := now.Add(-ws.blacklistFor)
ws.db.Find(&nodes).Count(&count) ws.db.Find(&nodes).Count(&count)
ws.nodes.Lock() ws.nodes.Lock()
i := 0 i := 0
for _, node := range nodes { for _, node := range nodes {
if node.Blacklist != nil && node.Blacklist.After(before) { if node.TimeFilter(ws.blacklistFor) {
continue
}
if node.Lastseen.Before(before) {
continue continue
} }
node.Update(ws.nodes.List[node.NodeID], ws.ipPrefix) node.Update(ws.nodes.List[node.NodeID], ws.ipPrefix)