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
// 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

View file

@ -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))
}

View file

@ -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 {

View file

@ -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.