-
Notifications
You must be signed in to change notification settings - Fork 394
/
listener_linux.go
109 lines (91 loc) · 3.17 KB
/
listener_linux.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
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
// +build linux
package server
import (
"net"
"runtime"
"golang.org/x/sys/unix"
)
// defaultUserTimeout is the value we use for the TCP_USER_TIMEOUT setting.
const defaultUserTimeout = 60 * 1000 // 60s in ms
// wrapListener wraps the provided net.Listener in one that sets timeouts
// and monitors if the returned connections are closed or leaked.
func wrapListener(lis net.Listener) net.Listener {
if lis, ok := lis.(*net.TCPListener); ok {
return newUserTimeoutListener(lis)
}
return lis
}
// userTimeoutListener wraps a tcp listener so that it sets the TCP_USER_TIMEOUT
// value for each socket it returns.
type userTimeoutListener struct {
lis *net.TCPListener
}
// newUserTimeoutListener wraps the tcp listener in a userTimeoutListener.
func newUserTimeoutListener(lis *net.TCPListener) *userTimeoutListener {
return &userTimeoutListener{lis: lis}
}
// Accept waits for and returns the next connection to the listener.
func (lis *userTimeoutListener) Accept() (net.Conn, error) {
conn, err := lis.lis.AcceptTCP()
if err != nil {
return nil, err
}
// By default from Go, keep alive period + idle are ~15sec. The default
// keep count is 8 according to some kernel docs. That means it should
// fail after ~120 seconds. Unfortunately, keep alive only happens if
// there is no send-q on the socket, and so a slow reader can still cause
// hanging sockets forever. By setting user timeout, we will kill the
// connection if any writes go unacknowledged for the amount of time.
// This should close the keep alive hole.
//
// See https://blog.cloudflare.com/when-tcp-sockets-refuse-to-die/
rawConn, err := conn.SyscallConn()
if err != nil {
return nil, err
}
controlErr := rawConn.Control(func(fd uintptr) {
err = unix.SetsockoptInt(int(fd), unix.SOL_TCP, unix.TCP_USER_TIMEOUT, defaultUserTimeout)
})
if controlErr != nil {
return nil, controlErr
}
if err != nil {
return nil, err
}
return newCloseTrackingConn(conn), nil
}
// Close closes the listener.
// Any blocked Accept operations will be unblocked and return errors.
func (lis *userTimeoutListener) Close() error {
return lis.lis.Close()
}
// Addr returns the listener's network address.
func (lis *userTimeoutListener) Addr() net.Addr {
return lis.lis.Addr()
}
// closeTrackingConn wraps a net.Conn and keeps track of if it was closed
// or if it was leaked (and closes it if it was leaked.)
type closeTrackingConn struct {
net.Conn
}
// newCloseTrackingConn wraps the conn in a closeTrackingConn. It sets a
// finalizer on the returned value to close the conn and monitor that it
// was leaked.
func newCloseTrackingConn(conn net.Conn) *closeTrackingConn {
tracked := &closeTrackingConn{Conn: conn}
runtime.SetFinalizer(tracked, (*closeTrackingConn).finalize)
return tracked
}
// Close clears the finalizer and closes the connection.
func (c *closeTrackingConn) Close() error {
runtime.SetFinalizer(c, nil)
mon.Event("connection_closed")
return c.Conn.Close()
}
// finalize monitors that a connection was leaked and closes the connection.
func (c *closeTrackingConn) finalize() {
mon.Event("connection_leaked")
_ = c.Conn.Close()
}