Commit 58b9745f authored by Brian Tiger Chow's avatar Brian Tiger Chow Committed by Jeromy

refactor(bitswap) group the deferreds

License: MIT
Signed-off-by: default avatarBrian Tiger Chow <brian@perfmode.com>
parent 26e6f28e
...@@ -92,11 +92,14 @@ func (bs *bitswap) GetBlock(parent context.Context, k u.Key) (*blocks.Block, err ...@@ -92,11 +92,14 @@ func (bs *bitswap) GetBlock(parent context.Context, k u.Key) (*blocks.Block, err
// functions. This is difficult to enforce. May this comment keep you safe. // functions. This is difficult to enforce. May this comment keep you safe.
ctx, cancelFunc := context.WithCancel(parent) ctx, cancelFunc := context.WithCancel(parent)
defer cancelFunc()
ctx = eventlog.ContextWithMetadata(ctx, eventlog.Uuid("GetBlockRequest")) ctx = eventlog.ContextWithMetadata(ctx, eventlog.Uuid("GetBlockRequest"))
log.Event(ctx, "GetBlockRequestBegin", &k) log.Event(ctx, "GetBlockRequestBegin", &k)
defer log.Event(ctx, "GetBlockRequestEnd", &k)
defer func() {
cancelFunc()
log.Event(ctx, "GetBlockRequestEnd", &k)
}()
promise, err := bs.GetBlocks(parent, []u.Key{k}) promise, err := bs.GetBlocks(parent, []u.Key{k})
if err != nil { if err != nil {
...@@ -109,6 +112,7 @@ func (bs *bitswap) GetBlock(parent context.Context, k u.Key) (*blocks.Block, err ...@@ -109,6 +112,7 @@ func (bs *bitswap) GetBlock(parent context.Context, k u.Key) (*blocks.Block, err
case <-parent.Done(): case <-parent.Done():
return nil, parent.Err() return nil, parent.Err()
} }
} }
// GetBlocks returns a channel where the caller may receive blocks that // GetBlocks returns a channel where the caller may receive blocks that
...@@ -172,13 +176,15 @@ func (bs *bitswap) sendWantListTo(ctx context.Context, peers <-chan peer.Peer) e ...@@ -172,13 +176,15 @@ func (bs *bitswap) sendWantListTo(ctx context.Context, peers <-chan peer.Peer) e
func (bs *bitswap) loop(parent context.Context) { func (bs *bitswap) loop(parent context.Context) {
ctx, cancel := context.WithCancel(parent) ctx, cancel := context.WithCancel(parent)
defer cancel() // signal termination
// Every so often, we should resend out our current want list // Every so often, we should resend out our current want list
rebroadcastTime := time.Second * 5 rebroadcastTime := time.Second * 5
broadcastSignal := time.NewTicker(bs.strategy.GetRebroadcastDelay()) broadcastSignal := time.NewTicker(bs.strategy.GetRebroadcastDelay())
defer broadcastSignal.Stop() defer func() {
cancel() // signal to derived async functions
broadcastSignal.Stop()
}()
for { for {
select { select {
......
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