Commit ea4eec1d authored by vyzo's avatar vyzo

introduce functional options for NewNamesys constructor

parent 5443ef9d
...@@ -23,6 +23,7 @@ import ( ...@@ -23,6 +23,7 @@ import (
lru "github.com/hashicorp/golang-lru" lru "github.com/hashicorp/golang-lru"
cid "github.com/ipfs/go-cid" cid "github.com/ipfs/go-cid"
ds "github.com/ipfs/go-datastore" ds "github.com/ipfs/go-datastore"
dssync "github.com/ipfs/go-datastore/sync"
path "github.com/ipfs/go-path" path "github.com/ipfs/go-path"
opts "github.com/ipfs/interface-go-ipfs-core/options/namesys" opts "github.com/ipfs/interface-go-ipfs-core/options/namesys"
isd "github.com/jbenet/go-is-domain" isd "github.com/jbenet/go-is-domain"
...@@ -42,6 +43,8 @@ import ( ...@@ -42,6 +43,8 @@ import (
// It can only publish to: (a) IPFS routing naming. // It can only publish to: (a) IPFS routing naming.
// //
type mpns struct { type mpns struct {
ds ds.Datastore
dnsResolver, proquintResolver, ipnsResolver resolver dnsResolver, proquintResolver, ipnsResolver resolver
ipnsPublisher Publisher ipnsPublisher Publisher
...@@ -49,15 +52,45 @@ type mpns struct { ...@@ -49,15 +52,45 @@ type mpns struct {
cache *lru.Cache cache *lru.Cache
} }
// NewNameSystem will construct the IPFS naming system based on Routing type Option func(*mpns) error
func NewNameSystem(r routing.ValueStore, ds ds.Datastore, rslv madns.BasicResolver, cachesize int) NameSystem {
var ( // WithCache is an option that instructs the name system to use a (LRU) cache of the given size.
cache *lru.Cache func WithCache(size int) Option {
staticMap map[string]path.Path return func(ns *mpns) error {
) if size <= 0 {
if cachesize > 0 { return fmt.Errorf("invalid cache size %d; must be > 0", size)
cache, _ = lru.New(cachesize) }
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. // Prewarm namesys cache with static records for deterministic tests and debugging.
// Useful for testing things like DNSLink without real DNS lookup. // Useful for testing things like DNSLink without real DNS lookup.
...@@ -73,14 +106,30 @@ func NewNameSystem(r routing.ValueStore, ds ds.Datastore, rslv madns.BasicResolv ...@@ -73,14 +106,30 @@ func NewNameSystem(r routing.ValueStore, ds ds.Datastore, rslv madns.BasicResolv
} }
} }
return &mpns{ ns := &mpns{
dnsResolver: NewDNSResolver(rslv.LookupTXT), staticMap: staticMap,
proquintResolver: new(ProquintResolver), }
ipnsResolver: NewIpnsResolver(r),
ipnsPublisher: NewIpnsPublisher(r, ds), for _, opt := range opts {
staticMap: staticMap, err := opt(ns)
cache: cache, 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. // DefaultResolverCacheTTL defines max ttl of a record placed in namesys cache.
......
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