Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-namesys
Commits
ea4eec1d
Commit
ea4eec1d
authored
Apr 16, 2021
by
vyzo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
introduce functional options for NewNamesys constructor
parent
5443ef9d
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
64 additions
and
15 deletions
+64
-15
namesys.go
namesys.go
+64
-15
No files found.
namesys.go
View file @
ea4eec1d
...
@@ -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.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment