diff --git a/router/aarp.go b/router/aarp.go index 2fdeb11..8158034 100644 --- a/router/aarp.go +++ b/router/aarp.go @@ -378,10 +378,7 @@ func (e AMTEntry) Valid() bool { // LastUpdatedAgo is a friendly string reporting how long ago the entry was // updated/resolved. func (e AMTEntry) LastUpdatedAgo() string { - if e.LastUpdated.IsZero() { - return "never" - } - return fmt.Sprintf("%v ago", time.Since(e.LastUpdated).Truncate(time.Millisecond)) + return ago(e.LastUpdated) } // addressMappingTable implements a concurrent-safe Address Mapping Table for diff --git a/router/misc.go b/router/misc.go index e84f00e..7011e48 100644 --- a/router/misc.go +++ b/router/misc.go @@ -16,6 +16,11 @@ package router +import ( + "fmt" + "time" +) + // StringSet is a set of strings. // Yep, yet another string set implementation. Took me 2 minutes to write *shrug* type StringSet map[string]struct{} @@ -50,3 +55,11 @@ func SetFromSlice(ss []string) StringSet { set.Insert(ss...) return set } + +// ago is a helper for formatting times. +func ago(t time.Time) string { + if t.IsZero() { + return "never" + } + return fmt.Sprintf("%v ago", time.Since(t).Truncate(time.Millisecond)) +} diff --git a/router/peer_aurp.go b/router/peer_aurp.go index 8913151..f2ad413 100644 --- a/router/peer_aurp.go +++ b/router/peer_aurp.go @@ -19,7 +19,6 @@ package router import ( "bytes" "context" - "fmt" "log" "net" "sync" @@ -151,37 +150,25 @@ func (p *AURPPeer) SenderState() SenderState { func (p *AURPPeer) LastReconnectAgo() string { p.mu.RLock() defer p.mu.RUnlock() - if p.lastReconnect.IsZero() { - return "never" - } - return fmt.Sprintf("%v ago", time.Since(p.lastReconnect).Truncate(time.Millisecond)) + return ago(p.lastReconnect) } func (p *AURPPeer) LastHeardFromAgo() string { p.mu.RLock() defer p.mu.RUnlock() - if p.lastHeardFrom.IsZero() { - return "never" - } - return fmt.Sprintf("%v ago", time.Since(p.lastHeardFrom).Truncate(time.Millisecond)) + return ago(p.lastHeardFrom) } func (p *AURPPeer) LastSendAgo() string { p.mu.RLock() defer p.mu.RUnlock() - if p.lastSend.IsZero() { - return "never" - } - return fmt.Sprintf("%v ago", time.Since(p.lastSend).Truncate(time.Millisecond)) + return ago(p.lastSend) } func (p *AURPPeer) LastUpdateAgo() string { p.mu.RLock() defer p.mu.RUnlock() - if p.lastUpdate.IsZero() { - return "never" - } - return fmt.Sprintf("%v ago", time.Since(p.lastUpdate).Truncate(time.Millisecond)) + return ago(p.lastUpdate) } func (p *AURPPeer) SendRetries() int { diff --git a/router/route.go b/router/route.go index bf16dfe..ae5de0c 100644 --- a/router/route.go +++ b/router/route.go @@ -45,10 +45,7 @@ type Route struct { } func (r Route) LastSeenAgo() string { - if r.LastSeen.IsZero() { - return "never" - } - return fmt.Sprintf("%v ago", time.Since(r.LastSeen).Truncate(time.Millisecond)) + return ago(r.LastSeen) } // Valid reports whether the route is valid.