diff --git a/runtime/node.go b/runtime/node.go index cd1bfcb..2e04c84 100644 --- a/runtime/node.go +++ b/runtime/node.go @@ -56,7 +56,7 @@ func GetWirelessSettings(node *yanicRuntime.Node) *WirelessSettings { } 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"` Blacklist *time.Time `json:"-"` Address string `json:"ip"` @@ -99,11 +99,19 @@ func NewNode(nodeOrigin *yanicRuntime.Node, ipPrefix string) *Node { func (n *Node) GetAddress() net.TCPAddr { 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) { if node == nil { return } - n.Lastseen = time.Now() + now := time.Now() + n.Lastseen = &now if nodeinfo := node.Nodeinfo; nodeinfo != nil { n.HostnameRespondd = nodeinfo.Hostname diff --git a/runtime/node_test.go b/runtime/node_test.go index d560d96..ba036f3 100644 --- a/runtime/node_test.go +++ b/runtime/node_test.go @@ -3,6 +3,7 @@ package runtime import ( "net" "testing" + "time" "github.com/stretchr/testify/assert" @@ -33,3 +34,50 @@ func TestNode(t *testing.T) { n1.Update(node1, "") 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)) +} diff --git a/runtime/ping.go b/runtime/ping.go index d1a0636..8162670 100644 --- a/runtime/ping.go +++ b/runtime/ping.go @@ -67,8 +67,6 @@ func (pinger *Pinger) Stop() { func (pinger *Pinger) run() { result := &data.PingResult{} - now := time.Now() - before := now.Add(-pinger.blacklistFor) count := 0 var nodes []*Node @@ -81,7 +79,7 @@ func (pinger *Pinger) run() { for _, node := range nodes { go func(n *Node) { defer wg.Done() - if n.Blacklist != nil && n.Blacklist.After(before) { + if n.TimeFilter(pinger.blacklistFor) { return } diff --git a/runtime/yanic.go b/runtime/yanic.go index df1a0f7..c0621a5 100644 --- a/runtime/yanic.go +++ b/runtime/yanic.go @@ -51,7 +51,7 @@ func (conn *YanicDB) InsertNode(n *runtimeYanic.Node) { "lastseen": now, }) - if lNode.Blacklist != nil && lNode.Blacklist.After(now.Add(-conn.blacklistFor)) { + if lNode.TimeFilter(conn.blacklistFor) { logger.Debug("on blacklist") return } diff --git a/websocket/hd_connect.go b/websocket/hd_connect.go index 1b2e7ee..7733a3d 100644 --- a/websocket/hd_connect.go +++ b/websocket/hd_connect.go @@ -1,8 +1,6 @@ package websocket import ( - "time" - log "github.com/sirupsen/logrus" 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 count int - now := time.Now() - before := now.Add(-ws.blacklistFor) - ws.db.Find(&nodes).Count(&count) ws.nodes.Lock() i := 0 for _, node := range nodes { - if node.Blacklist != nil && node.Blacklist.After(before) { - continue - } - if node.Lastseen.Before(before) { + if node.TimeFilter(ws.blacklistFor) { continue } node.Update(ws.nodes.List[node.NodeID], ws.ipPrefix)