optimization(bitswap) return connected peers as providers
Brian Tiger Chow committed
Jan 28, 2015 at 22:49 UTC
afd3333cab927dd434ac360146d31b12e6d789e7
1 file changed
+13
-1
exchange/bitswap/network/ipfs_impl.go
+13
-1
@@ -99,7 +99,19 @@ func (bsnet *impl) SetDelegate(r Receiver) {
99
100
// FindProvidersAsync returns a channel of providers for the given key
101
func (bsnet *impl) FindProvidersAsync(ctx context.Context, k util.Key, max int) <-chan peer.ID {
102
- out := make(chan peer.ID)
102
+
103
+ // Since routing queries are expensive, give bitswap the peers to which we
104
+ // have open connections. Note that this may cause issues if bitswap starts
105
+ // precisely tracking which peers provide certain keys. This optimization
106
+ // would be misleading. In the long run, this may not be the most
107
+ // appropriate place for this optimization, but it won't cause any harm in
108
+ // the short term.
109
+ connectedPeers := bsnet.host.Network().Peers()
110
+ out := make(chan peer.ID, len(connectedPeers)) // just enough buffer for these connectedPeers
111
+ for _, id := range bsnet.host.Network().Peers() {
112
+ out <- id
113
+ }
114
+
115
go func() {
116
defer close(out)
117
providers := bsnet.routing.FindProvidersAsync(ctx, k, max)