Commit 6adb15f4 authored by Łukasz Magiera's avatar Łukasz Magiera

namesys: async: go vet fixes

License: MIT
Signed-off-by: default avatarŁukasz Magiera <magik6k@gmail.com>
parent 7ff9f09b
......@@ -35,7 +35,7 @@ type mockNamesys map[string]path.Path
func (m mockNamesys) Resolve(ctx context.Context, name string, opts ...nsopts.ResolveOpt) (value path.Path, err error) {
cfg := nsopts.DefaultResolveOpts()
for _, o := range opts {
o(cfg)
o(&cfg)
}
depth := cfg.Depth
if depth == nsopts.UnlimitedDepth {
......@@ -57,6 +57,14 @@ func (m mockNamesys) Resolve(ctx context.Context, name string, opts ...nsopts.Re
return value, nil
}
func (m mockNamesys) ResolveAsync(ctx context.Context, name string, opts ...nsopts.ResolveOpt) <-chan namesys.Result {
out := make(chan namesys.Result, 1)
v, err := m.Resolve(ctx, name, opts...)
out <- namesys.Result{Path: v, Err: err}
close(out)
return nil
}
func (m mockNamesys) Publish(ctx context.Context, name ci.PrivKey, value path.Path) error {
return errors.New("not implemented for mockNamesys")
}
......
......@@ -68,23 +68,24 @@ func resolveAsyncDo(ctx context.Context, r resolver, name string, options opts.R
for {
select {
case res, ok := <-resCh:
if res.err != nil {
outCh <- Result{err: res.err}
return
}
if !ok {
resCh = nil
continue
}
if res.err != nil {
outCh <- Result{Err: res.err}
return
}
log.Debugf("resolved %s to %s", name, res.value.String())
if strings.HasPrefix(res.value.String(), "/ipfs/") {
outCh <- Result{err: res.err}
outCh <- Result{Err: res.err}
continue
}
p := strings.TrimPrefix(res.value.String(), prefix)
if depth == 1 {
outCh <- Result{err: ErrResolveRecursion}
outCh <- Result{Err: ErrResolveRecursion}
continue
}
......@@ -99,17 +100,20 @@ func resolveAsyncDo(ctx context.Context, r resolver, name string, options opts.R
cancelSub()
}
subCtx, cancelSub = context.WithCancel(ctx)
defer cancelSub()
subCh = resolveAsyncDo(subCtx, r, p, subopts, prefix)
case res, ok := <-subCh:
if res.err != nil {
outCh <- Result{err: res.err}
return
}
if !ok {
subCh = nil
continue
}
if res.Err != nil {
outCh <- Result{Err: res.Err}
return
}
outCh <- res
case <-ctx.Done():
}
......
......@@ -31,6 +31,10 @@ func (r *DNSResolver) Resolve(ctx context.Context, name string, options ...opts.
return resolve(ctx, r, name, opts.ProcessOpts(options), "/ipns/")
}
func (r *DNSResolver) ResolveAsync(ctx context.Context, name string, options ...opts.ResolveOpt) <-chan Result {
return resolveAsync(ctx, r, name, opts.ProcessOpts(options), "/ipns/")
}
type lookupRes struct {
path path.Path
error error
......@@ -112,8 +116,8 @@ func (r *DNSResolver) resolveOnceAsync(ctx context.Context, name string, options
}
if subRes.error == nil {
select {
case out <- onceResult{value: subRes.path}:
case <-ctx.Done():
case out <- onceResult{value: subRes.path}:
case <-ctx.Done():
}
return
}
......
......@@ -65,8 +65,8 @@ type NameSystem interface {
// Result is the return type for Resolver.ResolveAsync.
type Result struct {
path path.Path
err error
Path path.Path
Err error
}
// Resolver is an object capable of resolving names.
......
......@@ -38,11 +38,15 @@ func testResolution(t *testing.T, resolver Resolver, name string, depth uint, ex
}
}
func (r *mockResolver) resolveOnce(ctx context.Context, name string, opts *opts.ResolveOpts) (path.Path, time.Duration, error) {
func (r *mockResolver) resolveOnce(ctx context.Context, name string, opts opts.ResolveOpts) (path.Path, time.Duration, error) {
p, err := path.ParsePath(r.entries[name])
return p, 0, err
}
func (r *mockResolver) resolveOnceAsync(ctx context.Context, name string, options opts.ResolveOpts) <-chan onceResult {
panic("stub")
}
func mockResolverOne() *mockResolver {
return &mockResolver{
entries: map[string]string{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment