@@ -23,6 +23,7 @@ import (
23
23
lru "github.com/hashicorp/golang-lru"
24
24
cid "github.com/ipfs/go-cid"
25
25
ds "github.com/ipfs/go-datastore"
26
+ dssync "github.com/ipfs/go-datastore/sync"
26
27
path "github.com/ipfs/go-path"
27
28
opts "github.com/ipfs/interface-go-ipfs-core/options/namesys"
28
29
isd "github.com/jbenet/go-is-domain"
@@ -42,22 +43,54 @@ import (
42
43
// It can only publish to: (a) IPFS routing naming.
43
44
//
44
45
type mpns struct {
46
+ ds ds.Datastore
47
+
45
48
dnsResolver , proquintResolver , ipnsResolver resolver
46
49
ipnsPublisher Publisher
47
50
48
51
staticMap map [string ]path.Path
49
52
cache * lru.Cache
50
53
}
51
54
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
60
71
}
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
61
94
62
95
// Prewarm namesys cache with static records for deterministic tests and debugging.
63
96
// 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
106
}
74
107
}
75
108
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 )
83
126
}
127
+
128
+ ns .proquintResolver = new (ProquintResolver )
129
+ ns .ipnsResolver = NewIpnsResolver (r )
130
+ ns .ipnsPublisher = NewIpnsPublisher (r , ns .ds )
131
+
132
+ return ns , nil
84
133
}
85
134
86
135
// DefaultResolverCacheTTL defines max ttl of a record placed in namesys cache.
0 commit comments