-
Notifications
You must be signed in to change notification settings - Fork 32
/
conn.go
167 lines (141 loc) · 3.16 KB
/
conn.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package netstats
import (
"io"
"math"
"net"
"sync"
"time"
"github.com/segmentio/stats"
)
func init() {
stats.Buckets.Set("conn.read:bytes",
1e2, // 100 B
1e3, // 1 KB
1e4, // 10 KB
1e5, // 100 KB
math.Inf(+1),
)
stats.Buckets.Set("conn.write:bytes",
1e2, // 100 B
1e3, // 1 KB
1e4, // 10 KB
1e5, // 100 KB
math.Inf(+1),
)
}
// NewConn returns a net.Conn object that wraps c and produces metrics on the
// default engine.
func NewConn(c net.Conn) net.Conn {
return NewConnWith(stats.DefaultEngine, c)
}
// NewConn returns a net.Conn object that wraps c and produces metrics on eng.
func NewConnWith(eng *stats.Engine, c net.Conn) net.Conn {
nc := &conn{Conn: c, eng: eng}
proto := c.LocalAddr().Network()
nc.r.metrics.protocol = proto
nc.w.metrics.protocol = proto
eng.Incr("conn.open:count", stats.T("protocol", proto))
return nc
}
type conn struct {
net.Conn
eng *stats.Engine
once sync.Once
r struct {
sync.Mutex
metrics struct {
count int `metric:"count" type:"counter"`
bytes int `metric:"bytes" type:"histogram"`
protocol string `tag:"protocol"`
} `metric:"conn.read"`
}
w struct {
sync.Mutex
metrics struct {
count int `metric:"count" type:"counter"`
bytes int `metric:"bytes" type:"histogram"`
protocol string `tag:"protocol"`
} `metric:"conn.write"`
}
}
func (c *conn) BaseConn() net.Conn {
return c.Conn
}
func (c *conn) Close() (err error) {
err = c.Conn.Close()
c.once.Do(func() {
c.eng.Incr("conn.close:count", stats.T("protocol", c.w.metrics.protocol))
if err != nil {
c.error("close", err)
}
})
return
}
func (c *conn) Read(b []byte) (n int, err error) {
n, err = c.Conn.Read(b)
c.r.Lock()
c.r.metrics.count = 1
c.r.metrics.bytes = n
c.eng.Report(&c.r)
c.r.Unlock()
if err != nil && err != io.EOF {
c.error("read", err)
}
return
}
func (c *conn) Write(b []byte) (n int, err error) {
n, err = c.Conn.Write(b)
c.w.Lock()
c.w.metrics.count = 1
c.w.metrics.bytes = n
c.eng.Report(&c.w)
c.w.Unlock()
if err != nil {
c.error("write", err)
}
return
}
func (c *conn) SetDeadline(t time.Time) (err error) {
if err = c.Conn.SetDeadline(t); err != nil {
c.error("set-deadline", err)
}
return
}
func (c *conn) SetReadDeadline(t time.Time) (err error) {
if err = c.Conn.SetReadDeadline(t); err != nil {
c.error("set-read-deadline", err)
}
return
}
func (c *conn) SetWriteDeadline(t time.Time) (err error) {
if err = c.Conn.SetWriteDeadline(t); err != nil {
c.error("set-write-deadline", err)
}
return
}
func (c *conn) error(op string, err error) {
switch err = rootError(err); err {
case io.EOF, io.ErrClosedPipe, io.ErrUnexpectedEOF:
// this is expected to happen when connections are closed
default:
// only report serious errors, others should be handled gracefully
if !isTemporary(err) {
c.eng.Incr("conn.error:count",
stats.T("operation", op),
stats.T("protocol", c.w.metrics.protocol),
)
}
}
}
func rootError(err error) error {
searchRootError:
for i := 0; i != 10; i++ { // protect against cyclic errors
switch e := err.(type) {
case *net.OpError:
err = e.Err
default:
break searchRootError
}
}
return err
}