Skip to content

Commit cd98823

Browse files
committed
bitswap/network/router: Improve SendMessage and Self()
1 parent 889c053 commit cd98823

1 file changed

Lines changed: 26 additions & 3 deletions

File tree

bitswap/network/router.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ func New(pstore peerstore.Peerstore, bitswap BitSwapNetwork, http BitSwapNetwork
3131
return http
3232
}
3333

34+
if http.Self() != bitswap.Self() {
35+
panic("http and bitswap network report different peer IDs")
36+
}
37+
3438
return &router{
3539
Peerstore: pstore,
3640
Bitswap: bitswap,
@@ -48,8 +52,13 @@ func (rt *router) Stop() {
4852
rt.HTTP.Stop()
4953
}
5054

51-
// Should be the same for both
55+
// Self returns the peer ID of the network.
5256
func (rt *router) Self() peer.ID {
57+
// Self is used on the bitswap server,
58+
// and on the client on the message queue
59+
// and session manager.
60+
// We ensure during initialization that we are using
61+
// the same host for both networks.
5362
return rt.Bitswap.Self()
5463
}
5564

@@ -72,8 +81,22 @@ func (rt *router) Latency(p peer.ID) time.Duration {
7281
}
7382

7483
func (rt *router) SendMessage(ctx context.Context, p peer.ID, msg bsmsg.BitSwapMessage) error {
75-
// SendMessage is only used by bitswap server so we send a bitswap
76-
// message.
84+
// SendMessage is only used by bitswap server on sendBlocks(). We
85+
// should not be passing a router to the bitswap server but we try to
86+
// make our best.
87+
88+
// If the message has blocks, send it via bitswap.
89+
if len(msg.Blocks()) > 0 {
90+
return rt.Bitswap.SendMessage(ctx, p, msg)
91+
}
92+
93+
// Otherwise, assume it's a wantlist. Follow usual prioritization
94+
// of HTTP when possible.
95+
pi := rt.Peerstore.PeerInfo(p)
96+
htaddrs, _ := SplitHTTPAddrs(pi)
97+
if len(htaddrs.Addrs) > 0 {
98+
return rt.HTTP.SendMessage(ctx, p, msg)
99+
}
77100
return rt.Bitswap.SendMessage(ctx, p, msg)
78101
}
79102

0 commit comments

Comments
 (0)