-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ipfs name resolve --stream #5404
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
7ff9f09
namesys: Implement async methods
magik6k 6adb15f
namesys: async: go vet fixes
magik6k 804634d
namesys: switch to async code
magik6k f69cf07
ipfs name resolve --stream
magik6k 8e4ce42
namesys: use routing.SearchValue
magik6k ea352f6
update routing-helpers
magik6k 0bef460
Merge branch 'master' into feat/namestream
magik6k 94bbeff
Merge remote-tracking branch 'origin/master' into feat/namestream
magik6k 7dbeb27
namesys: review fixes
magik6k e335fd3
namesys: drop prefix args
magik6k 462c802
namesys: allow non /ipfs paths
magik6k 734615a
namesys: avoid defer in loop
magik6k 7fcf56e
namesys: select on output
magik6k 16e6343
Merge remote-tracking branch 'origin/master' into feat/namestream
magik6k 6207222
namesys: doc on emitResult
magik6k File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) | ||
magik6k marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please document this function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are docs for this on the interface (like all other coreapi functions). We may want to point people there, but I'd this in a separate PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's just do this now. Separate PRs for documentation tend to not happen.