bitswap: respond to peers connecting + disconnecting
With these notifications, bitswap can reclaim all resources for any outstanding work for a peer. cc @briantigerchow @whyrusleeping
Juan Batiz-Benet committed
Jan 24, 2015 at 09:12 UTC
105448769050cc046a97f55ece6cd39a7ae05bbe
4 files changed
+49
exchange/bitswap/bitswap.go
+18
@@ -339,6 +339,24 @@ func (bs *bitswap) ReceiveMessage(ctx context.Context, p peer.ID, incoming bsmsg
339
return "", nil
340
}
341
342
+// Connected/Disconnected warns bitswap about peer connections
343
+func (bs *bitswap) PeerConnected(p peer.ID) {
344
+ // TODO: add to clientWorker??
345
+
346
+ peers := make(chan peer.ID)
347
+ err := bs.sendWantlistToPeers(context.TODO(), peers)
348
+ if err != nil {
349
+ log.Errorf("error sending wantlist: %s", err)
350
+ }
351
+ peers <- p
352
+ close(peers)
353
+}
354
+
355
+// Connected/Disconnected warns bitswap about peer connections
356
+func (bs *bitswap) PeerDisconnected(peer.ID) {
357
+ // TODO: release resources.
358
+}
359
+
360
func (bs *bitswap) cancelBlocks(ctx context.Context, bkeys []u.Key) {
361
if len(bkeys) < 1 {
362
return
exchange/bitswap/network/interface.go
+4
@@ -40,6 +40,10 @@ type Receiver interface {
40
destination peer.ID, outgoing bsmsg.BitSwapMessage)
41
42
ReceiveError(error)
43
+
44
+ // Connected/Disconnected warns bitswap about peer connections
45
+ PeerConnected(peer.ID)
46
+ PeerDisconnected(peer.ID)
47
}
48
49
type Routing interface {
exchange/bitswap/network/ipfs_impl.go
+20
@@ -21,6 +21,9 @@ func NewFromIpfsHost(host host.Host, r routing.IpfsRouting) BitSwapNetwork {
21
routing: r,
22
}
23
host.SetStreamHandler(ProtocolBitswap, bitswapNetwork.handleNewStream)
24
+ host.Network().Notify((*netNotifiee)(&bitswapNetwork))
25
+ // TODO: StopNotify.
26
+
27
return &bitswapNetwork
28
}
29
@@ -139,3 +142,20 @@ func (bsnet *impl) handleNewStream(s inet.Stream) {
142
log.Debugf("bitswap net handleNewStream from %s", s.Conn().RemotePeer())
143
bsnet.receiver.ReceiveMessage(ctx, p, received)
144
}
145
+
146
+type netNotifiee impl
147
+
148
+func (nn *netNotifiee) impl() *impl {
149
+ return (*impl)(nn)
150
+}
151
+
152
+func (nn *netNotifiee) Connected(n inet.Network, v inet.Conn) {
153
+ nn.impl().receiver.PeerConnected(v.RemotePeer())
154
+}
155
+
156
+func (nn *netNotifiee) Disconnected(n inet.Network, v inet.Conn) {
157
+ nn.impl().receiver.PeerDisconnected(v.RemotePeer())
158
+}
159
+
160
+func (nn *netNotifiee) OpenedStream(n inet.Network, v inet.Stream) {}
161
+func (nn *netNotifiee) ClosedStream(n inet.Network, v inet.Stream) {}
exchange/bitswap/testnet/network_test.go
+7
@@ -146,3 +146,10 @@ func (lam *lambdaImpl) ReceiveMessage(ctx context.Context,
146
func (lam *lambdaImpl) ReceiveError(err error) {
147
// TODO log error
148
}
149
+
150
+func (lam *lambdaImpl) PeerConnected(p peer.ID) {
151
+ // TODO
152
+}
153
+func (lam *lambdaImpl) PeerDisconnected(peer.ID) {
154
+ // TODO
155
+}