@cryptotaxi247 / kubo / commits / 734615ac9

namesys: avoid defer in loop

License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>

Łukasz Magiera committed Oct 16, 2018 at 16:53 UTC 734615ac98d0ccadbb33d7cf8ba3ebdb438cfb1f
2 files changed +15 -4
namesys/base.go
+13 -2
@@ -60,6 +60,9 @@ func resolveAsync(ctx context.Context, r resolver, name string, options opts.Res
60
61 if res.err != nil {
62 outCh <- Result{Err: res.err}
63 + if cancelSub != nil {
64 + cancelSub()
65 + }
66 return
67 }
68 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
82 }
83
84 var subCtx context.Context
82 - if subCh != nil {
85 + if cancelSub != nil {
86 // Cancel previous recursive resolve since it won't be used anyways
87 cancelSub()
88 }
89 subCtx, cancelSub = context.WithCancel(ctx)
87 - defer cancelSub()
90
91 p := strings.TrimPrefix(res.value.String(), ipnsPrefix)
92 subCh = resolveAsync(subCtx, r, p, subopts)
@@ -97,12 +99,21 @@ func resolveAsync(ctx context.Context, r resolver, name string, options opts.Res
99 select {
100 case outCh <- res:
101 case <-ctx.Done():
102 + if cancelSub != nil {
103 + cancelSub()
104 + }
105 return
106 }
107 case <-ctx.Done():
108 + if cancelSub != nil {
109 + cancelSub()
110 + }
111 return
112 }
113 if resCh == nil && subCh == nil {
114 + if cancelSub != nil {
115 + cancelSub()
116 + }
117 return
118 }
119 }
namesys/namesys.go
+2 -2
@@ -86,8 +86,8 @@ func (ns *mpns) ResolveAsync(ctx context.Context, name string, options ...opts.R
86 func (ns *mpns) resolveOnceAsync(ctx context.Context, name string, options opts.ResolveOpts) <-chan onceResult {
87 out := make(chan onceResult, 1)
88
89 - if !strings.HasPrefix(name, "/ipns/") {
90 - name = "/ipns/" + name
89 + if !strings.HasPrefix(name, ipnsPrefix) {
90 + name = ipnsPrefix + name
91 }
92 segments := strings.SplitN(name, "/", 4)
93 if len(segments) < 3 || segments[0] != "" {