refactor ago

This commit is contained in:
Josh Deprez 2024-05-12 18:12:27 +10:00
parent 331da095c3
commit 9fe09c0a9b
Signed by: josh
SSH key fingerprint: SHA256:zZji7w1Ilh2RuUpbQcqkLPrqmRwpiCSycbF2EfKm6Kw
4 changed files with 19 additions and 25 deletions

View file

@ -378,10 +378,7 @@ func (e AMTEntry) Valid() bool {
// LastUpdatedAgo is a friendly string reporting how long ago the entry was // LastUpdatedAgo is a friendly string reporting how long ago the entry was
// updated/resolved. // updated/resolved.
func (e AMTEntry) LastUpdatedAgo() string { func (e AMTEntry) LastUpdatedAgo() string {
if e.LastUpdated.IsZero() { return ago(e.LastUpdated)
return "never"
}
return fmt.Sprintf("%v ago", time.Since(e.LastUpdated).Truncate(time.Millisecond))
} }
// addressMappingTable implements a concurrent-safe Address Mapping Table for // addressMappingTable implements a concurrent-safe Address Mapping Table for

View file

@ -16,6 +16,11 @@
package router package router
import (
"fmt"
"time"
)
// StringSet is a set of strings. // StringSet is a set of strings.
// Yep, yet another string set implementation. Took me 2 minutes to write *shrug* // Yep, yet another string set implementation. Took me 2 minutes to write *shrug*
type StringSet map[string]struct{} type StringSet map[string]struct{}
@ -50,3 +55,11 @@ func SetFromSlice(ss []string) StringSet {
set.Insert(ss...) set.Insert(ss...)
return set 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))
}

View file

@ -19,7 +19,6 @@ package router
import ( import (
"bytes" "bytes"
"context" "context"
"fmt"
"log" "log"
"net" "net"
"sync" "sync"
@ -151,37 +150,25 @@ func (p *AURPPeer) SenderState() SenderState {
func (p *AURPPeer) LastReconnectAgo() string { func (p *AURPPeer) LastReconnectAgo() string {
p.mu.RLock() p.mu.RLock()
defer p.mu.RUnlock() defer p.mu.RUnlock()
if p.lastReconnect.IsZero() { return ago(p.lastReconnect)
return "never"
}
return fmt.Sprintf("%v ago", time.Since(p.lastReconnect).Truncate(time.Millisecond))
} }
func (p *AURPPeer) LastHeardFromAgo() string { func (p *AURPPeer) LastHeardFromAgo() string {
p.mu.RLock() p.mu.RLock()
defer p.mu.RUnlock() defer p.mu.RUnlock()
if p.lastHeardFrom.IsZero() { return ago(p.lastHeardFrom)
return "never"
}
return fmt.Sprintf("%v ago", time.Since(p.lastHeardFrom).Truncate(time.Millisecond))
} }
func (p *AURPPeer) LastSendAgo() string { func (p *AURPPeer) LastSendAgo() string {
p.mu.RLock() p.mu.RLock()
defer p.mu.RUnlock() defer p.mu.RUnlock()
if p.lastSend.IsZero() { return ago(p.lastSend)
return "never"
}
return fmt.Sprintf("%v ago", time.Since(p.lastSend).Truncate(time.Millisecond))
} }
func (p *AURPPeer) LastUpdateAgo() string { func (p *AURPPeer) LastUpdateAgo() string {
p.mu.RLock() p.mu.RLock()
defer p.mu.RUnlock() defer p.mu.RUnlock()
if p.lastUpdate.IsZero() { return ago(p.lastUpdate)
return "never"
}
return fmt.Sprintf("%v ago", time.Since(p.lastUpdate).Truncate(time.Millisecond))
} }
func (p *AURPPeer) SendRetries() int { func (p *AURPPeer) SendRetries() int {

View file

@ -45,10 +45,7 @@ type Route struct {
} }
func (r Route) LastSeenAgo() string { func (r Route) LastSeenAgo() string {
if r.LastSeen.IsZero() { return ago(r.LastSeen)
return "never"
}
return fmt.Sprintf("%v ago", time.Since(r.LastSeen).Truncate(time.Millisecond))
} }
// Valid reports whether the route is valid. // Valid reports whether the route is valid.