-
Notifications
You must be signed in to change notification settings - Fork 12
/
stats.go
95 lines (85 loc) · 2.01 KB
/
stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package server
import (
"context"
"strconv"
"sync/atomic"
"time"
"github.com/decred/slog"
)
type counter int64
func (c *counter) add(amt int64) {
atomic.AddInt64((*int64)(c), amt)
}
func (c *counter) value() int64 {
return atomic.LoadInt64((*int64)(c))
}
type stats struct {
bytesSent counter
bytesRecv counter
matomsRecv counter
invoicesSent counter
invoicesRecv counter
subsRecv counter
rmsSent counter
rmsRecv counter
activeSubs counter
connections counter
disconnections counter
}
// hbytes == "human bytes"
func hbytes(i int64) string {
switch {
case i < 1e3:
return strconv.FormatInt(i, 10) + "B"
case i < 1e6:
f := float64(i)
return strconv.FormatFloat(f/1e3, 'f', 2, 64) + "KB"
case i < 1e9:
f := float64(i)
return strconv.FormatFloat(f/1e6, 'f', 2, 64) + "MB"
case i < 1e12:
f := float64(i)
return strconv.FormatFloat(f/1e9, 'f', 2, 64) + "GB"
case i < 1e15:
f := float64(i)
return strconv.FormatFloat(f/1e12, 'f', 2, 64) + "TB"
default:
return strconv.FormatInt(i, 10)
}
}
func (s *stats) runPrinter(ctx context.Context, log slog.Logger) error {
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(10 * time.Second):
// Not fetching all under a single lock makes the stats
// less exact, but more performant.
bs := s.bytesSent.value()
br := s.bytesRecv.value()
mr := s.matomsRecv.value()
ivs := s.invoicesSent.value()
ivr := s.invoicesRecv.value()
sr := s.subsRecv.value()
rs := s.rmsRecv.value()
rr := s.rmsSent.value()
as := s.activeSubs.value()
conn := s.connections.value()
disc := s.disconnections.value()
online := conn - disc
log.Infof("Server Stats: "+
"bytes %s in / %s out, "+
"invoices: %d gen / %d paid, "+
"dcr recv: %.8f, "+
"subs: %d total / %d active, "+
"RMs recv %d / sent %d, "+
"conns %d in / %d out / %d online",
hbytes(br), hbytes(bs),
ivs, ivr,
float64(mr)/1e11,
sr, as,
rs, rr,
conn, disc, online)
}
}
}