Clean up timestamp usage

This commit is contained in:
Julian Kornberger 2017-01-29 20:46:06 +01:00
parent 5ca74e51bc
commit 5062b71318
2 changed files with 12 additions and 18 deletions

View File

@ -25,16 +25,16 @@ type Flags struct {
// i.e. https://github.com/ffnord/meshviewer/tree/master // i.e. https://github.com/ffnord/meshviewer/tree/master
type NodesV1 struct { type NodesV1 struct {
Version int `json:"version"` Version int `json:"version"`
Timestamp jsontime.Time `json:"timestamp"` Timestamp jsontime.Time `json:"timestamp"` // Timestamp of the generation
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
} }
// NodesV2 struct, to support new version of meshviewer (which are in legacy develop branch or newer) // NodesV2 struct, to support new version of meshviewer (which are in legacy develop branch or newer)
// i.e. https://github.com/ffnord/meshviewer/tree/dev or https://github.com/ffrgb/meshviewer/tree/develop // i.e. https://github.com/ffnord/meshviewer/tree/dev or https://github.com/ffrgb/meshviewer/tree/develop
type NodesV2 struct { type NodesV2 struct {
Version int `json:"version"` Version int `json:"version"`
Timestamp jsontime.Time `json:"timestamp"` Timestamp jsontime.Time `json:"timestamp"` // Timestamp of the generation
List []*Node `json:"nodes"` // the current nodemap, as array List []*Node `json:"nodes"` // the current nodemap, as array
} }
// Statistics a meshviewer spezifisch struct, diffrent from respondd // Statistics a meshviewer spezifisch struct, diffrent from respondd

View File

@ -14,10 +14,8 @@ import (
// Nodes struct: cache DB of Node's structs // Nodes struct: cache DB of Node's structs
type Nodes struct { type Nodes struct {
Version int `json:"version"` List map[string]*Node `json:"nodes"` // the current nodemap, indexed by node ID
Timestamp jsontime.Time `json:"timestamp"` config *Config
List map[string]*Node `json:"nodes"` // the current nodemap, indexed by node ID
config *Config
sync.RWMutex sync.RWMutex
} }
@ -31,11 +29,7 @@ func NewNodes(config *Config) *Nodes {
if config.Nodes.StatePath != "" { if config.Nodes.StatePath != "" {
nodes.load() nodes.load()
} }
/**
* Version '-1' because the nodes.json would not be defined,
* it would be change with the change of the respondd application on gluon
*/
nodes.Version = -1
return nodes return nodes
} }
@ -92,7 +86,7 @@ func (nodes *Nodes) GetNodesV1() *meshviewer.NodesV1 {
meshviewerNodes := &meshviewer.NodesV1{ meshviewerNodes := &meshviewer.NodesV1{
Version: 1, Version: 1,
List: make(map[string]*meshviewer.Node), List: make(map[string]*meshviewer.Node),
Timestamp: nodes.Timestamp, Timestamp: jsontime.Now(),
} }
for nodeID := range nodes.List { for nodeID := range nodes.List {
@ -118,7 +112,7 @@ func (nodes *Nodes) GetNodesV1() *meshviewer.NodesV1 {
func (nodes *Nodes) GetNodesV2() *meshviewer.NodesV2 { func (nodes *Nodes) GetNodesV2() *meshviewer.NodesV2 {
meshviewerNodes := &meshviewer.NodesV2{ meshviewerNodes := &meshviewer.NodesV2{
Version: 2, Version: 2,
Timestamp: nodes.Timestamp, Timestamp: jsontime.Now(),
} }
for nodeID := range nodes.List { for nodeID := range nodes.List {
@ -150,17 +144,17 @@ func (nodes *Nodes) worker() {
// Expires nodes and set nodes offline // Expires nodes and set nodes offline
func (nodes *Nodes) expire() { func (nodes *Nodes) expire() {
nodes.Timestamp = jsontime.Now() now := jsontime.Now()
// Nodes last seen before expireTime will be removed // Nodes last seen before expireTime will be removed
pruneAfter := nodes.config.Nodes.PruneAfter.Duration pruneAfter := nodes.config.Nodes.PruneAfter.Duration
if pruneAfter == 0 { if pruneAfter == 0 {
pruneAfter = time.Hour * 24 * 7 // our default pruneAfter = time.Hour * 24 * 7 // our default
} }
expireTime := nodes.Timestamp.Add(-pruneAfter) expireTime := now.Add(-pruneAfter)
// Nodes last seen before offlineTime are changed to 'offline' // Nodes last seen before offlineTime are changed to 'offline'
offlineTime := nodes.Timestamp.Add(-time.Minute * 10) offlineTime := now.Add(-time.Minute * 10)
// Locking foo // Locking foo
nodes.Lock() nodes.Lock()