-
-
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.
- Loading branch information
Showing
30 changed files
with
2,578 additions
and
363 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
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
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 |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package namesys | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"time" | ||
|
||
path "github.com/ipfs/go-path" | ||
opts "github.com/ipfs/interface-go-ipfs-core/options/namesys" | ||
) | ||
|
||
type onceResult struct { | ||
value path.Path | ||
ttl time.Duration | ||
err error | ||
} | ||
|
||
type resolver interface { | ||
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) (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 { | ||
break | ||
} | ||
} | ||
|
||
return p, err | ||
} | ||
|
||
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) | ||
|
||
go func() { | ||
defer close(outCh) | ||
var subCh <-chan Result | ||
var cancelSub context.CancelFunc | ||
defer func() { | ||
if cancelSub != nil { | ||
cancelSub() | ||
} | ||
}() | ||
|
||
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-- | ||
} | ||
|
||
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(): | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package namesys | ||
|
||
import ( | ||
"time" | ||
|
||
path "github.com/ipfs/go-path" | ||
) | ||
|
||
func (ns *mpns) cacheGet(name string) (path.Path, bool) { | ||
// existence of optional mapping defined via IPFS_NS_MAP is checked first | ||
if ns.staticMap != nil { | ||
val, ok := ns.staticMap[name] | ||
if ok { | ||
return val, true | ||
} | ||
} | ||
|
||
if ns.cache == nil { | ||
return "", false | ||
} | ||
|
||
ientry, ok := ns.cache.Get(name) | ||
if !ok { | ||
return "", false | ||
} | ||
|
||
entry, ok := ientry.(cacheEntry) | ||
if !ok { | ||
// should never happen, purely for sanity | ||
log.Panicf("unexpected type %T in cache for %q.", ientry, name) | ||
} | ||
|
||
if time.Now().Before(entry.eol) { | ||
return entry.val, true | ||
} | ||
|
||
ns.cache.Remove(name) | ||
|
||
return "", false | ||
} | ||
|
||
func (ns *mpns) cacheSet(name string, val path.Path, ttl time.Duration) { | ||
if ns.cache == nil || ttl <= 0 { | ||
return | ||
} | ||
ns.cache.Add(name, cacheEntry{ | ||
val: val, | ||
eol: time.Now().Add(ttl), | ||
}) | ||
} | ||
|
||
func (ns *mpns) cacheInvalidate(name string) { | ||
if ns.cache == nil { | ||
return | ||
} | ||
ns.cache.Remove(name) | ||
} | ||
|
||
type cacheEntry struct { | ||
val path.Path | ||
eol time.Time | ||
} |
Oops, something went wrong.