提交 829eac30 作者: Brian Tiger Chow 提交者: Jeromy

fix(bitswap) pass derived context to called functions

@whyrusleeping @jbenet

License: MIT
Signed-off-by: 's avatarBrian Tiger Chow <brian@perfmode.com>
上级 bb0b5f7c
......@@ -87,9 +87,13 @@ type bitswap struct {
// deadline enforced by the context.
func (bs *bitswap) GetBlock(parent context.Context, k u.Key) (*blocks.Block, error) {
// make sure to derive a new |ctx| and pass it to children. It's correct to
// listen on |parent| here, but incorrect to pass |parent| to new async
// functions. This is difficult to enforce. May this comment keep you safe.
// Any async work initiated by this function must end when this function
// returns. To ensure this, derive a new context. Note that it is okay to
// listen on parent in this scope, but NOT okay to pass |parent| to
// functions called by this one. Otherwise those functions won't return
// when this context Otherwise those functions won't return when this
// context's cancel func is executed. This is difficult to enforce. May
// this comment keep you safe.
ctx, cancelFunc := context.WithCancel(parent)
......@@ -101,7 +105,7 @@ func (bs *bitswap) GetBlock(parent context.Context, k u.Key) (*blocks.Block, err
log.Event(ctx, "GetBlockRequestEnd", &k)
}()
promise, err := bs.GetBlocks(parent, []u.Key{k})
promise, err := bs.GetBlocks(ctx, []u.Key{k})
if err != nil {
return nil, err
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论