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 ...@@ -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) { func (m mockNamesys) Resolve(ctx context.Context, name string, opts ...nsopts.ResolveOpt) (value path.Path, err error) {
cfg := nsopts.DefaultResolveOpts() cfg := nsopts.DefaultResolveOpts()
for _, o := range opts { for _, o := range opts {
o(cfg) o(&cfg)
} }
depth := cfg.Depth depth := cfg.Depth
if depth == nsopts.UnlimitedDepth { if depth == nsopts.UnlimitedDepth {
...@@ -57,6 +57,14 @@ func (m mockNamesys) Resolve(ctx context.Context, name string, opts ...nsopts.Re ...@@ -57,6 +57,14 @@ func (m mockNamesys) Resolve(ctx context.Context, name string, opts ...nsopts.Re
return value, nil 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 { func (m mockNamesys) Publish(ctx context.Context, name ci.PrivKey, value path.Path) error {
return errors.New("not implemented for mockNamesys") return errors.New("not implemented for mockNamesys")
} }
......
...@@ -68,23 +68,24 @@ func resolveAsyncDo(ctx context.Context, r resolver, name string, options opts.R ...@@ -68,23 +68,24 @@ func resolveAsyncDo(ctx context.Context, r resolver, name string, options opts.R
for { for {
select { select {
case res, ok := <-resCh: case res, ok := <-resCh:
if res.err != nil {
outCh <- Result{err: res.err}
return
}
if !ok { if !ok {
resCh = nil resCh = nil
continue continue
} }
if res.err != nil {
outCh <- Result{Err: res.err}
return
}
log.Debugf("resolved %s to %s", name, res.value.String()) log.Debugf("resolved %s to %s", name, res.value.String())
if strings.HasPrefix(res.value.String(), "/ipfs/") { if strings.HasPrefix(res.value.String(), "/ipfs/") {
outCh <- Result{err: res.err} outCh <- Result{Err: res.err}
continue continue
} }
p := strings.TrimPrefix(res.value.String(), prefix) p := strings.TrimPrefix(res.value.String(), prefix)
if depth == 1 { if depth == 1 {
outCh <- Result{err: ErrResolveRecursion} outCh <- Result{Err: ErrResolveRecursion}
continue continue
} }
...@@ -99,17 +100,20 @@ func resolveAsyncDo(ctx context.Context, r resolver, name string, options opts.R ...@@ -99,17 +100,20 @@ func resolveAsyncDo(ctx context.Context, r resolver, name string, options opts.R
cancelSub() cancelSub()
} }
subCtx, cancelSub = context.WithCancel(ctx) subCtx, cancelSub = context.WithCancel(ctx)
defer cancelSub()
subCh = resolveAsyncDo(subCtx, r, p, subopts, prefix) subCh = resolveAsyncDo(subCtx, r, p, subopts, prefix)
case res, ok := <-subCh: case res, ok := <-subCh:
if res.err != nil {
outCh <- Result{err: res.err}
return
}
if !ok { if !ok {
subCh = nil subCh = nil
continue continue
} }
if res.Err != nil {
outCh <- Result{Err: res.Err}
return
}
outCh <- res outCh <- res
case <-ctx.Done(): case <-ctx.Done():
} }
......
...@@ -31,6 +31,10 @@ func (r *DNSResolver) Resolve(ctx context.Context, name string, options ...opts. ...@@ -31,6 +31,10 @@ func (r *DNSResolver) Resolve(ctx context.Context, name string, options ...opts.
return resolve(ctx, r, name, opts.ProcessOpts(options), "/ipns/") 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 { type lookupRes struct {
path path.Path path path.Path
error error error error
......
...@@ -65,8 +65,8 @@ type NameSystem interface { ...@@ -65,8 +65,8 @@ type NameSystem interface {
// Result is the return type for Resolver.ResolveAsync. // Result is the return type for Resolver.ResolveAsync.
type Result struct { type Result struct {
path path.Path Path path.Path
err error Err error
} }
// Resolver is an object capable of resolving names. // Resolver is an object capable of resolving names.
......
...@@ -38,11 +38,15 @@ func testResolution(t *testing.T, resolver Resolver, name string, depth uint, ex ...@@ -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]) p, err := path.ParsePath(r.entries[name])
return p, 0, err return p, 0, err
} }
func (r *mockResolver) resolveOnceAsync(ctx context.Context, name string, options opts.ResolveOpts) <-chan onceResult {
panic("stub")
}
func mockResolverOne() *mockResolver { func mockResolverOne() *mockResolver {
return &mockResolver{ return &mockResolver{
entries: map[string]string{ 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