Skip to content

Commit

Permalink
Merge pull request ipfs/go-bitswap#531 from ipfs/chore/rename-wiretap
Browse files Browse the repository at this point in the history
fix: rename wiretap to tracer

This commit was moved from ipfs/go-bitswap@958b163
  • Loading branch information
Stebalien authored Sep 15, 2021
2 parents 04cf894 + 11a8c51 commit 3a26de9
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 43 deletions.
6 changes: 3 additions & 3 deletions bitswap/bitswap.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ type Bitswap struct {
sendTimeHistogram metrics.Histogram

// External statistics interface
wiretap WireTap
tracer Tracer

// the SessionManager routes requests to interested sessions
sm *bssm.SessionManager
Expand Down Expand Up @@ -527,8 +527,8 @@ func (bs *Bitswap) ReceiveMessage(ctx context.Context, p peer.ID, incoming bsmsg
// TODO: this is bad, and could be easily abused.
// Should only track *useful* messages in ledger

if bs.wiretap != nil {
bs.wiretap.MessageReceived(p, incoming)
if bs.tracer != nil {
bs.tracer.MessageReceived(p, incoming)
}

iblocks := incoming.Blocks()
Expand Down
22 changes: 11 additions & 11 deletions bitswap/bitswap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -870,29 +870,29 @@ type logItem struct {
pid peer.ID
msg bsmsg.BitSwapMessage
}
type mockWireTap struct {
type mockTracer struct {
mu sync.Mutex
log []logItem
}

func (m *mockWireTap) MessageReceived(p peer.ID, msg bsmsg.BitSwapMessage) {
func (m *mockTracer) MessageReceived(p peer.ID, msg bsmsg.BitSwapMessage) {
m.mu.Lock()
defer m.mu.Unlock()
m.log = append(m.log, logItem{'r', p, msg})
}
func (m *mockWireTap) MessageSent(p peer.ID, msg bsmsg.BitSwapMessage) {
func (m *mockTracer) MessageSent(p peer.ID, msg bsmsg.BitSwapMessage) {
m.mu.Lock()
defer m.mu.Unlock()
m.log = append(m.log, logItem{'s', p, msg})
}

func (m *mockWireTap) getLog() []logItem {
func (m *mockTracer) getLog() []logItem {
m.mu.Lock()
defer m.mu.Unlock()
return m.log[:len(m.log):len(m.log)]
}

func TestWireTap(t *testing.T) {
func TestTracer(t *testing.T) {
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
defer ig.Close()
Expand All @@ -901,9 +901,9 @@ func TestWireTap(t *testing.T) {
instances := ig.Instances(3)
blocks := bg.Blocks(2)

// Install WireTap
wiretap := new(mockWireTap)
bitswap.EnableWireTap(wiretap)(instances[0].Exchange)
// Install Tracer
wiretap := new(mockTracer)
bitswap.WithTracer(wiretap)(instances[0].Exchange)

// First peer has block
err := instances[0].Exchange.HasBlock(blocks[0])
Expand Down Expand Up @@ -937,9 +937,9 @@ func TestWireTap(t *testing.T) {

log := wiretap.getLog()

// After communication, 3 messages should be logged via WireTap
// After communication, 3 messages should be logged via Tracer
if l := len(log); l != 3 {
t.Fatal("expected 3 items logged via WireTap, found", l)
t.Fatal("expected 3 items logged via Tracer, found", l)
}

// Received: 'Have'
Expand Down Expand Up @@ -988,7 +988,7 @@ func TestWireTap(t *testing.T) {
}

// After disabling WireTap, no new messages are logged
bitswap.DisableWireTap()(instances[0].Exchange)
bitswap.WithTracer(nil)(instances[0].Exchange)

err = instances[0].Exchange.HasBlock(blocks[1])
if err != nil {
Expand Down
20 changes: 20 additions & 0 deletions bitswap/tracer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package bitswap

import (
bsmsg "github.com/ipfs/go-bitswap/message"
peer "github.com/libp2p/go-libp2p-core/peer"
)

// Tracer provides methods to access all messages sent and received by Bitswap.
// This interface can be used to implement various statistics (this is original intent).
type Tracer interface {
MessageReceived(peer.ID, bsmsg.BitSwapMessage)
MessageSent(peer.ID, bsmsg.BitSwapMessage)
}

// Configures Bitswap to use given tracer.
func WithTracer(tap Tracer) Option {
return func(bs *Bitswap) {
bs.tracer = tap
}
}
27 changes: 0 additions & 27 deletions bitswap/wiretap.go

This file was deleted.

4 changes: 2 additions & 2 deletions bitswap/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ func (bs *Bitswap) taskWorker(ctx context.Context, id int) {
// Ideally, yes. But we'd need some way to trigger a retry and/or drop
// the peer.
bs.engine.MessageSent(envelope.Peer, envelope.Message)
if bs.wiretap != nil {
bs.wiretap.MessageSent(envelope.Peer, envelope.Message)
if bs.tracer != nil {
bs.tracer.MessageSent(envelope.Peer, envelope.Message)
}
bs.sendBlocks(ctx, envelope)

Expand Down

0 comments on commit 3a26de9

Please sign in to comment.