refactor(bitswap) change PeerInfo to ID in bitswap package
@jbenet @whyrusleeping This commit replaces peer.PeerInfo with peer.ID in the bitswap package
Brian Tiger Chow committed
Dec 23, 2014 at 08:33 UTC
c34132e080b873ca64c0fc2b02feac6bc6e9f285
4 files changed
+47
-18
exchange/bitswap/bitswap.go
+5
-7
@@ -164,7 +164,7 @@ func (bs *bitswap) HasBlock(ctx context.Context, blk *blocks.Block) error {
164
return bs.network.Provide(ctx, blk.Key())
165
}
166
167
-func (bs *bitswap) sendWantListTo(ctx context.Context, peers <-chan peer.PeerInfo) error {
167
+func (bs *bitswap) sendWantListTo(ctx context.Context, peers <-chan peer.ID) error {
168
if peers == nil {
169
panic("Cant send wantlist to nil peerchan")
170
}
@@ -174,16 +174,15 @@ func (bs *bitswap) sendWantListTo(ctx context.Context, peers <-chan peer.PeerInf
174
}
175
wg := sync.WaitGroup{}
176
for peerToQuery := range peers {
177
- log.Event(ctx, "PeerToQuery", peerToQuery.ID)
177
+ log.Event(ctx, "PeerToQuery", peerToQuery)
178
wg.Add(1)
179
- bs.network.Peerstore().AddAddresses(peerToQuery.ID, peerToQuery.Addrs)
179
go func(p peer.ID) {
180
defer wg.Done()
181
if err := bs.send(ctx, p, message); err != nil {
182
log.Error(err)
183
return
184
}
186
- }(peerToQuery.ID)
185
+ }(peerToQuery)
186
}
187
wg.Wait()
188
return nil
@@ -210,9 +209,8 @@ func (bs *bitswap) sendWantlistToProviders(ctx context.Context, wantlist *wantli
209
child, _ := context.WithTimeout(ctx, providerRequestTimeout)
210
providers := bs.network.FindProvidersAsync(child, k, maxProvidersPerRequest)
211
for prov := range providers {
213
- bs.network.Peerstore().AddAddresses(prov.ID, prov.Addrs)
214
- if set.TryAdd(prov.ID) { //Do once per peer
215
- bs.send(ctx, prov.ID, message)
212
+ if set.TryAdd(prov) { //Do once per peer
213
+ bs.send(ctx, prov, message)
214
}
215
}
216
}(e.Key)
exchange/bitswap/network/interface.go
+1
-1
@@ -46,7 +46,7 @@ type Receiver interface {
46
47
type Routing interface {
48
// FindProvidersAsync returns a channel of providers for the given key
49
- FindProvidersAsync(context.Context, u.Key, int) <-chan peer.PeerInfo
49
+ FindProvidersAsync(context.Context, u.Key, int) <-chan peer.ID
50
51
// Provide provides the key to the network
52
Provide(context.Context, u.Key) error
exchange/bitswap/network/ipfs_impl.go
+17
-5
@@ -2,10 +2,10 @@ package network
2
3
import (
4
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
5
-
5
bsmsg "github.com/jbenet/go-ipfs/exchange/bitswap/message"
6
inet "github.com/jbenet/go-ipfs/net"
7
peer "github.com/jbenet/go-ipfs/peer"
8
+ routing "github.com/jbenet/go-ipfs/routing"
9
util "github.com/jbenet/go-ipfs/util"
10
)
11
@@ -13,7 +13,7 @@ var log = util.Logger("bitswap_network")
13
14
// NewFromIpfsNetwork returns a BitSwapNetwork supported by underlying IPFS
15
// Dialer & Service
16
-func NewFromIpfsNetwork(n inet.Network, r Routing) BitSwapNetwork {
16
+func NewFromIpfsNetwork(n inet.Network, r routing.IpfsRouting) BitSwapNetwork {
17
bitswapNetwork := impl{
18
network: n,
19
routing: r,
@@ -26,7 +26,7 @@ func NewFromIpfsNetwork(n inet.Network, r Routing) BitSwapNetwork {
26
// NetMessage objects, into the bitswap network interface.
27
type impl struct {
28
network inet.Network
29
- routing Routing
29
+ routing routing.IpfsRouting
30
31
// inbound messages from the network are forwarded to the receiver
32
receiver Receiver
@@ -77,8 +77,20 @@ func (bsnet *impl) Peerstore() peer.Peerstore {
77
}
78
79
// FindProvidersAsync returns a channel of providers for the given key
80
-func (bsnet *impl) FindProvidersAsync(ctx context.Context, k util.Key, max int) <-chan peer.PeerInfo { // TODO change to return ID
81
- return bsnet.routing.FindProvidersAsync(ctx, k, max)
80
+func (bsnet *impl) FindProvidersAsync(ctx context.Context, k util.Key, max int) <-chan peer.ID {
81
+ out := make(chan peer.ID)
82
+ go func() {
83
+ defer close(out)
84
+ providers := bsnet.routing.FindProvidersAsync(ctx, k, max)
85
+ for info := range providers {
86
+ bsnet.network.Peerstore().AddAddresses(info.ID, info.Addrs)
87
+ select {
88
+ case <-ctx.Done():
89
+ case out <- info.ID:
90
+ }
91
+ }
92
+ }()
93
+ return out
94
}
95
96
// Provide provides the key to the network
exchange/bitswap/testnet/network.go
+24
-5
@@ -5,6 +5,7 @@ import (
5
"fmt"
6
7
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
8
+ "github.com/jbenet/go-ipfs/routing"
9
"github.com/jbenet/go-ipfs/routing/mock"
10
11
bsmsg "github.com/jbenet/go-ipfs/exchange/bitswap/message"
@@ -37,8 +38,8 @@ type Network interface {
38
39
func VirtualNetwork(rs mockrouting.Server, d delay.D) Network {
40
return &network{
40
- clients: make(map[peer.ID]bsnet.Receiver),
41
- delay: d,
41
+ clients: make(map[peer.ID]bsnet.Receiver),
42
+ delay: d,
43
routingserver: rs,
44
}
45
}
@@ -156,7 +157,7 @@ type networkClient struct {
157
bsnet.Receiver
158
network Network
159
peerstore peer.Peerstore
159
- routing bsnet.Routing
160
+ routing routing.IpfsRouting
161
}
162
163
func (nc *networkClient) SendMessage(
@@ -174,8 +175,26 @@ func (nc *networkClient) SendRequest(
175
}
176
177
// FindProvidersAsync returns a channel of providers for the given key
177
-func (nc *networkClient) FindProvidersAsync(ctx context.Context, k util.Key, max int) <-chan peer.PeerInfo { // TODO change to return ID
178
- return nc.routing.FindProvidersAsync(ctx, k, max)
178
+func (nc *networkClient) FindProvidersAsync(ctx context.Context, k util.Key, max int) <-chan peer.ID {
179
+
180
+ // NB: this function duplicates the PeerInfo -> ID transformation in the
181
+ // bitswap network adapter. Not to worry. This network client will be
182
+ // deprecated once the ipfsnet.Mock is added. The code below is only
183
+ // temporary.
184
+
185
+ out := make(chan peer.ID)
186
+ go func() {
187
+ defer close(out)
188
+ providers := nc.routing.FindProvidersAsync(ctx, k, max)
189
+ for info := range providers {
190
+ nc.peerstore.AddAddresses(info.ID, info.Addrs)
191
+ select {
192
+ case <-ctx.Done():
193
+ case out <- info.ID:
194
+ }
195
+ }
196
+ }()
197
+ return out
198
}
199
200
// Provide provides the key to the network