mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-28 00:39:31 +08:00
namesys: avoid defer in loop
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
This commit is contained in:
@ -60,6 +60,9 @@ func resolveAsync(ctx context.Context, r resolver, name string, options opts.Res
|
|||||||
|
|
||||||
if res.err != nil {
|
if res.err != nil {
|
||||||
outCh <- Result{Err: res.err}
|
outCh <- Result{Err: res.err}
|
||||||
|
if cancelSub != nil {
|
||||||
|
cancelSub()
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Debugf("resolved %s to %s", name, res.value.String())
|
log.Debugf("resolved %s to %s", name, res.value.String())
|
||||||
@ -79,12 +82,11 @@ func resolveAsync(ctx context.Context, r resolver, name string, options opts.Res
|
|||||||
}
|
}
|
||||||
|
|
||||||
var subCtx context.Context
|
var subCtx context.Context
|
||||||
if subCh != nil {
|
if cancelSub != nil {
|
||||||
// Cancel previous recursive resolve since it won't be used anyways
|
// Cancel previous recursive resolve since it won't be used anyways
|
||||||
cancelSub()
|
cancelSub()
|
||||||
}
|
}
|
||||||
subCtx, cancelSub = context.WithCancel(ctx)
|
subCtx, cancelSub = context.WithCancel(ctx)
|
||||||
defer cancelSub()
|
|
||||||
|
|
||||||
p := strings.TrimPrefix(res.value.String(), ipnsPrefix)
|
p := strings.TrimPrefix(res.value.String(), ipnsPrefix)
|
||||||
subCh = resolveAsync(subCtx, r, p, subopts)
|
subCh = resolveAsync(subCtx, r, p, subopts)
|
||||||
@ -97,12 +99,21 @@ func resolveAsync(ctx context.Context, r resolver, name string, options opts.Res
|
|||||||
select {
|
select {
|
||||||
case outCh <- res:
|
case outCh <- res:
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
|
if cancelSub != nil {
|
||||||
|
cancelSub()
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
|
if cancelSub != nil {
|
||||||
|
cancelSub()
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if resCh == nil && subCh == nil {
|
if resCh == nil && subCh == nil {
|
||||||
|
if cancelSub != nil {
|
||||||
|
cancelSub()
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -86,8 +86,8 @@ func (ns *mpns) ResolveAsync(ctx context.Context, name string, options ...opts.R
|
|||||||
func (ns *mpns) resolveOnceAsync(ctx context.Context, name string, options opts.ResolveOpts) <-chan onceResult {
|
func (ns *mpns) resolveOnceAsync(ctx context.Context, name string, options opts.ResolveOpts) <-chan onceResult {
|
||||||
out := make(chan onceResult, 1)
|
out := make(chan onceResult, 1)
|
||||||
|
|
||||||
if !strings.HasPrefix(name, "/ipns/") {
|
if !strings.HasPrefix(name, ipnsPrefix) {
|
||||||
name = "/ipns/" + name
|
name = ipnsPrefix + name
|
||||||
}
|
}
|
||||||
segments := strings.SplitN(name, "/", 4)
|
segments := strings.SplitN(name, "/", 4)
|
||||||
if len(segments) < 3 || segments[0] != "" {
|
if len(segments) < 3 || segments[0] != "" {
|
||||||
|
Reference in New Issue
Block a user