Browse Source

tcp: add an option to enable metrics (disabled by default) (#1734)

pull/1745/head
Marten Seemann 2 years ago
committed by GitHub
parent
commit
b52af7d043
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 27
      p2p/transport/tcp/tcp.go
  2. 17
      p2p/transport/tcp/tcp_test.go

27
p2p/transport/tcp/tcp.go

@ -106,14 +106,21 @@ func WithConnectionTimeout(d time.Duration) Option {
}
}
func WithMetrics() Option {
return func(tr *TcpTransport) error {
tr.enableMetrics = true
return nil
}
}
// TcpTransport is the TCP transport.
type TcpTransport struct {
// Connection upgrader for upgrading insecure stream connections to
// secure multiplex connections.
upgrader transport.Upgrader
// Explicitly disable reuseport.
disableReuseport bool
disableReuseport bool // Explicitly disable reuseport.
enableMetrics bool
// TCP connect timeout
connectTimeout time.Duration
@ -189,10 +196,14 @@ func (t *TcpTransport) Dial(ctx context.Context, raddr ma.Multiaddr, p peer.ID)
// This means we can immediately reuse the 5-tuple and reconnect.
tryLinger(conn, 0)
tryKeepAlive(conn, true)
c, err := newTracingConn(conn, true)
if err != nil {
connScope.Done()
return nil, err
c := conn
if t.enableMetrics {
var err error
c, err = newTracingConn(conn, true)
if err != nil {
connScope.Done()
return nil, err
}
}
direction := network.DirOutbound
if ok, isClient, _ := network.GetSimultaneousConnect(ctx); ok && !isClient {
@ -219,7 +230,9 @@ func (t *TcpTransport) Listen(laddr ma.Multiaddr) (transport.Listener, error) {
if err != nil {
return nil, err
}
list = newTracingListener(&tcpListener{list, 0})
if t.enableMetrics {
list = newTracingListener(&tcpListener{list, 0})
}
return t.upgrader.UpgradeListener(t, list), nil
}

17
p2p/transport/tcp/tcp_test.go

@ -44,6 +44,23 @@ func TestTcpTransport(t *testing.T) {
envReuseportVal = true
}
func TestTcpTransportWithMetrics(t *testing.T) {
peerA, ia := makeInsecureMuxer(t)
_, ib := makeInsecureMuxer(t)
ua, err := tptu.New(ia, yamux.DefaultTransport)
require.NoError(t, err)
ta, err := NewTCPTransport(ua, nil, WithMetrics())
require.NoError(t, err)
ub, err := tptu.New(ib, yamux.DefaultTransport)
require.NoError(t, err)
tb, err := NewTCPTransport(ub, nil, WithMetrics())
require.NoError(t, err)
zero := "/ip4/127.0.0.1/tcp/0"
ttransport.SubtestTransport(t, ta, tb, zero, peerA)
}
func TestResourceManager(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

Loading…
Cancel
Save