Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
introduce functional options for NewNamesys constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
vyzo committed Apr 15, 2021
1 parent 2c3c57c commit 8bd5e9d
Showing 1 changed file with 64 additions and 15 deletions.
79 changes: 64 additions & 15 deletions namesys.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
lru "github.com/hashicorp/golang-lru"
cid "github.com/ipfs/go-cid"
ds "github.com/ipfs/go-datastore"
dssync "github.com/ipfs/go-datastore/sync"
path "github.com/ipfs/go-path"
opts "github.com/ipfs/interface-go-ipfs-core/options/namesys"
isd "github.com/jbenet/go-is-domain"
Expand All @@ -42,22 +43,54 @@ import (
// It can only publish to: (a) IPFS routing naming.
//
type mpns struct {
ds ds.Datastore

dnsResolver, proquintResolver, ipnsResolver resolver
ipnsPublisher Publisher

staticMap map[string]path.Path
cache *lru.Cache
}

// NewNameSystem will construct the IPFS naming system based on Routing
func NewNameSystem(r routing.ValueStore, ds ds.Datastore, rslv madns.BasicResolver, cachesize int) NameSystem {
var (
cache *lru.Cache
staticMap map[string]path.Path
)
if cachesize > 0 {
cache, _ = lru.New(cachesize)
type Option func(*mpns) error

// WithCache is an option that instructs the name system to use a (LRU) cache of the given size.
func WithCache(size int) Option {
return func(ns *mpns) error {
if size <= 0 {
return fmt.Errorf("invalid cache size %d; must be > 0", size)
}

cache, err := lru.New(size)
if err != nil {
return err
}

ns.cache = cache
return nil
}
}

// WithDNSResolver is an option that supplies a custom DNS resolver to use instead of the system
// default.
func WithDNSResolver(rslv madns.BasicResolver) Option {
return func(ns *mpns) error {
ns.dnsResolver = NewDNSResolver(rslv.LookupTXT)
return nil
}
}

// WithDatastore is an option that supplies a datastore to use instead of an in-memory map datastore.
func WithDatastore(ds ds.Datastore) Option {
return func(ns *mpns) error {
ns.ds = ds
return nil
}
}

// NewNameSystem will construct the IPFS naming system based on Routing
func NewNameSystem(r routing.ValueStore, opts ...Option) (NameSystem, error) {
var staticMap map[string]path.Path

// Prewarm namesys cache with static records for deterministic tests and debugging.
// Useful for testing things like DNSLink without real DNS lookup.
Expand All @@ -73,14 +106,30 @@ func NewNameSystem(r routing.ValueStore, ds ds.Datastore, rslv madns.BasicResolv
}
}

return &mpns{
dnsResolver: NewDNSResolver(rslv.LookupTXT),
proquintResolver: new(ProquintResolver),
ipnsResolver: NewIpnsResolver(r),
ipnsPublisher: NewIpnsPublisher(r, ds),
staticMap: staticMap,
cache: cache,
ns := &mpns{
staticMap: staticMap,
}

for _, opt := range opts {
err := opt(ns)
if err != nil {
return nil, err
}
}

if ns.ds == nil {
ns.ds = dssync.MutexWrap(ds.NewMapDatastore())
}

if ns.dnsResolver == nil {
ns.dnsResolver = NewDNSResolver(madns.DefaultResolver.LookupTXT)
}

ns.proquintResolver = new(ProquintResolver)
ns.ipnsResolver = NewIpnsResolver(r)
ns.ipnsPublisher = NewIpnsPublisher(r, ns.ds)

return ns, nil
}

// DefaultResolverCacheTTL defines max ttl of a record placed in namesys cache.
Expand Down

0 comments on commit 8bd5e9d

Please sign in to comment.