Skip to content
Permalink
Newer
Older
100644 74 lines (64 sloc) 1.96 KB
1
package nsopts
2
3
import (
4
"time"
5
)
6
7
const (
8
// DefaultDepthLimit is the default depth limit used by Resolve.
9
DefaultDepthLimit = 32
10
11
// UnlimitedDepth allows infinite recursion in Resolve. You
12
// probably don't want to use this, but it's here if you absolutely
13
// trust resolution to eventually complete and can't put an upper
14
// limit on how many steps it will take.
15
UnlimitedDepth = 0
16
)
17
18
// ResolveOpts specifies options for resolving an IPNS path
19
type ResolveOpts struct {
20
// Recursion depth limit
21
Depth uint
22
// The number of IPNS records to retrieve from the DHT
23
// (the best record is selected from this set)
24
DhtRecordCount uint
25
// The amount of time to wait for DHT records to be fetched
26
// and verified. A zero value indicates that there is no explicit
27
// timeout (although there is an implicit timeout due to dial
28
// timeouts within the DHT)
29
DhtTimeout time.Duration
30
}
31
32
// DefaultResolveOpts returns the default options for resolving
33
// an IPNS path
34
func DefaultResolveOpts() ResolveOpts {
35
return ResolveOpts{
36
Depth: DefaultDepthLimit,
37
DhtRecordCount: 16,
38
DhtTimeout: time.Minute,
39
}
40
}
41
42
// ResolveOpt is used to set an option
43
type ResolveOpt func(*ResolveOpts)
44
45
// Depth is the recursion depth limit
46
func Depth(depth uint) ResolveOpt {
47
return func(o *ResolveOpts) {
48
o.Depth = depth
49
}
50
}
51
52
// DhtRecordCount is the number of IPNS records to retrieve from the DHT
53
func DhtRecordCount(count uint) ResolveOpt {
54
return func(o *ResolveOpts) {
55
o.DhtRecordCount = count
56
}
57
}
58
59
// DhtTimeout is the amount of time to wait for DHT records to be fetched
60
// and verified. A zero value indicates that there is no explicit timeout
61
func DhtTimeout(timeout time.Duration) ResolveOpt {
62
return func(o *ResolveOpts) {
63
o.DhtTimeout = timeout
64
}
65
}
66
67
// ProcessOpts converts an array of ResolveOpt into a ResolveOpts object
68
func ProcessOpts(opts []ResolveOpt) ResolveOpts {
69
rsopts := DefaultResolveOpts()
70
for _, option := range opts {
71
option(&rsopts)
72
}
73
return rsopts
74
}