Skip to content

Commit

Permalink
namesys: avoid defer in loop
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
  • Loading branch information
magik6k committed Oct 16, 2018
1 parent 462c802 commit 734615a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
15 changes: 13 additions & 2 deletions namesys/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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)
Expand All @@ -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
}
}
Expand Down
4 changes: 2 additions & 2 deletions namesys/namesys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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] != "" {
Expand Down

0 comments on commit 734615a

Please sign in to comment.