-
Notifications
You must be signed in to change notification settings - Fork 15
/
tcpx.go
46 lines (38 loc) · 1.49 KB
/
tcpx.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
package testingx
import "net"
// TCPListener creates TCP connections for HTTP, TLS, etc. This type should work both
// with the standard library and with netem as its backend.
type TCPListener interface {
ListenTCP(network string, addr *net.TCPAddr) (net.Listener, error)
}
// TCPListenerStdlib implements [HTTPListener] for the stdlib.
type TCPListenerStdlib struct{}
var _ TCPListener = &TCPListenerStdlib{}
// ListenTCP implements HTTPListener.
func (*TCPListenerStdlib) ListenTCP(network string, addr *net.TCPAddr) (net.Listener, error) {
return net.ListenTCP(network, addr)
}
// tcpMaybeResetNetConn is a portable mechanism to reset a net.Conn that takes into account
// both TLS wrapping with any library and stdlib vs. netem concerns.
//
// Bug: netem is not WAI because there's no *gonet.TCPConn.SetLinger method.
func tcpMaybeResetNetConn(conn net.Conn) {
// first, let's try to get the underlying conn, when we're using TLS
type connUnwrapper interface {
NetConn() net.Conn
}
if unwrapper, good := conn.(connUnwrapper); good {
conn = unwrapper.NetConn()
}
// then, let's try to get the controller for disabling linger
type connLingerSetter interface {
SetLinger(sec int) error
}
if setter, good := conn.(connLingerSetter); good {
_ = setter.SetLinger(0)
}
// close the conn to trigger the reset (we MUST call Close here where
// we're using the underlying conn and it doesn't suffice to call it
// inside the http.Handler, where wrapping would not cause a RST)
_ = conn.Close()
}