-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5404 from ipfs/feat/namestream
ipfs name resolve --stream
- Loading branch information
Showing
16 changed files
with
466 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,121 @@ | ||
package namesys | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"time" | ||
|
||
context "context" | ||
|
||
opts "github.com/ipfs/go-ipfs/namesys/opts" | ||
|
||
path "gx/ipfs/QmdrpbDgeYH3VxkCciQCJY5LkDYdXtig6unDzQmMxFtWEw/go-path" | ||
) | ||
|
||
type onceResult struct { | ||
value path.Path | ||
ttl time.Duration | ||
err error | ||
} | ||
|
||
type resolver interface { | ||
// resolveOnce looks up a name once (without recursion). | ||
resolveOnce(ctx context.Context, name string, options *opts.ResolveOpts) (value path.Path, ttl time.Duration, err error) | ||
resolveOnceAsync(ctx context.Context, name string, options opts.ResolveOpts) <-chan onceResult | ||
} | ||
|
||
// resolve is a helper for implementing Resolver.ResolveN using resolveOnce. | ||
func resolve(ctx context.Context, r resolver, name string, options *opts.ResolveOpts, prefixes ...string) (path.Path, error) { | ||
depth := options.Depth | ||
for { | ||
p, _, err := r.resolveOnce(ctx, name, options) | ||
func resolve(ctx context.Context, r resolver, name string, options opts.ResolveOpts) (path.Path, error) { | ||
ctx, cancel := context.WithCancel(ctx) | ||
defer cancel() | ||
|
||
err := ErrResolveFailed | ||
var p path.Path | ||
|
||
resCh := resolveAsync(ctx, r, name, options) | ||
|
||
for res := range resCh { | ||
p, err = res.Path, res.Err | ||
if err != nil { | ||
return "", err | ||
break | ||
} | ||
log.Debugf("resolved %s to %s", name, p.String()) | ||
} | ||
|
||
if strings.HasPrefix(p.String(), "/ipfs/") { | ||
// we've bottomed out with an IPFS path | ||
return p, nil | ||
} | ||
return p, err | ||
} | ||
|
||
if depth == 1 { | ||
return p, ErrResolveRecursion | ||
} | ||
func resolveAsync(ctx context.Context, r resolver, name string, options opts.ResolveOpts) <-chan Result { | ||
resCh := r.resolveOnceAsync(ctx, name, options) | ||
depth := options.Depth | ||
outCh := make(chan Result, 1) | ||
|
||
matched := false | ||
for _, prefix := range prefixes { | ||
if strings.HasPrefix(p.String(), prefix) { | ||
matched = true | ||
if len(prefixes) == 1 { | ||
name = strings.TrimPrefix(p.String(), prefix) | ||
} | ||
break | ||
go func() { | ||
defer close(outCh) | ||
var subCh <-chan Result | ||
var cancelSub context.CancelFunc | ||
defer func() { | ||
if cancelSub != nil { | ||
cancelSub() | ||
} | ||
} | ||
}() | ||
|
||
if !matched { | ||
return p, nil | ||
} | ||
for { | ||
select { | ||
case res, ok := <-resCh: | ||
if !ok { | ||
resCh = nil | ||
break | ||
} | ||
|
||
if res.err != nil { | ||
emitResult(ctx, outCh, Result{Err: res.err}) | ||
return | ||
} | ||
log.Debugf("resolved %s to %s", name, res.value.String()) | ||
if !strings.HasPrefix(res.value.String(), ipnsPrefix) { | ||
emitResult(ctx, outCh, Result{Path: res.value}) | ||
break | ||
} | ||
|
||
if depth == 1 { | ||
emitResult(ctx, outCh, Result{Path: res.value, Err: ErrResolveRecursion}) | ||
break | ||
} | ||
|
||
subopts := options | ||
if subopts.Depth > 1 { | ||
subopts.Depth-- | ||
} | ||
|
||
if depth > 1 { | ||
depth-- | ||
var subCtx context.Context | ||
if cancelSub != nil { | ||
// Cancel previous recursive resolve since it won't be used anyways | ||
cancelSub() | ||
} | ||
subCtx, cancelSub = context.WithCancel(ctx) | ||
_ = cancelSub | ||
|
||
p := strings.TrimPrefix(res.value.String(), ipnsPrefix) | ||
subCh = resolveAsync(subCtx, r, p, subopts) | ||
case res, ok := <-subCh: | ||
if !ok { | ||
subCh = nil | ||
break | ||
} | ||
|
||
// We don't bother returning here in case of context timeout as there is | ||
// no good reason to do that, and we may still be able to emit a result | ||
emitResult(ctx, outCh, res) | ||
case <-ctx.Done(): | ||
return | ||
} | ||
if resCh == nil && subCh == nil { | ||
return | ||
} | ||
} | ||
}() | ||
return outCh | ||
} | ||
|
||
func emitResult(ctx context.Context, outCh chan<- Result, r Result) { | ||
select { | ||
case outCh <- r: | ||
case <-ctx.Done(): | ||
} | ||
} |
Oops, something went wrong.