-
Notifications
You must be signed in to change notification settings - Fork 32
/
listener.go
64 lines (52 loc) · 1.08 KB
/
listener.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
package netstats
import (
"net"
"sync/atomic"
"github.com/segmentio/stats"
)
func NewListener(lstn net.Listener) net.Listener {
return NewListenerWith(stats.DefaultEngine, lstn)
}
func NewListenerWith(eng *stats.Engine, lstn net.Listener) net.Listener {
return &listener{
lstn: lstn,
eng: eng,
}
}
type listener struct {
lstn net.Listener
eng *stats.Engine
closed uint32
}
func (l *listener) Accept() (conn net.Conn, err error) {
if conn, err = l.lstn.Accept(); err != nil {
if atomic.LoadUint32(&l.closed) == 0 {
l.error("accept", err)
}
}
if conn != nil {
conn = NewConnWith(l.eng, conn)
}
return
}
func (l *listener) Close() (err error) {
atomic.StoreUint32(&l.closed, 1)
return l.lstn.Close()
}
func (l *listener) Addr() net.Addr {
return l.lstn.Addr()
}
func (l *listener) error(op string, err error) {
if !isTemporary(err) {
l.eng.Incr("conn.error:count",
stats.T("operation", op),
stats.T("protocol", l.Addr().Network()),
)
}
}
func isTemporary(err error) bool {
e, ok := err.(interface {
Temporary() bool
})
return ok && e.Temporary()
}