diff --git a/p2p/transport/tcp/metrics_unix_test.go b/p2p/transport/tcp/metrics_unix_test.go new file mode 100644 index 0000000000..094ced0d45 --- /dev/null +++ b/p2p/transport/tcp/metrics_unix_test.go @@ -0,0 +1,33 @@ +// go:build: unix + +package tcp + +import ( + "testing" + + tptu "github.com/libp2p/go-libp2p/p2p/net/upgrader" + "github.com/libp2p/go-libp2p/p2p/transport/tcpreuse" + ttransport "github.com/libp2p/go-libp2p/p2p/transport/testsuite" + + "github.com/stretchr/testify/require" +) + +func TestTcpTransportCollectsMetricsWithSharedTcpSocket(t *testing.T) { + peerA, ia := makeInsecureMuxer(t) + _, ib := makeInsecureMuxer(t) + + sharedTCPSocketA := tcpreuse.NewConnMgr(false, nil, nil) + sharedTCPSocketB := tcpreuse.NewConnMgr(false, nil, nil) + + ua, err := tptu.New(ia, muxers, nil, nil, nil) + require.NoError(t, err) + ta, err := NewTCPTransport(ua, nil, sharedTCPSocketA, WithMetrics()) + require.NoError(t, err) + ub, err := tptu.New(ib, muxers, nil, nil, nil) + require.NoError(t, err) + tb, err := NewTCPTransport(ub, nil, sharedTCPSocketB, WithMetrics()) + require.NoError(t, err) + + zero := "/ip4/127.0.0.1/tcp/0" + ttransport.SubtestTransport(t, ta, tb, zero, peerA) +} diff --git a/p2p/transport/tcpreuse/connwithscope.go b/p2p/transport/tcpreuse/connwithscope.go new file mode 100644 index 0000000000..ca66f20325 --- /dev/null +++ b/p2p/transport/tcpreuse/connwithscope.go @@ -0,0 +1,26 @@ +package tcpreuse + +import ( + "fmt" + + "github.com/libp2p/go-libp2p/core/network" + "github.com/libp2p/go-libp2p/p2p/transport/tcpreuse/internal/sampledconn" + manet "github.com/multiformats/go-multiaddr/net" +) + +type connWithScope struct { + sampledconn.ManetTCPConnInterface + scope network.ConnManagementScope +} + +func (c connWithScope) Scope() network.ConnManagementScope { + return c.scope +} + +func manetConnWithScope(c manet.Conn, scope network.ConnManagementScope) (manet.Conn, error) { + if tcpconn, ok := c.(sampledconn.ManetTCPConnInterface); ok { + return &connWithScope{tcpconn, scope}, nil + } + + return nil, fmt.Errorf("manet.Conn is not a TCP Conn") +}