-
Notifications
You must be signed in to change notification settings - Fork 6
/
options.go
176 lines (156 loc) · 3.77 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package exchange
import (
"sync"
"time"
"github.com/ipfs/kubo/core"
iface "github.com/ipfs/kubo/core/coreiface"
"github.com/ipni/index-provider/engine"
dht "github.com/libp2p/go-libp2p-kad-dht"
"github.com/libp2p/go-libp2p/core/peer"
"go.uber.org/ratelimit"
)
type (
Option func(*options) error
ConfigUpdater func([]peer.ID) error
options struct {
authorizer peer.ID
authorizedPeers []peer.ID
allowTransientConnection bool
ipniPublishDisabled bool
poolHostMode bool
ipniPublishTicker *time.Ticker
ipniPublishChanBuffer int
ipniPublishMaxBatchSize int
ipniGetEndpoint string
ipniProviderEngineOpts []engine.Option
dhtProviderOpts []dht.Option
updateConfig ConfigUpdater
wg *sync.WaitGroup
ipfsApi iface.CoreAPI
pushRateLimiter ratelimit.Limiter
ipfsNode *core.IpfsNode
}
)
func newOptions(o ...Option) (*options, error) {
opts := options{
ipniPublishMaxBatchSize: 16 << 10,
ipniPublishChanBuffer: 1,
wg: nil,
pushRateLimiter: ratelimit.New(1000), // Default of 5 per second
}
for _, apply := range o {
if err := apply(&opts); err != nil {
return nil, err
}
}
if opts.ipniPublishTicker == nil {
opts.ipniPublishTicker = time.NewTicker(10 * time.Second)
}
if opts.ipniGetEndpoint == "" {
opts.ipniGetEndpoint = "https://cid.contact/cid/"
}
if opts.wg == nil {
opts.wg = new(sync.WaitGroup)
}
return &opts, nil
}
func WithUpdateConfig(updateConfig ConfigUpdater) Option {
return func(o *options) error {
o.updateConfig = updateConfig
return nil
}
}
// WithAuthorizer sets the peer ID that has permission to configure DAG exchange authorization.
// Defaults to authorization disabled.
func WithAuthorizer(a peer.ID) Option {
return func(o *options) error {
o.authorizer = a
return nil
}
}
func WithAuthorizedPeers(l []peer.ID) Option {
return func(o *options) error {
o.authorizedPeers = l
return nil
}
}
func WithAllowTransientConnection(t bool) Option {
return func(o *options) error {
o.allowTransientConnection = t
return nil
}
}
func WithIpniPublishDisabled(d bool) Option {
return func(o *options) error {
o.ipniPublishDisabled = d
return nil
}
}
func WithIpniPublishInterval(t time.Duration) Option {
return func(o *options) error {
o.ipniPublishTicker = time.NewTicker(t)
return nil
}
}
func WithIpniGetEndPoint(l string) Option {
return func(o *options) error {
o.ipniGetEndpoint = l
return nil
}
}
func WithPoolHostMode(n bool) Option {
return func(o *options) error {
o.poolHostMode = n
return nil
}
}
func WithIpniPublishChanBuffer(s int) Option {
return func(o *options) error {
o.ipniPublishChanBuffer = s
return nil
}
}
func WithIpniPublishMaxBatchSize(s int) Option {
return func(o *options) error {
o.ipniPublishMaxBatchSize = s
return nil
}
}
func WithIpniProviderEngineOptions(e ...engine.Option) Option {
return func(o *options) error {
o.ipniProviderEngineOpts = e
return nil
}
}
func WithDhtProviderOptions(d ...dht.Option) Option {
return func(o *options) error {
o.dhtProviderOpts = d
return nil
}
}
func WithWg(wg *sync.WaitGroup) Option {
return func(o *options) error {
o.wg = wg
return nil
}
}
func WithIPFSApi(ipfsApi iface.CoreAPI) Option {
return func(o *options) error {
o.ipfsApi = ipfsApi
return nil
}
}
func WithIPFSNode(ipfsNode *core.IpfsNode) Option {
return func(o *options) error {
o.ipfsNode = ipfsNode
return nil
}
}
// WithMaxPushRate sets the maximum number of CIDs pushed per second.
// Defaults to 5 per second.
func WithMaxPushRate(r int) Option {
return func(o *options) error {
o.pushRateLimiter = ratelimit.New(r)
return nil
}
}