Skip to content

Commit

Permalink
tcp: add an option to enable metrics (disabled by default) (#1734)
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann authored Sep 7, 2022
1 parent d934c56 commit b52af7d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
27 changes: 20 additions & 7 deletions p2p/transport/tcp/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down
17 changes: 17 additions & 0 deletions p2p/transport/tcp/tcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit b52af7d

Please sign in to comment.