forked from ipfs/rainbow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_routing.go
More file actions
419 lines (362 loc) · 12.6 KB
/
setup_routing.go
File metadata and controls
419 lines (362 loc) · 12.6 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package main
import (
"context"
"fmt"
"net/http"
"time"
ocprom "contrib.go.opencensus.io/exporter/prometheus"
autoconf "github.com/ipfs/boxo/autoconf"
"github.com/ipfs/boxo/gateway"
"github.com/ipfs/boxo/ipns"
routingv1client "github.com/ipfs/boxo/routing/http/client"
httpcontentrouter "github.com/ipfs/boxo/routing/http/contentrouter"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
"github.com/libp2p/go-libp2p"
dht "github.com/libp2p/go-libp2p-kad-dht"
"github.com/libp2p/go-libp2p-kad-dht/fullrt"
record "github.com/libp2p/go-libp2p-record"
routinghelpers "github.com/libp2p/go-libp2p-routing-helpers"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/metrics"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/routing"
prometheus "github.com/prometheus/client_golang/prometheus"
"go.opencensus.io/stats/view"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)
func init() {
promRegistry, ok := prometheus.DefaultRegisterer.(*prometheus.Registry)
if !ok {
goLog.Error("routing metrics: error casting DefaultRegisterer")
return
}
pe, err := ocprom.NewExporter(ocprom.Options{
Namespace: "ipfs",
Registry: promRegistry,
OnError: func(err error) {
goLog.Errorf("ocprom error: %w", err)
},
})
if err != nil {
goLog.Errorf("routing metrics: error creating exporter: %w", err)
return
}
view.RegisterExporter(pe)
view.SetReportingPeriod(2 * time.Second)
}
func setupDelegatedRouting(cfg Config, dnsCache *cachedDNS) ([]routing.Routing, error) {
// Set configurable timeout with 30s default
timeout := cfg.HTTPRoutersTimeout
if timeout == 0 {
timeout = 30 * time.Second
}
// Increase per-host connection pool since we are making lots of concurrent requests.
httpClient := &http.Client{
Timeout: timeout,
Transport: otelhttp.NewTransport(
&routingv1client.ResponseBodyLimitedTransport{
RoundTripper: &customTransport{
// Roundtripper with increased defaults than http.Transport such that retrieving
// multiple lookups concurrently is fast.
RoundTripper: &http.Transport{
MaxIdleConns: 1000,
MaxConnsPerHost: 100,
MaxIdleConnsPerHost: 100,
IdleConnTimeout: 90 * time.Second,
DialContext: dnsCache.dialWithCachedDNS,
ForceAttemptHTTP2: true,
},
},
LimitBytes: 1 << 20,
}),
}
var delegatedRouters []routing.Routing
// Group endpoints by base URL and merge capabilities
// Rainbow only supports read operations (no IPNS publishing)
goLog.Debugf("Input endpoints: %v", cfg.RoutingV1Endpoints)
groupedEndpoints, err := autoconf.GroupByKnownCapabilities(cfg.RoutingV1Endpoints, true, false)
if err != nil {
return nil, err
}
goLog.Debugf("Grouped endpoints: %v", groupedEndpoints)
// Create a routing client for each unique base URL with appropriate capabilities
for baseURL, capabilities := range groupedEndpoints {
if capabilities.IsEmpty() {
goLog.Warnf("Skipping endpoint %s with no capabilities", baseURL)
continue
}
goLog.Debugf("Creating routing client for base URL %q with capabilities: Providers=%v, Peers=%v, IPNSGet=%v",
baseURL, capabilities.Providers, capabilities.Peers, capabilities.IPNSGet)
// Create HTTP routing client with base URL
delegatedRouter, err := delegatedHTTPContentRouterWithCapabilities(baseURL, capabilities,
routingv1client.WithHTTPClient(httpClient),
routingv1client.WithProtocolFilter(cfg.RoutingV1FilterProtocols), // IPIP-484
routingv1client.WithStreamResultsRequired(), // https://specs.ipfs.tech/routing/http-routing-v1/#streaming
routingv1client.WithDisabledLocalFiltering(false), // force local filtering in case remote server does not support IPIP-484
)
if err != nil {
return nil, err
}
delegatedRouters = append(delegatedRouters, delegatedRouter)
}
return delegatedRouters, nil
}
// parseBootstrapPeers parses a list of bootstrap peer strings into AddrInfo structs
// It skips empty strings and "auto" placeholders, and logs warnings for invalid peers
func parseBootstrapPeers(peers []string, warnOnAuto bool) ([]peer.AddrInfo, error) {
bootstrapPeers := make([]peer.AddrInfo, 0, len(peers))
for _, peerStr := range peers {
if peerStr == "" {
continue
}
if peerStr == autoconf.AutoPlaceholder {
if warnOnAuto {
goLog.Warnf("Bootstrap peer 'auto' placeholder not expanded - is autoconf enabled?")
}
continue
}
ai, err := peer.AddrInfoFromString(peerStr)
if err != nil {
goLog.Warnf("Failed to parse bootstrap peer %q: %v", peerStr, err)
continue
}
bootstrapPeers = append(bootstrapPeers, *ai)
}
return bootstrapPeers, nil
}
func setupDHTRouting(ctx context.Context, cfg Config, h host.Host, ds datastore.Batching, dhtRcMgr network.ResourceManager, bwc metrics.Reporter) (routing.Routing, error) {
if cfg.DHTRouting == DHTOff {
return nil, nil
}
// Parse bootstrap peers
bootstrapPeers, err := parseBootstrapPeers(cfg.Bootstrap, true)
if err != nil {
return nil, err
}
// If no bootstrap peers provided, use defaults for seed peering or error otherwise
if len(bootstrapPeers) == 0 {
if !cfg.SeedPeering {
return nil, fmt.Errorf("no valid bootstrap peers configured - provide bootstrap peers or enable autoconf")
}
// Use default bootstrap peers for seed peering
bootstrapPeers = dht.GetDefaultBootstrapPeerAddrInfos()
}
var dhtHost host.Host
if cfg.DHTSharedHost {
dhtHost = h
} else {
dhtHost, err = libp2p.New(
libp2p.UserAgent("rainbow/"+buildVersion()),
libp2p.NoListenAddrs,
libp2p.BandwidthReporter(bwc),
libp2p.DefaultTransports,
libp2p.DefaultMuxers,
libp2p.ResourceManager(dhtRcMgr),
)
if err != nil {
return nil, err
}
}
standardClient, err := dht.New(ctx, dhtHost,
dht.Datastore(ds),
dht.BootstrapPeers(bootstrapPeers...),
dht.Mode(dht.ModeClient),
)
if err != nil {
return nil, err
}
if cfg.DHTRouting == DHTStandard {
return standardClient, nil
}
if cfg.DHTRouting == DHTAccelerated {
fullRTClient, err := fullrt.NewFullRT(dhtHost, dht.DefaultPrefix,
fullrt.DHTOption(
dht.Validator(record.NamespacedValidator{
"pk": record.PublicKeyValidator{},
"ipns": ipns.Validator{KeyBook: h.Peerstore()},
}),
dht.Datastore(ds),
dht.BootstrapPeers(bootstrapPeers...),
dht.BucketSize(20),
))
if err != nil {
return nil, err
}
return &bundledDHT{
standard: standardClient,
fullRT: fullRTClient,
}, nil
}
return nil, fmt.Errorf("unknown DHTRouting option: %q", cfg.DHTRouting)
}
func setupCompositeRouting(delegatedRouters []routing.Routing, dht routing.Routing, cfg Config) routing.Routing {
// Default router is no routing at all: can be especially useful during tests.
var router routing.Routing
router = &routinghelpers.Null{}
if len(delegatedRouters) == 0 && dht != nil {
router = dht
} else {
var routers []*routinghelpers.ParallelRouter
if dht != nil {
routers = append(routers, &routinghelpers.ParallelRouter{
Router: dht,
ExecuteAfter: 0,
DoNotWaitForSearchValue: true,
IgnoreError: false,
})
}
timeout := cfg.RoutingTimeout
if timeout == 0 {
timeout = 30 * time.Second
}
for _, routingV1Router := range delegatedRouters {
routers = append(routers, &routinghelpers.ParallelRouter{
Timeout: timeout,
Router: routingV1Router,
ExecuteAfter: 0,
DoNotWaitForSearchValue: true,
IgnoreError: true,
})
}
if len(routers) > 0 {
router = routinghelpers.NewComposableParallel(routers)
}
}
return router
}
func setupRouting(ctx context.Context, cfg Config, h host.Host, ds datastore.Batching, dhtRcMgr network.ResourceManager, bwc metrics.Reporter, dnsCache *cachedDNS) (routing.ContentRouting, routing.PeerRouting, routing.ValueStore, error) {
delegatedRouters, err := setupDelegatedRouting(cfg, dnsCache)
if err != nil {
return nil, nil, nil, err
}
dhtRouter, err := setupDHTRouting(ctx, cfg, h, ds, dhtRcMgr, bwc)
if err != nil {
return nil, nil, nil, err
}
router := setupCompositeRouting(delegatedRouters, dhtRouter, cfg)
var (
cr routing.ContentRouting = router
pr routing.PeerRouting = router
vs routing.ValueStore = router
)
// If we're using a remote backend, but we also have libp2p enabled (e.g. for
// seed peering), we can still leverage the remote backend here.
if len(cfg.RemoteBackends) > 0 && cfg.RemoteBackendsIPNS {
remoteValueStore, err := gateway.NewRemoteValueStore(cfg.RemoteBackends, nil)
if err != nil {
return nil, nil, nil, err
}
vs = setupCompositeRouting(append(delegatedRouters, &routinghelpers.Compose{
ValueStore: remoteValueStore,
}), dhtRouter, cfg)
}
// If we're using seed peering, we need to run a lighter Amino DHT for the
// peering routing. We need to run a separate DHT with the main host if
// the shared host is disabled, or if we're not running any DHT at all.
if cfg.SeedPeering && (!cfg.DHTSharedHost || dhtRouter == nil) {
// Parse bootstrap peers for seed peering DHT (don't warn on auto since it's expected)
seedBootstrapPeers, err := parseBootstrapPeers(cfg.Bootstrap, false)
if err != nil {
return nil, nil, nil, err
}
// Use provided bootstrap peers or fall back to defaults
dhtOpts := []dht.Option{
dht.Datastore(ds),
dht.Mode(dht.ModeClient),
}
if len(seedBootstrapPeers) > 0 {
dhtOpts = append(dhtOpts, dht.BootstrapPeers(seedBootstrapPeers...))
} else {
// Use default bootstrap peers if none provided
dhtOpts = append(dhtOpts, dht.BootstrapPeers(dht.GetDefaultBootstrapPeerAddrInfos()...))
}
pr, err = dht.New(ctx, h, dhtOpts...)
if err != nil {
return nil, nil, nil, err
}
}
return cr, pr, vs, nil
}
func setupRoutingNoLibp2p(cfg Config, dnsCache *cachedDNS) (routing.ValueStore, error) {
delegatedRouters, err := setupDelegatedRouting(cfg, dnsCache)
if err != nil {
return nil, err
}
if len(cfg.RemoteBackends) > 0 && cfg.RemoteBackendsIPNS {
remoteValueStore, err := gateway.NewRemoteValueStore(cfg.RemoteBackends, nil)
if err != nil {
return nil, err
}
delegatedRouters = append(delegatedRouters, &routinghelpers.Compose{
ValueStore: remoteValueStore,
})
}
return setupCompositeRouting(delegatedRouters, nil, cfg), nil
}
type bundledDHT struct {
standard *dht.IpfsDHT
fullRT *fullrt.FullRT
}
func (b *bundledDHT) getDHT() routing.Routing {
if b.fullRT.Ready() {
return b.fullRT
}
return b.standard
}
func (b *bundledDHT) Provide(ctx context.Context, c cid.Cid, brdcst bool) error {
return b.getDHT().Provide(ctx, c, brdcst)
}
func (b *bundledDHT) FindProvidersAsync(ctx context.Context, c cid.Cid, i int) <-chan peer.AddrInfo {
return b.getDHT().FindProvidersAsync(ctx, c, i)
}
func (b *bundledDHT) FindPeer(ctx context.Context, id peer.ID) (peer.AddrInfo, error) {
return b.getDHT().FindPeer(ctx, id)
}
func (b *bundledDHT) PutValue(ctx context.Context, k string, v []byte, option ...routing.Option) error {
return b.getDHT().PutValue(ctx, k, v, option...)
}
func (b *bundledDHT) GetValue(ctx context.Context, s string, option ...routing.Option) ([]byte, error) {
return b.getDHT().GetValue(ctx, s, option...)
}
func (b *bundledDHT) SearchValue(ctx context.Context, s string, option ...routing.Option) (<-chan []byte, error) {
return b.getDHT().SearchValue(ctx, s, option...)
}
func (b *bundledDHT) Bootstrap(ctx context.Context) error {
return b.standard.Bootstrap(ctx)
}
var _ routing.Routing = (*bundledDHT)(nil)
// delegatedHTTPContentRouterWithCapabilities creates a routing client with selective capabilities
func delegatedHTTPContentRouterWithCapabilities(baseURL string, capabilities autoconf.EndpointCapabilities, rv1Opts ...routingv1client.Option) (routing.Routing, error) {
// Create the HTTP routing client with base URL
cli, err := routingv1client.New(
baseURL,
append([]routingv1client.Option{
routingv1client.WithUserAgent("rainbow/" + buildVersion()),
}, rv1Opts...)...,
)
if err != nil {
return nil, err
}
cr := httpcontentrouter.NewContentRoutingClient(cli)
err = view.Register(routingv1client.OpenCensusViews...)
if err != nil {
return nil, fmt.Errorf("registering HTTP delegated routing views: %w", err)
}
// Create a composed router with selective capabilities
composer := &routinghelpers.Compose{}
// Enable operations based on capabilities
if capabilities.IPNSGet {
composer.ValueStore = cr
}
if capabilities.Peers {
composer.PeerRouting = cr
}
if capabilities.Providers {
composer.ContentRouting = cr
}
// Note: Rainbow doesn't support IPNS publishing (IPNSPut)
// so we don't need to handle that capability here
return composer, nil
}