diff --git a/gateway/utilities_test.go b/gateway/utilities_test.go index 76359dcdd..85153f808 100644 --- a/gateway/utilities_test.go +++ b/gateway/utilities_test.go @@ -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) @@ -79,13 +79,13 @@ 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 @@ -93,13 +93,13 @@ func (m mockNamesys) Resolve(ctx context.Context, p path.Path, opts ...namesys.R } 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 } diff --git a/namesys/dns_resolver.go b/namesys/dns_resolver.go index fb780ca8b..ef46ce399 100644 --- a/namesys/dns_resolver.go +++ b/namesys/dns_resolver.go @@ -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))) defer span.End() return resolveAsync(ctx, r, p, ProcessResolveOptions(options)) } -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 } 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)} close(out) return out } @@ -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() { @@ -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 @@ -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 { @@ -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}) } return } @@ -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() @@ -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) { diff --git a/namesys/interface.go b/namesys/interface.go index c248551ec..21c93126c 100644 --- a/namesys/interface.go +++ b/namesys/interface.go @@ -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 @@ -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. diff --git a/namesys/ipns_resolver.go b/namesys/ipns_resolver.go index 486f1d541..5efcf8785 100644 --- a/namesys/ipns_resolver.go +++ b/namesys/ipns_resolver.go @@ -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)) } -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 } @@ -70,7 +70,7 @@ 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 @@ -78,7 +78,7 @@ func (r *IPNSResolver) resolveOnceAsync(ctx context.Context, p path.Path, option 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 @@ -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 } resolvedBase, err := rec.Value() if err != nil { - emitOnceResult(ctx, out, ResolveAsyncResult{Err: err}) + emitOnceResult(ctx, out, AsyncResult{Err: err}) return } resolvedBase, err = joinPaths(resolvedBase, p) if err != nil { - emitOnceResult(ctx, out, ResolveAsyncResult{Err: err}) + emitOnceResult(ctx, out, AsyncResult{Err: err}) return } ttl, err := calculateBestTTL(rec) if err != nil { - emitOnceResult(ctx, out, ResolveAsyncResult{Err: err}) + emitOnceResult(ctx, out, AsyncResult{Err: err}) return } // 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 } diff --git a/namesys/namesys.go b/namesys/namesys.go index 8d44a041d..00b1f4d2d 100644 --- a/namesys/namesys.go +++ b/namesys/namesys.go @@ -138,14 +138,14 @@ 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))) defer span.End() @@ -153,13 +153,13 @@ func (ns *namesys) ResolveAsync(ctx context.Context, p path.Path, options ...Res } // 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 } @@ -167,7 +167,7 @@ func (ns *namesys) resolveOnceAsync(ctx context.Context, p path.Path, options Re 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 } @@ -176,7 +176,7 @@ func (ns *namesys) resolveOnceAsync(ctx context.Context, p path.Path, options Re p, err = joinPaths(resolvedBase, p) 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} close(out) return out } else { @@ -200,9 +200,9 @@ 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())} } close(out) @@ -210,14 +210,14 @@ func (ns *namesys) resolveOnceAsync(ctx context.Context, p path.Path, options Re } 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 @@ -233,7 +233,7 @@ func (ns *namesys) resolveOnceAsync(ctx context.Context, p path.Path, options Re res.Err = multierr.Combine(err, res.Err) } - 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 } @@ -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(): @@ -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 } return ns.Resolve(ctx, p) diff --git a/namesys/namesys_test.go b/namesys/namesys_test.go index 89c4ba3d4..41fa0ce88 100644 --- a/namesys/namesys_test.go +++ b/namesys/namesys_test.go @@ -36,10 +36,10 @@ func testResolution(t *testing.T, resolver Resolver, name string, depth uint, ex } } -func (r *mockResolver) resolveOnceAsync(ctx context.Context, p path.Path, options ResolveOptions) <-chan ResolveAsyncResult { +func (r *mockResolver) resolveOnceAsync(ctx context.Context, p path.Path, options ResolveOptions) <-chan AsyncResult { p, err := path.NewPath(r.entries[p.String()]) - out := make(chan ResolveAsyncResult, 1) - out <- ResolveAsyncResult{Path: p, Err: err} + out := make(chan AsyncResult, 1) + out <- AsyncResult{Path: p, Err: err} close(out) return out } diff --git a/namesys/utilities.go b/namesys/utilities.go index 2cf4a3871..cba7ee728 100644 --- a/namesys/utilities.go +++ b/namesys/utilities.go @@ -11,11 +11,11 @@ import ( ) type resolver interface { - resolveOnceAsync(context.Context, path.Path, ResolveOptions) <-chan ResolveAsyncResult + resolveOnceAsync(context.Context, path.Path, ResolveOptions) <-chan AsyncResult } // resolve is a helper for implementing Resolver.ResolveN using resolveOnce. -func resolve(ctx context.Context, r resolver, p path.Path, options ResolveOptions) (result ResolveResult, err error) { +func resolve(ctx context.Context, r resolver, p path.Path, options ResolveOptions) (result Result, err error) { ctx, cancel := context.WithCancel(ctx) defer cancel() @@ -32,20 +32,20 @@ func resolve(ctx context.Context, r resolver, p path.Path, options ResolveOption return result, err } -func resolveAsync(ctx context.Context, r resolver, p path.Path, options ResolveOptions) <-chan ResolveAsyncResult { +func resolveAsync(ctx context.Context, r resolver, p path.Path, options ResolveOptions) <-chan AsyncResult { ctx, span := startSpan(ctx, "ResolveAsync") defer span.End() resCh := r.resolveOnceAsync(ctx, p, options) depth := options.Depth - outCh := make(chan ResolveAsyncResult, 1) + outCh := make(chan AsyncResult, 1) go func() { defer close(outCh) ctx, span := startSpan(ctx, "ResolveAsync.Worker") defer span.End() - var subCh <-chan ResolveAsyncResult + var subCh <-chan AsyncResult var cancelSub context.CancelFunc defer func() { if cancelSub != nil { @@ -114,7 +114,7 @@ func resolveAsync(ctx context.Context, r resolver, p path.Path, options ResolveO return outCh } -func emitResult(ctx context.Context, outCh chan<- ResolveAsyncResult, r ResolveAsyncResult) { +func emitResult(ctx context.Context, outCh chan<- AsyncResult, r AsyncResult) { select { case outCh <- r: case <-ctx.Done():