|
| 1 | +package namesys |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + "time" |
| 7 | + |
| 8 | + path "github.com/ipfs/go-path" |
| 9 | + opts "github.com/ipfs/interface-go-ipfs-core/options/namesys" |
| 10 | +) |
| 11 | + |
| 12 | +type onceResult struct { |
| 13 | + value path.Path |
| 14 | + ttl time.Duration |
| 15 | + err error |
| 16 | +} |
| 17 | + |
| 18 | +type resolver interface { |
| 19 | + resolveOnceAsync(ctx context.Context, name string, options opts.ResolveOpts) <-chan onceResult |
| 20 | +} |
| 21 | + |
| 22 | +// resolve is a helper for implementing Resolver.ResolveN using resolveOnce. |
| 23 | +func resolve(ctx context.Context, r resolver, name string, options opts.ResolveOpts) (path.Path, error) { |
| 24 | + ctx, cancel := context.WithCancel(ctx) |
| 25 | + defer cancel() |
| 26 | + |
| 27 | + err := ErrResolveFailed |
| 28 | + var p path.Path |
| 29 | + |
| 30 | + resCh := resolveAsync(ctx, r, name, options) |
| 31 | + |
| 32 | + for res := range resCh { |
| 33 | + p, err = res.Path, res.Err |
| 34 | + if err != nil { |
| 35 | + break |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + return p, err |
| 40 | +} |
| 41 | + |
| 42 | +func resolveAsync(ctx context.Context, r resolver, name string, options opts.ResolveOpts) <-chan Result { |
| 43 | + resCh := r.resolveOnceAsync(ctx, name, options) |
| 44 | + depth := options.Depth |
| 45 | + outCh := make(chan Result, 1) |
| 46 | + |
| 47 | + go func() { |
| 48 | + defer close(outCh) |
| 49 | + var subCh <-chan Result |
| 50 | + var cancelSub context.CancelFunc |
| 51 | + defer func() { |
| 52 | + if cancelSub != nil { |
| 53 | + cancelSub() |
| 54 | + } |
| 55 | + }() |
| 56 | + |
| 57 | + for { |
| 58 | + select { |
| 59 | + case res, ok := <-resCh: |
| 60 | + if !ok { |
| 61 | + resCh = nil |
| 62 | + break |
| 63 | + } |
| 64 | + |
| 65 | + if res.err != nil { |
| 66 | + emitResult(ctx, outCh, Result{Err: res.err}) |
| 67 | + return |
| 68 | + } |
| 69 | + log.Debugf("resolved %s to %s", name, res.value.String()) |
| 70 | + if !strings.HasPrefix(res.value.String(), ipnsPrefix) { |
| 71 | + emitResult(ctx, outCh, Result{Path: res.value}) |
| 72 | + break |
| 73 | + } |
| 74 | + |
| 75 | + if depth == 1 { |
| 76 | + emitResult(ctx, outCh, Result{Path: res.value, Err: ErrResolveRecursion}) |
| 77 | + break |
| 78 | + } |
| 79 | + |
| 80 | + subopts := options |
| 81 | + if subopts.Depth > 1 { |
| 82 | + subopts.Depth-- |
| 83 | + } |
| 84 | + |
| 85 | + var subCtx context.Context |
| 86 | + if cancelSub != nil { |
| 87 | + // Cancel previous recursive resolve since it won't be used anyways |
| 88 | + cancelSub() |
| 89 | + } |
| 90 | + subCtx, cancelSub = context.WithCancel(ctx) |
| 91 | + _ = cancelSub |
| 92 | + |
| 93 | + p := strings.TrimPrefix(res.value.String(), ipnsPrefix) |
| 94 | + subCh = resolveAsync(subCtx, r, p, subopts) |
| 95 | + case res, ok := <-subCh: |
| 96 | + if !ok { |
| 97 | + subCh = nil |
| 98 | + break |
| 99 | + } |
| 100 | + |
| 101 | + // We don't bother returning here in case of context timeout as there is |
| 102 | + // no good reason to do that, and we may still be able to emit a result |
| 103 | + emitResult(ctx, outCh, res) |
| 104 | + case <-ctx.Done(): |
| 105 | + return |
| 106 | + } |
| 107 | + if resCh == nil && subCh == nil { |
| 108 | + return |
| 109 | + } |
| 110 | + } |
| 111 | + }() |
| 112 | + return outCh |
| 113 | +} |
| 114 | + |
| 115 | +func emitResult(ctx context.Context, outCh chan<- Result, r Result) { |
| 116 | + select { |
| 117 | + case outCh <- r: |
| 118 | + case <-ctx.Done(): |
| 119 | + } |
| 120 | +} |
0 commit comments