From 82c09ad95234dc6189e0fbb8f8a5ae4e466a8f75 Mon Sep 17 00:00:00 2001 From: Julian Kornberger Date: Sun, 29 Jan 2017 21:04:10 +0100 Subject: [PATCH] Add configuration option `offline_after` --- config_example.toml | 3 +++ models/config.go | 1 + models/nodes.go | 18 +++++++++--------- models/nodes_test.go | 1 + 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/config_example.toml b/config_example.toml index 8f19ba4..48ad64b 100644 --- a/config_example.toml +++ b/config_example.toml @@ -20,6 +20,9 @@ state_path = "/var/lib/collector/state.json" # Export nodes and graph periodically save_interval = "5s" +# Set node to offline if not seen within this period +offline_after = "10m" + # Prune offline nodes after a time of inactivity prune_after = "7d" diff --git a/models/config.go b/models/config.go index e2d4295..bdb62a7 100644 --- a/models/config.go +++ b/models/config.go @@ -25,6 +25,7 @@ type Config struct { GraphPath string StatePath string SaveInterval Duration // Save nodes periodically + OfflineAfter Duration // Set node to offline if not seen within this period PruneAfter Duration // Remove nodes after n days of inactivity } Influxdb struct { diff --git a/models/nodes.go b/models/nodes.go index fa43be9..0a504bf 100644 --- a/models/nodes.go +++ b/models/nodes.go @@ -146,25 +146,25 @@ func (nodes *Nodes) worker() { func (nodes *Nodes) expire() { now := jsontime.Now() - // Nodes last seen before expireTime will be removed - pruneAfter := nodes.config.Nodes.PruneAfter.Duration - if pruneAfter == 0 { - pruneAfter = time.Hour * 24 * 7 // our default + // Nodes last seen before expireAfter will be removed + prunePeriod := nodes.config.Nodes.PruneAfter.Duration + if prunePeriod == 0 { + prunePeriod = time.Hour * 24 * 7 // our default } - expireTime := now.Add(-pruneAfter) + pruneAfter := now.Add(-prunePeriod) - // Nodes last seen before offlineTime are changed to 'offline' - offlineTime := now.Add(-time.Minute * 10) + // Nodes last seen within OfflineAfter are changed to 'offline' + offlineAfter := now.Add(-nodes.config.Nodes.OfflineAfter.Duration) // Locking foo nodes.Lock() defer nodes.Unlock() for id, node := range nodes.List { - if node.Lastseen.Before(expireTime) { + if node.Lastseen.Before(pruneAfter) { // expire delete(nodes.List, id) - } else if node.Lastseen.Before(offlineTime) { + } else if node.Lastseen.Before(offlineAfter) { // set to offline node.Flags.Online = false } diff --git a/models/nodes_test.go b/models/nodes_test.go index aa84adb..d1d9694 100644 --- a/models/nodes_test.go +++ b/models/nodes_test.go @@ -13,6 +13,7 @@ import ( func TestExpire(t *testing.T) { assert := assert.New(t) config := &Config{} + config.Nodes.OfflineAfter.Duration = time.Minute * 10 config.Nodes.PruneAfter.Duration = time.Hour * 24 * 6 nodes := &Nodes{ config: config,