diff --git a/p2p/host/basic/basic_host.go b/p2p/host/basic/basic_host.go index b450db1b71f4abd45f958274f80384a9ddafcd12..66949a0c85bc3e5ff55844550dde9cc079cce979 100644 --- a/p2p/host/basic/basic_host.go +++ b/p2p/host/basic/basic_host.go @@ -58,7 +58,6 @@ func New(net inet.Network, opts ...interface{}) *BasicHost { h := &BasicHost{ network: net, mux: msmux.NewMultistreamMuxer(), - bwc: metrics.NewBandwidthCounter(), } h.proc = goprocess.WithTeardown(func() error { @@ -90,6 +89,8 @@ func New(net inet.Network, opts ...interface{}) *BasicHost { } } + h.ids.Reporter = h.bwc + net.SetConnHandler(h.newConnHandler) net.SetStreamHandler(h.newStreamHandler) @@ -217,7 +218,11 @@ func (h *BasicHost) NewStream(ctx context.Context, p peer.ID, pids ...protocol.I s.SetProtocol(selpid) h.Peerstore().AddProtocols(p, selected) - return mstream.WrapStream(s, h.bwc), nil + if h.bwc != nil { + s = mstream.WrapStream(s, h.bwc) + } + + return s, nil } func pidsToStrings(pids []protocol.ID) []string { @@ -250,11 +255,13 @@ func (h *BasicHost) newStream(ctx context.Context, p peer.ID, pid protocol.ID) ( s.SetProtocol(pid) - logStream := mstream.WrapStream(s, h.bwc) + if h.bwc != nil { + s = mstream.WrapStream(s, h.bwc) + } - lzcon := msmux.NewMSSelect(logStream, string(pid)) + lzcon := msmux.NewMSSelect(s, string(pid)) return &streamWrapper{ - Stream: logStream, + Stream: s, rw: lzcon, }, nil } diff --git a/p2p/host/routed/routed.go b/p2p/host/routed/routed.go index 7279a78b47713963ed4a51712e6401eb6ba87cdb..81b3c6381fa10f77a14812e5fea9d0ab62da83dd 100644 --- a/p2p/host/routed/routed.go +++ b/p2p/host/routed/routed.go @@ -9,7 +9,6 @@ import ( logging "github.com/ipfs/go-log" lgbl "github.com/libp2p/go-libp2p-loggables" - metrics "github.com/libp2p/go-libp2p-metrics" inet "github.com/libp2p/go-libp2p-net" peer "github.com/libp2p/go-libp2p-peer" pstore "github.com/libp2p/go-libp2p-peerstore" @@ -18,7 +17,7 @@ import ( msmux "github.com/whyrusleeping/go-multistream" ) -var log = logging.Logger("github.com/libp2p/go-libp2p/p2p/host/routed") +var log = logging.Logger("routedhost") // AddressTTL is the expiry time for our addresses. // We expire them quickly. @@ -126,8 +125,4 @@ func (rh *RoutedHost) Close() error { return rh.host.Close() } -func (rh *RoutedHost) GetBandwidthReporter() metrics.Reporter { - return rh.host.GetBandwidthReporter() -} - var _ (host.Host) = (*RoutedHost)(nil) diff --git a/p2p/protocol/identify/id.go b/p2p/protocol/identify/id.go index baffc5afaf06a54d31a90c8eb433f2feabf62c39..67bacfd2aa56e1a3ea3f2eeebbaa56d60f6e61b8 100644 --- a/p2p/protocol/identify/id.go +++ b/p2p/protocol/identify/id.go @@ -13,6 +13,7 @@ import ( ic "github.com/libp2p/go-libp2p-crypto" host "github.com/libp2p/go-libp2p-host" lgbl "github.com/libp2p/go-libp2p-loggables" + metrics "github.com/libp2p/go-libp2p-metrics" mstream "github.com/libp2p/go-libp2p-metrics/stream" inet "github.com/libp2p/go-libp2p-net" peer "github.com/libp2p/go-libp2p-peer" @@ -42,6 +43,7 @@ const ClientVersion = "go-libp2p/3.3.4" type IDService struct { Host host.Host + Reporter metrics.Reporter // connections undergoing identification // for wait purposes currid map[inet.Conn]chan struct{} @@ -91,8 +93,9 @@ func (ids *IDService) IdentifyConn(c inet.Conn) { s.SetProtocol(ID) - bwc := ids.Host.GetBandwidthReporter() - s = mstream.WrapStream(s, bwc) + if ids.Reporter != nil { + s = mstream.WrapStream(s, ids.Reporter) + } // ok give the response to our handler. if err := msmux.SelectProtoOrFail(ID, s); err != nil { @@ -118,8 +121,9 @@ func (ids *IDService) RequestHandler(s inet.Stream) { defer s.Close() c := s.Conn() - bwc := ids.Host.GetBandwidthReporter() - s = mstream.WrapStream(s, bwc) + if ids.Reporter != nil { + s = mstream.WrapStream(s, ids.Reporter) + } w := ggio.NewDelimitedWriter(s) mes := pb.Identify{} diff --git a/package.json b/package.json index fd1da9cebf2bc3c7d7cfbba8805f8f58bf739c18..bb10e4d1449769726458d8224e329a8d40661819 100644 --- a/package.json +++ b/package.json @@ -211,9 +211,9 @@ }, { "author": "whyrusleeping", - "hash": "QmU5qKZsCG1Wg38jwg8XezBdc3fBGMMZjM7YFMAhunC1Yh", + "hash": "Qmb6UFbVu1grhv5o5KnouvtZ6cqdrjXj6zLejAHWunxgCt", "name": "go-libp2p-host", - "version": "1.2.0" + "version": "1.3.0" }, { "author": "whyrusleeping",