Skip to content

Commit

Permalink
refactor: rename Resolve*Result to *Result
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Oct 18, 2023
1 parent 8d4e7dd commit d4411ba
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 64 deletions.
14 changes: 7 additions & 7 deletions gateway/utilities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func newMockNamesysItem(p path.Path, ttl time.Duration) *mockNamesysItem {

type mockNamesys map[string]*mockNamesysItem

func (m mockNamesys) Resolve(ctx context.Context, p path.Path, opts ...namesys.ResolveOption) (result namesys.ResolveResult, err error) {
func (m mockNamesys) Resolve(ctx context.Context, p path.Path, opts ...namesys.ResolveOption) (result namesys.Result, err error) {
cfg := namesys.DefaultResolveOptions()
for _, o := range opts {
o(&cfg)
Expand All @@ -79,27 +79,27 @@ func (m mockNamesys) Resolve(ctx context.Context, p path.Path, opts ...namesys.R
name := path.SegmentsToString(p.Segments()[:2]...)
for strings.HasPrefix(name, "/ipns/") {
if depth == 0 {
return namesys.ResolveResult{Path: value, TTL: ttl}, namesys.ErrResolveRecursion
return namesys.Result{Path: value, TTL: ttl}, namesys.ErrResolveRecursion
}
depth--

v, ok := m[name]
if !ok {
return namesys.ResolveResult{}, namesys.ErrResolveFailed
return namesys.Result{}, namesys.ErrResolveFailed
}
value = v.path
ttl = v.ttl
name = value.String()
}

value, err = path.Join(value, p.Segments()[2:]...)
return namesys.ResolveResult{Path: value, TTL: ttl}, err
return namesys.Result{Path: value, TTL: ttl}, err
}

func (m mockNamesys) ResolveAsync(ctx context.Context, p path.Path, opts ...namesys.ResolveOption) <-chan namesys.ResolveAsyncResult {
out := make(chan namesys.ResolveAsyncResult, 1)
func (m mockNamesys) ResolveAsync(ctx context.Context, p path.Path, opts ...namesys.ResolveOption) <-chan namesys.AsyncResult {
out := make(chan namesys.AsyncResult, 1)
res, err := m.Resolve(ctx, p, opts...)
out <- namesys.ResolveAsyncResult{Path: res.Path, TTL: res.TTL, LastMod: res.LastMod, Err: err}
out <- namesys.AsyncResult{Path: res.Path, TTL: res.TTL, LastMod: res.LastMod, Err: err}
close(out)
return out
}
Expand Down
30 changes: 15 additions & 15 deletions namesys/dns_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,34 @@ func NewDNSResolver(lookup LookupTXTFunc) *DNSResolver {
return &DNSResolver{lookupTXT: lookup}
}

func (r *DNSResolver) Resolve(ctx context.Context, p path.Path, options ...ResolveOption) (ResolveResult, error) {
func (r *DNSResolver) Resolve(ctx context.Context, p path.Path, options ...ResolveOption) (Result, error) {
ctx, span := startSpan(ctx, "DNSResolver.Resolve", trace.WithAttributes(attribute.Stringer("Path", p)))
defer span.End()

return resolve(ctx, r, p, ProcessResolveOptions(options))
}

func (r *DNSResolver) ResolveAsync(ctx context.Context, p path.Path, options ...ResolveOption) <-chan ResolveAsyncResult {
func (r *DNSResolver) ResolveAsync(ctx context.Context, p path.Path, options ...ResolveOption) <-chan AsyncResult {
ctx, span := startSpan(ctx, "DNSResolver.ResolveAsync", trace.WithAttributes(attribute.Stringer("Path", p)))

Check warning on line 42 in namesys/dns_resolver.go

View check run for this annotation

Codecov / codecov/patch

namesys/dns_resolver.go#L41-L42

Added lines #L41 - L42 were not covered by tests
defer span.End()

return resolveAsync(ctx, r, p, ProcessResolveOptions(options))

Check warning on line 45 in namesys/dns_resolver.go

View check run for this annotation

Codecov / codecov/patch

namesys/dns_resolver.go#L45

Added line #L45 was not covered by tests
}

func (r *DNSResolver) resolveOnceAsync(ctx context.Context, p path.Path, options ResolveOptions) <-chan ResolveAsyncResult {
func (r *DNSResolver) resolveOnceAsync(ctx context.Context, p path.Path, options ResolveOptions) <-chan AsyncResult {
ctx, span := startSpan(ctx, "DNSResolver.ResolveOnceAsync", trace.WithAttributes(attribute.Stringer("Path", p)))
defer span.End()

out := make(chan ResolveAsyncResult, 1)
out := make(chan AsyncResult, 1)
if p.Namespace() != path.IPNSNamespace {
out <- ResolveAsyncResult{Err: fmt.Errorf("unsupported namespace: %q", p.Namespace())}
out <- AsyncResult{Err: fmt.Errorf("unsupported namespace: %q", p.Namespace())}
close(out)
return out
}

Check warning on line 57 in namesys/dns_resolver.go

View check run for this annotation

Codecov / codecov/patch

namesys/dns_resolver.go#L54-L57

Added lines #L54 - L57 were not covered by tests

fqdn := p.Segments()[1]
if _, ok := dns.IsDomainName(fqdn); !ok {
out <- ResolveAsyncResult{Err: fmt.Errorf("not a valid domain name: %q", fqdn)}
out <- AsyncResult{Err: fmt.Errorf("not a valid domain name: %q", fqdn)}

Check warning on line 61 in namesys/dns_resolver.go

View check run for this annotation

Codecov / codecov/patch

namesys/dns_resolver.go#L61

Added line #L61 was not covered by tests
close(out)
return out
}
Expand All @@ -69,10 +69,10 @@ func (r *DNSResolver) resolveOnceAsync(ctx context.Context, p path.Path, options
fqdn += "."
}

rootChan := make(chan ResolveAsyncResult, 1)
rootChan := make(chan AsyncResult, 1)
go workDomain(ctx, r, fqdn, rootChan)

subChan := make(chan ResolveAsyncResult, 1)
subChan := make(chan AsyncResult, 1)
go workDomain(ctx, r, "_dnslink."+fqdn, subChan)

go func() {
Expand All @@ -90,7 +90,7 @@ func (r *DNSResolver) resolveOnceAsync(ctx context.Context, p path.Path, options
}
if subRes.Err == nil {
p, err := joinPaths(subRes.Path, p)
emitOnceResult(ctx, out, ResolveAsyncResult{Path: p, LastMod: time.Now(), Err: err})
emitOnceResult(ctx, out, AsyncResult{Path: p, LastMod: time.Now(), Err: err})
// Return without waiting for rootRes, since this result
// (for "_dnslink."+fqdn) takes precedence
return
Expand All @@ -103,7 +103,7 @@ func (r *DNSResolver) resolveOnceAsync(ctx context.Context, p path.Path, options
}
if rootRes.Err == nil {
p, err := joinPaths(rootRes.Path, p)
emitOnceResult(ctx, out, ResolveAsyncResult{Path: p, LastMod: time.Now(), Err: err})
emitOnceResult(ctx, out, AsyncResult{Path: p, LastMod: time.Now(), Err: err})
// Do not return here. Wait for subRes so that it is
// output last if good, thereby giving subRes precedence.
} else {
Expand All @@ -120,7 +120,7 @@ func (r *DNSResolver) resolveOnceAsync(ctx context.Context, p path.Path, options
if rootResErr == ErrResolveFailed && subResErr == ErrResolveFailed {
// Wrap error so that it can be tested if it is a ErrResolveFailed
err := fmt.Errorf("%w: _dnslink subdomain at %q is missing a TXT record (https://docs.ipfs.tech/concepts/dnslink/)", ErrResolveFailed, gopath.Base(fqdn))
emitOnceResult(ctx, out, ResolveAsyncResult{Err: err})
emitOnceResult(ctx, out, AsyncResult{Err: err})

Check warning on line 123 in namesys/dns_resolver.go

View check run for this annotation

Codecov / codecov/patch

namesys/dns_resolver.go#L122-L123

Added lines #L122 - L123 were not covered by tests
}
return
}
Expand All @@ -130,7 +130,7 @@ func (r *DNSResolver) resolveOnceAsync(ctx context.Context, p path.Path, options
return out
}

func workDomain(ctx context.Context, r *DNSResolver, name string, res chan ResolveAsyncResult) {
func workDomain(ctx context.Context, r *DNSResolver, name string, res chan AsyncResult) {
ctx, span := startSpan(ctx, "DNSResolver.WorkDomain", trace.WithAttributes(attribute.String("Name", name)))
defer span.End()

Expand All @@ -146,20 +146,20 @@ func workDomain(ctx context.Context, r *DNSResolver, name string, res chan Resol
}
}
// Could not look up any text records for name
res <- ResolveAsyncResult{Err: err}
res <- AsyncResult{Err: err}
return
}

for _, t := range txt {
p, err := parseEntry(t)
if err == nil {
res <- ResolveAsyncResult{Path: p}
res <- AsyncResult{Path: p}
return
}
}

// There were no TXT records with a dnslink
res <- ResolveAsyncResult{Err: ErrResolveFailed}
res <- AsyncResult{Err: ErrResolveFailed}
}

func parseEntry(txt string) (path.Path, error) {
Expand Down
12 changes: 6 additions & 6 deletions namesys/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ type NameSystem interface {
Publisher
}

// ResolveResult is the return type for [Resolver.Resolve].
type ResolveResult struct {
// Result is the return type for [Resolver.Resolve].
type Result struct {
Path path.Path
TTL time.Duration
LastMod time.Time
}

// ResolveAsyncResult is the return type for [Resolver.ResolveAsync].
type ResolveAsyncResult struct {
// AsyncResult is the return type for [Resolver.ResolveAsync].
type AsyncResult struct {
Path path.Path
TTL time.Duration
LastMod time.Time
Expand Down Expand Up @@ -96,12 +96,12 @@ type Resolver interface {
//
// There is a default depth-limit to avoid infinite recursion. Most users will be fine with
// this default limit, but if you need to adjust the limit you can specify it as an option.
Resolve(context.Context, path.Path, ...ResolveOption) (ResolveResult, error)
Resolve(context.Context, path.Path, ...ResolveOption) (Result, error)

// ResolveAsync performs recursive name lookup, like Resolve, but it returns entries as
// they are discovered in the DHT. Each returned result is guaranteed to be "better"
// (which usually means newer) than the previous one.
ResolveAsync(context.Context, path.Path, ...ResolveOption) <-chan ResolveAsyncResult
ResolveAsync(context.Context, path.Path, ...ResolveOption) <-chan AsyncResult
}

// ResolveOptions specifies options for resolving an IPNS Path.
Expand Down
24 changes: 12 additions & 12 deletions namesys/ipns_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,27 @@ func NewIPNSResolver(route routing.ValueStore) *IPNSResolver {
}
}

func (r *IPNSResolver) Resolve(ctx context.Context, p path.Path, options ...ResolveOption) (ResolveResult, error) {
func (r *IPNSResolver) Resolve(ctx context.Context, p path.Path, options ...ResolveOption) (Result, error) {
ctx, span := startSpan(ctx, "IPNSResolver.Resolve", trace.WithAttributes(attribute.Stringer("Path", p)))
defer span.End()

return resolve(ctx, r, p, ProcessResolveOptions(options))
}

func (r *IPNSResolver) ResolveAsync(ctx context.Context, p path.Path, options ...ResolveOption) <-chan ResolveAsyncResult {
func (r *IPNSResolver) ResolveAsync(ctx context.Context, p path.Path, options ...ResolveOption) <-chan AsyncResult {
ctx, span := startSpan(ctx, "IPNSResolver.ResolveAsync", trace.WithAttributes(attribute.Stringer("Path", p)))
defer span.End()

return resolveAsync(ctx, r, p, ProcessResolveOptions(options))

Check warning on line 51 in namesys/ipns_resolver.go

View check run for this annotation

Codecov / codecov/patch

namesys/ipns_resolver.go#L47-L51

Added lines #L47 - L51 were not covered by tests
}

func (r *IPNSResolver) resolveOnceAsync(ctx context.Context, p path.Path, options ResolveOptions) <-chan ResolveAsyncResult {
func (r *IPNSResolver) resolveOnceAsync(ctx context.Context, p path.Path, options ResolveOptions) <-chan AsyncResult {
ctx, span := startSpan(ctx, "IPNSResolver.ResolveOnceAsync", trace.WithAttributes(attribute.Stringer("Path", p)))
defer span.End()

out := make(chan ResolveAsyncResult, 1)
out := make(chan AsyncResult, 1)
if p.Namespace() != path.IPNSNamespace {
out <- ResolveAsyncResult{Err: fmt.Errorf("unsupported namespace: %s", p.Namespace())}
out <- AsyncResult{Err: fmt.Errorf("unsupported namespace: %s", p.Namespace())}
close(out)
return out
}

Check warning on line 63 in namesys/ipns_resolver.go

View check run for this annotation

Codecov / codecov/patch

namesys/ipns_resolver.go#L60-L63

Added lines #L60 - L63 were not covered by tests
Expand All @@ -70,15 +70,15 @@ func (r *IPNSResolver) resolveOnceAsync(ctx context.Context, p path.Path, option

name, err := ipns.NameFromString(p.Segments()[1])
if err != nil {
out <- ResolveAsyncResult{Err: err}
out <- AsyncResult{Err: err}
close(out)
cancel()
return out
}

Check warning on line 77 in namesys/ipns_resolver.go

View check run for this annotation

Codecov / codecov/patch

namesys/ipns_resolver.go#L73-L77

Added lines #L73 - L77 were not covered by tests

vals, err := r.routing.SearchValue(ctx, string(name.RoutingKey()), dht.Quorum(int(options.DhtRecordCount)))
if err != nil {
out <- ResolveAsyncResult{Err: err}
out <- AsyncResult{Err: err}
close(out)
cancel()
return out
Expand All @@ -99,31 +99,31 @@ func (r *IPNSResolver) resolveOnceAsync(ctx context.Context, p path.Path, option

rec, err := ipns.UnmarshalRecord(val)
if err != nil {
emitOnceResult(ctx, out, ResolveAsyncResult{Err: err})
emitOnceResult(ctx, out, AsyncResult{Err: err})
return
}

Check warning on line 104 in namesys/ipns_resolver.go

View check run for this annotation

Codecov / codecov/patch

namesys/ipns_resolver.go#L102-L104

Added lines #L102 - L104 were not covered by tests

resolvedBase, err := rec.Value()
if err != nil {
emitOnceResult(ctx, out, ResolveAsyncResult{Err: err})
emitOnceResult(ctx, out, AsyncResult{Err: err})
return
}

Check warning on line 110 in namesys/ipns_resolver.go

View check run for this annotation

Codecov / codecov/patch

namesys/ipns_resolver.go#L108-L110

Added lines #L108 - L110 were not covered by tests

resolvedBase, err = joinPaths(resolvedBase, p)
if err != nil {
emitOnceResult(ctx, out, ResolveAsyncResult{Err: err})
emitOnceResult(ctx, out, AsyncResult{Err: err})
return
}

Check warning on line 116 in namesys/ipns_resolver.go

View check run for this annotation

Codecov / codecov/patch

namesys/ipns_resolver.go#L114-L116

Added lines #L114 - L116 were not covered by tests

ttl, err := calculateBestTTL(rec)
if err != nil {
emitOnceResult(ctx, out, ResolveAsyncResult{Err: err})
emitOnceResult(ctx, out, AsyncResult{Err: err})
return
}

Check warning on line 122 in namesys/ipns_resolver.go

View check run for this annotation

Codecov / codecov/patch

namesys/ipns_resolver.go#L120-L122

Added lines #L120 - L122 were not covered by tests

// TODO: in the future it would be interesting to set the last modified date
// as the date in which the record has been signed.
emitOnceResult(ctx, out, ResolveAsyncResult{Path: resolvedBase, TTL: ttl, LastMod: time.Now()})
emitOnceResult(ctx, out, AsyncResult{Path: resolvedBase, TTL: ttl, LastMod: time.Now()})
case <-ctx.Done():
return

Check warning on line 128 in namesys/ipns_resolver.go

View check run for this annotation

Codecov / codecov/patch

namesys/ipns_resolver.go#L127-L128

Added lines #L127 - L128 were not covered by tests
}
Expand Down
30 changes: 15 additions & 15 deletions namesys/namesys.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,36 +138,36 @@ func NewNameSystem(r routing.ValueStore, opts ...Option) (NameSystem, error) {
}

// Resolve implements Resolver.
func (ns *namesys) Resolve(ctx context.Context, p path.Path, options ...ResolveOption) (ResolveResult, error) {
func (ns *namesys) Resolve(ctx context.Context, p path.Path, options ...ResolveOption) (Result, error) {
ctx, span := startSpan(ctx, "namesys.Resolve", trace.WithAttributes(attribute.Stringer("Path", p)))
defer span.End()

return resolve(ctx, ns, p, ProcessResolveOptions(options))
}

func (ns *namesys) ResolveAsync(ctx context.Context, p path.Path, options ...ResolveOption) <-chan ResolveAsyncResult {
func (ns *namesys) ResolveAsync(ctx context.Context, p path.Path, options ...ResolveOption) <-chan AsyncResult {
ctx, span := startSpan(ctx, "namesys.ResolveAsync", trace.WithAttributes(attribute.Stringer("Path", p)))

Check warning on line 149 in namesys/namesys.go

View check run for this annotation

Codecov / codecov/patch

namesys/namesys.go#L148-L149

Added lines #L148 - L149 were not covered by tests
defer span.End()

return resolveAsync(ctx, ns, p, ProcessResolveOptions(options))

Check warning on line 152 in namesys/namesys.go

View check run for this annotation

Codecov / codecov/patch

namesys/namesys.go#L152

Added line #L152 was not covered by tests
}

// resolveOnce implements resolver.
func (ns *namesys) resolveOnceAsync(ctx context.Context, p path.Path, options ResolveOptions) <-chan ResolveAsyncResult {
func (ns *namesys) resolveOnceAsync(ctx context.Context, p path.Path, options ResolveOptions) <-chan AsyncResult {
ctx, span := startSpan(ctx, "namesys.ResolveOnceAsync", trace.WithAttributes(attribute.Stringer("Path", p)))
defer span.End()

out := make(chan ResolveAsyncResult, 1)
out := make(chan AsyncResult, 1)
if !p.Mutable() {
out <- ResolveAsyncResult{Path: p}
out <- AsyncResult{Path: p}
close(out)
return out
}

segments := p.Segments()
resolvablePath, err := path.NewPathFromSegments(segments[0], segments[1])
if err != nil {
out <- ResolveAsyncResult{Err: err}
out <- AsyncResult{Err: err}
close(out)
return out

Check warning on line 172 in namesys/namesys.go

View check run for this annotation

Codecov / codecov/patch

namesys/namesys.go#L170-L172

Added lines #L170 - L172 were not covered by tests
}
Expand All @@ -176,7 +176,7 @@ func (ns *namesys) resolveOnceAsync(ctx context.Context, p path.Path, options Re
p, err = joinPaths(resolvedBase, p)

Check warning on line 176 in namesys/namesys.go

View check run for this annotation

Codecov / codecov/patch

namesys/namesys.go#L176

Added line #L176 was not covered by tests
span.SetAttributes(attribute.Bool("CacheHit", true))
span.RecordError(err)
out <- ResolveAsyncResult{Path: p, TTL: ttl, LastMod: lastMod, Err: err}
out <- AsyncResult{Path: p, TTL: ttl, LastMod: lastMod, Err: err}

Check warning on line 179 in namesys/namesys.go

View check run for this annotation

Codecov / codecov/patch

namesys/namesys.go#L179

Added line #L179 was not covered by tests
close(out)
return out
} else {
Expand All @@ -200,24 +200,24 @@ func (ns *namesys) resolveOnceAsync(ctx context.Context, p path.Path, options Re
fixedCid := cid.NewCidV1(cid.Libp2pKey, ipnsCid.Hash()).String()
codecErr := fmt.Errorf("peer ID represented as CIDv1 require libp2p-key multicodec: retry with /ipns/%s", fixedCid)
log.Debugf("RoutingResolver: could not convert public key hash %q to peer ID: %s\n", segments[1], codecErr)
out <- ResolveAsyncResult{Err: codecErr}
out <- AsyncResult{Err: codecErr}
} else {
out <- ResolveAsyncResult{Err: fmt.Errorf("cannot resolve: %q", resolvablePath.String())}
out <- AsyncResult{Err: fmt.Errorf("cannot resolve: %q", resolvablePath.String())}
}

Check warning on line 206 in namesys/namesys.go

View check run for this annotation

Codecov / codecov/patch

namesys/namesys.go#L196-L206

Added lines #L196 - L206 were not covered by tests

close(out)
return out
}

resCh := res.resolveOnceAsync(ctx, resolvablePath, options)
var best ResolveAsyncResult
var best AsyncResult
go func() {
defer close(out)
for {
select {
case res, ok := <-resCh:
if !ok {
if best != (ResolveAsyncResult{}) {
if best != (AsyncResult{}) {
ns.cacheSet(resolvablePath.String(), best.Path, best.TTL, best.LastMod)
}
return
Expand All @@ -233,7 +233,7 @@ func (ns *namesys) resolveOnceAsync(ctx context.Context, p path.Path, options Re
res.Err = multierr.Combine(err, res.Err)

Check warning on line 233 in namesys/namesys.go

View check run for this annotation

Codecov / codecov/patch

namesys/namesys.go#L232-L233

Added lines #L232 - L233 were not covered by tests
}

emitOnceResult(ctx, out, ResolveAsyncResult{Path: p, TTL: res.TTL, LastMod: res.LastMod, Err: res.Err})
emitOnceResult(ctx, out, AsyncResult{Path: p, TTL: res.TTL, LastMod: res.LastMod, Err: res.Err})
case <-ctx.Done():
return
}
Expand All @@ -243,7 +243,7 @@ func (ns *namesys) resolveOnceAsync(ctx context.Context, p path.Path, options Re
return out
}

func emitOnceResult(ctx context.Context, outCh chan<- ResolveAsyncResult, r ResolveAsyncResult) {
func emitOnceResult(ctx context.Context, outCh chan<- AsyncResult, r AsyncResult) {
select {
case outCh <- r:
case <-ctx.Done():
Expand Down Expand Up @@ -294,12 +294,12 @@ func (ns *namesys) Publish(ctx context.Context, name ci.PrivKey, value path.Path
// Resolve is an utility function that takes a [NameSystem] and a [path.Path], and
// returns the result of [NameSystem.Resolve] for the given path. If the given namesys
// is nil, [ErrNoNamesys] is returned.
func Resolve(ctx context.Context, ns NameSystem, p path.Path) (ResolveResult, error) {
func Resolve(ctx context.Context, ns NameSystem, p path.Path) (Result, error) {
ctx, span := startSpan(ctx, "Resolve", trace.WithAttributes(attribute.Stringer("Path", p)))
defer span.End()

if ns == nil {
return ResolveResult{}, ErrNoNamesys
return Result{}, ErrNoNamesys
}

Check warning on line 303 in namesys/namesys.go

View check run for this annotation

Codecov / codecov/patch

namesys/namesys.go#L302-L303

Added lines #L302 - L303 were not covered by tests

return ns.Resolve(ctx, p)
Expand Down
Loading

0 comments on commit d4411ba

Please sign in to comment.