forked from lonelycode/gorpc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
conn_stats_generic.go
112 lines (94 loc) · 2.86 KB
/
conn_stats_generic.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// +build !386
package gorpc
import (
"sync/atomic"
)
// Snapshot returns connection statistics' snapshot.
//
// Use stats returned from ConnStats.Snapshot() on live Client and / or Server,
// since the original stats can be updated by concurrently running goroutines.
func (cs *ConnStats) Snapshot() *ConnStats {
cs.lock.Lock()
funcCallStatsCopy := make(map[string]uint64)
for k, v := range cs.FuncCallStats {
funcCallStatsCopy[k] = v
}
cs.lock.Unlock()
return &ConnStats{
RPCCalls: atomic.LoadUint64(&cs.RPCCalls),
RPCTime: atomic.LoadUint64(&cs.RPCTime),
BytesWritten: atomic.LoadUint64(&cs.BytesWritten),
BytesRead: atomic.LoadUint64(&cs.BytesRead),
ReadCalls: atomic.LoadUint64(&cs.ReadCalls),
ReadErrors: atomic.LoadUint64(&cs.ReadErrors),
WriteCalls: atomic.LoadUint64(&cs.WriteCalls),
WriteErrors: atomic.LoadUint64(&cs.WriteErrors),
DialCalls: atomic.LoadUint64(&cs.DialCalls),
DialErrors: atomic.LoadUint64(&cs.DialErrors),
AcceptCalls: atomic.LoadUint64(&cs.AcceptCalls),
AcceptErrors: atomic.LoadUint64(&cs.AcceptErrors),
FuncCallStats: funcCallStatsCopy,
}
}
// Reset resets all the stats counters.
func (cs *ConnStats) Reset() {
atomic.StoreUint64(&cs.RPCCalls, 0)
atomic.StoreUint64(&cs.RPCTime, 0)
atomic.StoreUint64(&cs.BytesWritten, 0)
atomic.StoreUint64(&cs.BytesRead, 0)
atomic.StoreUint64(&cs.WriteCalls, 0)
atomic.StoreUint64(&cs.WriteErrors, 0)
atomic.StoreUint64(&cs.ReadCalls, 0)
atomic.StoreUint64(&cs.ReadErrors, 0)
atomic.StoreUint64(&cs.DialCalls, 0)
atomic.StoreUint64(&cs.DialErrors, 0)
atomic.StoreUint64(&cs.AcceptCalls, 0)
atomic.StoreUint64(&cs.AcceptErrors, 0)
cs.lock.Lock()
cs.FuncCallStats = make(map[string]uint64)
cs.lock.Unlock()
}
func (cs *ConnStats) incRPCCalls() {
atomic.AddUint64(&cs.RPCCalls, 1)
}
func (cs *ConnStats) incRPCTime(dt uint64) {
atomic.AddUint64(&cs.RPCTime, dt)
}
func (cs *ConnStats) addBytesWritten(n uint64) {
atomic.AddUint64(&cs.BytesWritten, n)
}
func (cs *ConnStats) addBytesRead(n uint64) {
atomic.AddUint64(&cs.BytesRead, n)
}
func (cs *ConnStats) incReadCalls() {
atomic.AddUint64(&cs.ReadCalls, 1)
}
func (cs *ConnStats) incReadErrors() {
atomic.AddUint64(&cs.ReadErrors, 1)
}
func (cs *ConnStats) incWriteCalls() {
atomic.AddUint64(&cs.WriteCalls, 1)
}
func (cs *ConnStats) incWriteErrors() {
atomic.AddUint64(&cs.WriteErrors, 1)
}
func (cs *ConnStats) incDialCalls() {
atomic.AddUint64(&cs.DialCalls, 1)
}
func (cs *ConnStats) incDialErrors() {
atomic.AddUint64(&cs.DialErrors, 1)
}
func (cs *ConnStats) incAcceptCalls() {
atomic.AddUint64(&cs.AcceptCalls, 1)
}
func (cs *ConnStats) incAcceptErrors() {
atomic.AddUint64(&cs.AcceptErrors, 1)
}
func (cs *ConnStats) incFuncCalls(fn string) {
cs.lock.Lock()
if cs.FuncCallStats == nil {
cs.FuncCallStats = make(map[string]uint64)
}
cs.FuncCallStats[fn]++
cs.lock.Unlock()
}