Commit 26e6f28e authored by Brian Tiger Chow's avatar Brian Tiger Chow Committed by Jeromy

fix(exchange) allow exchange to be closed

License: MIT
Signed-off-by: default avatarBrian Tiger Chow <brian@perfmode.com>
parent d6e2157a
...@@ -25,9 +25,11 @@ var log = eventlog.Logger("bitswap") ...@@ -25,9 +25,11 @@ var log = eventlog.Logger("bitswap")
// provided BitSwapNetwork. This function registers the returned instance as // provided BitSwapNetwork. This function registers the returned instance as
// the network delegate. // the network delegate.
// Runs until context is cancelled // Runs until context is cancelled
func New(ctx context.Context, p peer.Peer, network bsnet.BitSwapNetwork, routing bsnet.Routing, func New(parent context.Context, p peer.Peer, network bsnet.BitSwapNetwork, routing bsnet.Routing,
bstore blockstore.Blockstore, nice bool) exchange.Interface { bstore blockstore.Blockstore, nice bool) exchange.Interface {
ctx, cancelFunc := context.WithCancel(parent)
notif := notifications.New() notif := notifications.New()
go func() { go func() {
<-ctx.Done() <-ctx.Done()
...@@ -36,6 +38,7 @@ func New(ctx context.Context, p peer.Peer, network bsnet.BitSwapNetwork, routing ...@@ -36,6 +38,7 @@ func New(ctx context.Context, p peer.Peer, network bsnet.BitSwapNetwork, routing
bs := &bitswap{ bs := &bitswap{
blockstore: bstore, blockstore: bstore,
cancelFunc: cancelFunc,
notifications: notif, notifications: notif,
strategy: strategy.New(nice), strategy: strategy.New(nice),
routing: routing, routing: routing,
...@@ -75,6 +78,9 @@ type bitswap struct { ...@@ -75,6 +78,9 @@ type bitswap struct {
strategy strategy.Strategy strategy strategy.Strategy
wantlist u.KeySet wantlist u.KeySet
// cancelFunc signals cancellation to the bitswap event loop
cancelFunc func()
} }
// GetBlock attempts to retrieve a particular block from peers within the // GetBlock attempts to retrieve a particular block from peers within the
...@@ -295,3 +301,8 @@ func (bs *bitswap) sendToPeersThatWant(ctx context.Context, block blocks.Block) ...@@ -295,3 +301,8 @@ func (bs *bitswap) sendToPeersThatWant(ctx context.Context, block blocks.Block)
} }
} }
} }
func (bs *bitswap) Close() error {
bs.cancelFunc()
return nil // to conform to Closer interface
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment