go beetle server: show time in human readable format
MODIFY
go/src/github.com/xing/beetle/server.go
go/src/github.com/xing/beetle/server.go
@@ -100,7 +100,7 @@ func (s *ServerState) GetStatus() *ServerStatus {
type PST struct {
c string
- t int
+ t time.Duration
}
type PSTA []PST
@@ -119,16 +119,18 @@ func (a PSTA) Swap(i, j int) {
func (s *ServerState) UnresponsiveClients() []string {
res := make([]string, 0)
now := time.Now()
- threshold := now.Add(-(time.Duration(s.GetConfig().ClientTimeout) * time.Second))
+ threshold := now.Add(-(time.Duration(s.GetConfig().ClientTimeout)))
a := make(PSTA, 0)
for c, t := range s.clientsLastSeen {
if t.Before(threshold) {
- a = append(a, PST{c: c, t: int(now.Sub(t).Seconds())})
+ a = append(a, PST{c: c, t: now.Sub(t)})
}
}
sort.Sort(sort.Reverse(a))
for _, x := range a {
- res = append(res, fmt.Sprintf("%s:%d", x.c, x.t))
+ if rounded := x.t.Truncate(time.Second); rounded > 0 {
+ res = append(res, fmt.Sprintf("%s: last seen %s ago", x.c, rounded))
+ }
}
return res
}

MODIFY
go/src/github.com/xing/beetle/server_test.go
go/src/github.com/xing/beetle/server_test.go
@@ -76,14 +76,14 @@ func TestServerManagingUnresponsiveClients(t *testing.T) {
// add an old client
s.clientsLastSeen["b"] = old
u = s.UnresponsiveClients()
- if len(u) != 1 || u[0] != "b:2" {
- t.Errorf("after adding an old client, UnresponsiveClients() should be '[b:2]', but is: '%v'", u)
+ if len(u) != 1 || u[0] != "b: last seen 2s ago" {
+ t.Errorf("after adding an old client, UnresponsiveClients() should be '[b: last seen 2s ago]', but is: '%v'", u)
}
// add an older client
s.clientsLastSeen["c"] = older
u = s.UnresponsiveClients()
- if len(u) != 2 || u[0] != "c:3" || u[1] != "b:2" {
- t.Errorf("after adding an old client, UnresponsiveClients() should be '[c:3 b:2]', but is: '%v'", u)
+ if len(u) != 2 || u[0] != "c: last seen 3s ago" || u[1] != "b: last seen 2s ago" {
+ t.Errorf("after adding an old client, UnresponsiveClients() should be '[c:last seen 3s ago 3 b: last seen 2s ago]', but is: '%v'", u)
}
}

go beetle server: show time in human readable format