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

Commit 8bd5e9d

Browse files
committed
introduce functional options for NewNamesys constructor
1 parent 2c3c57c commit 8bd5e9d

File tree

1 file changed

+64
-15
lines changed

1 file changed

+64
-15
lines changed

namesys.go

Lines changed: 64 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
lru "github.com/hashicorp/golang-lru"
2424
cid "github.com/ipfs/go-cid"
2525
ds "github.com/ipfs/go-datastore"
26+
dssync "github.com/ipfs/go-datastore/sync"
2627
path "github.com/ipfs/go-path"
2728
opts "github.com/ipfs/interface-go-ipfs-core/options/namesys"
2829
isd "github.com/jbenet/go-is-domain"
@@ -42,22 +43,54 @@ import (
4243
// It can only publish to: (a) IPFS routing naming.
4344
//
4445
type mpns struct {
46+
ds ds.Datastore
47+
4548
dnsResolver, proquintResolver, ipnsResolver resolver
4649
ipnsPublisher Publisher
4750

4851
staticMap map[string]path.Path
4952
cache *lru.Cache
5053
}
5154

52-
// NewNameSystem will construct the IPFS naming system based on Routing
53-
func NewNameSystem(r routing.ValueStore, ds ds.Datastore, rslv madns.BasicResolver, cachesize int) NameSystem {
54-
var (
55-
cache *lru.Cache
56-
staticMap map[string]path.Path
57-
)
58-
if cachesize > 0 {
59-
cache, _ = lru.New(cachesize)
55+
type Option func(*mpns) error
56+
57+
// WithCache is an option that instructs the name system to use a (LRU) cache of the given size.
58+
func WithCache(size int) Option {
59+
return func(ns *mpns) error {
60+
if size <= 0 {
61+
return fmt.Errorf("invalid cache size %d; must be > 0", size)
62+
}
63+
64+
cache, err := lru.New(size)
65+
if err != nil {
66+
return err
67+
}
68+
69+
ns.cache = cache
70+
return nil
6071
}
72+
}
73+
74+
// WithDNSResolver is an option that supplies a custom DNS resolver to use instead of the system
75+
// default.
76+
func WithDNSResolver(rslv madns.BasicResolver) Option {
77+
return func(ns *mpns) error {
78+
ns.dnsResolver = NewDNSResolver(rslv.LookupTXT)
79+
return nil
80+
}
81+
}
82+
83+
// WithDatastore is an option that supplies a datastore to use instead of an in-memory map datastore.
84+
func WithDatastore(ds ds.Datastore) Option {
85+
return func(ns *mpns) error {
86+
ns.ds = ds
87+
return nil
88+
}
89+
}
90+
91+
// NewNameSystem will construct the IPFS naming system based on Routing
92+
func NewNameSystem(r routing.ValueStore, opts ...Option) (NameSystem, error) {
93+
var staticMap map[string]path.Path
6194

6295
// Prewarm namesys cache with static records for deterministic tests and debugging.
6396
// 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
73106
}
74107
}
75108

76-
return &mpns{
77-
dnsResolver: NewDNSResolver(rslv.LookupTXT),
78-
proquintResolver: new(ProquintResolver),
79-
ipnsResolver: NewIpnsResolver(r),
80-
ipnsPublisher: NewIpnsPublisher(r, ds),
81-
staticMap: staticMap,
82-
cache: cache,
109+
ns := &mpns{
110+
staticMap: staticMap,
111+
}
112+
113+
for _, opt := range opts {
114+
err := opt(ns)
115+
if err != nil {
116+
return nil, err
117+
}
118+
}
119+
120+
if ns.ds == nil {
121+
ns.ds = dssync.MutexWrap(ds.NewMapDatastore())
122+
}
123+
124+
if ns.dnsResolver == nil {
125+
ns.dnsResolver = NewDNSResolver(madns.DefaultResolver.LookupTXT)
83126
}
127+
128+
ns.proquintResolver = new(ProquintResolver)
129+
ns.ipnsResolver = NewIpnsResolver(r)
130+
ns.ipnsPublisher = NewIpnsPublisher(r, ns.ds)
131+
132+
return ns, nil
84133
}
85134

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

0 commit comments

Comments
 (0)