mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-27 16:07:42 +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 {
|
||||
outCh <- Result{Err: res.err}
|
||||
if cancelSub != nil {
|
||||
cancelSub()
|
||||
}
|
||||
return
|
||||
}
|
||||
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
|
||||
if subCh != nil {
|
||||
if cancelSub != nil {
|
||||
// Cancel previous recursive resolve since it won't be used anyways
|
||||
cancelSub()
|
||||
}
|
||||
subCtx, cancelSub = context.WithCancel(ctx)
|
||||
defer cancelSub()
|
||||
|
||||
p := strings.TrimPrefix(res.value.String(), ipnsPrefix)
|
||||
subCh = resolveAsync(subCtx, r, p, subopts)
|
||||
@ -97,12 +99,21 @@ func resolveAsync(ctx context.Context, r resolver, name string, options opts.Res
|
||||
select {
|
||||
case outCh <- res:
|
||||
case <-ctx.Done():
|
||||
if cancelSub != nil {
|
||||
cancelSub()
|
||||
}
|
||||
return
|
||||
}
|
||||
case <-ctx.Done():
|
||||
if cancelSub != nil {
|
||||
cancelSub()
|
||||
}
|
||||
return
|
||||
}
|
||||
if resCh == nil && subCh == nil {
|
||||
if cancelSub != nil {
|
||||
cancelSub()
|
||||
}
|
||||
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 {
|
||||
out := make(chan onceResult, 1)
|
||||
|
||||
if !strings.HasPrefix(name, "/ipns/") {
|
||||
name = "/ipns/" + name
|
||||
if !strings.HasPrefix(name, ipnsPrefix) {
|
||||
name = ipnsPrefix + name
|
||||
}
|
||||
segments := strings.SplitN(name, "/", 4)
|
||||
if len(segments) < 3 || segments[0] != "" {
|
||||
|
Reference in New Issue
Block a user