-
Notifications
You must be signed in to change notification settings - Fork 4
/
server_routers.go
496 lines (408 loc) · 11.2 KB
/
server_routers.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
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
package main
import (
"context"
"errors"
"reflect"
"sync"
"time"
"github.com/ipfs/boxo/ipns"
"github.com/ipfs/boxo/routing/http/client"
"github.com/ipfs/boxo/routing/http/server"
"github.com/ipfs/boxo/routing/http/types"
"github.com/ipfs/boxo/routing/http/types/iter"
"github.com/ipfs/go-cid"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/routing"
manet "github.com/multiformats/go-multiaddr/net"
)
type router interface {
providersRouter
peersRouter
ipnsRouter
}
type providersRouter interface {
FindProviders(ctx context.Context, cid cid.Cid, limit int) (iter.ResultIter[types.Record], error)
}
type peersRouter interface {
FindPeers(ctx context.Context, pid peer.ID, limit int) (iter.ResultIter[*types.PeerRecord], error)
}
type ipnsRouter interface {
GetIPNS(ctx context.Context, name ipns.Name) (*ipns.Record, error)
PutIPNS(ctx context.Context, name ipns.Name, record *ipns.Record) error
}
var _ server.ContentRouter = composableRouter{}
type composableRouter struct {
providers providersRouter
peers peersRouter
ipns ipnsRouter
}
func (r composableRouter) FindProviders(ctx context.Context, key cid.Cid, limit int) (iter.ResultIter[types.Record], error) {
if r.providers == nil {
return iter.ToResultIter(iter.FromSlice([]types.Record{})), nil
}
return r.providers.FindProviders(ctx, key, limit)
}
func (r composableRouter) FindPeers(ctx context.Context, pid peer.ID, limit int) (iter.ResultIter[*types.PeerRecord], error) {
if r.peers == nil {
return iter.ToResultIter(iter.FromSlice([]*types.PeerRecord{})), nil
}
return r.peers.FindPeers(ctx, pid, limit)
}
func (r composableRouter) GetIPNS(ctx context.Context, name ipns.Name) (*ipns.Record, error) {
if r.ipns == nil {
return nil, routing.ErrNotFound
}
return r.ipns.GetIPNS(ctx, name)
}
func (r composableRouter) PutIPNS(ctx context.Context, name ipns.Name, record *ipns.Record) error {
if r.ipns == nil {
return nil
}
return r.ipns.PutIPNS(ctx, name, record)
}
//lint:ignore SA1019 // ignore staticcheck
func (r composableRouter) ProvideBitswap(ctx context.Context, req *server.BitswapWriteProvideRequest) (time.Duration, error) {
return 0, routing.ErrNotSupported
}
var _ server.ContentRouter = parallelRouter{}
type parallelRouter struct {
routers []router
}
func (r parallelRouter) FindProviders(ctx context.Context, key cid.Cid, limit int) (iter.ResultIter[types.Record], error) {
return find(ctx, r.routers, func(ri router) (iter.ResultIter[types.Record], error) {
return ri.FindProviders(ctx, key, limit)
})
}
func (r parallelRouter) FindPeers(ctx context.Context, pid peer.ID, limit int) (iter.ResultIter[*types.PeerRecord], error) {
return find(ctx, r.routers, func(ri router) (iter.ResultIter[*types.PeerRecord], error) {
return ri.FindPeers(ctx, pid, limit)
})
}
func find[T any](ctx context.Context, routers []router, call func(router) (iter.ResultIter[T], error)) (iter.ResultIter[T], error) {
switch len(routers) {
case 0:
return iter.ToResultIter(iter.FromSlice([]T{})), nil
case 1:
return call(routers[0])
}
its := make([]iter.ResultIter[T], 0, len(routers))
var err error
for _, ri := range routers {
it, itErr := call(ri)
if itErr != nil {
logger.Warnf("error from router: %w", itErr)
err = errors.Join(err, itErr)
} else {
its = append(its, it)
}
}
// If all iterators failed to be created, then return the error.
if len(its) == 0 {
logger.Warnf("failed to create all iterators: %w", err)
return nil, err
} else if err != nil {
logger.Warnf("failed to create some iterators: %w", err)
}
// Otherwise return manyIter with remaining iterators.
return newManyIter(ctx, its), nil
}
type manyIter[T any] struct {
ctx context.Context
cancel context.CancelFunc
wg sync.WaitGroup
its []iter.ResultIter[T]
ch chan iter.Result[T]
val iter.Result[T]
done bool
}
func newManyIter[T any](ctx context.Context, its []iter.ResultIter[T]) *manyIter[T] {
ctx, cancel := context.WithCancel(ctx)
mi := &manyIter[T]{
ctx: ctx,
cancel: cancel,
its: its,
ch: make(chan iter.Result[T]),
}
for _, it := range its {
mi.wg.Add(1)
go func(it iter.ResultIter[T]) {
defer mi.wg.Done()
for it.Next() {
select {
case mi.ch <- it.Val():
case <-ctx.Done():
return
}
}
}(it)
}
go func() {
mi.wg.Wait()
close(mi.ch)
}()
return mi
}
func (mi *manyIter[T]) Next() bool {
if mi.done {
return false
}
select {
case val, ok := <-mi.ch:
if ok {
mi.val = val
} else {
mi.done = true
}
case <-mi.ctx.Done():
mi.done = true
}
return !mi.done
}
func (mi *manyIter[T]) Val() iter.Result[T] {
return mi.val
}
func (mi *manyIter[T]) Close() error {
mi.done = true
mi.cancel()
mi.wg.Wait()
var err error
for _, it := range mi.its {
err = errors.Join(err, it.Close())
}
if err != nil {
logger.Warnf("errors on closing iterators: %w", err)
}
return err
}
func (r parallelRouter) GetIPNS(ctx context.Context, name ipns.Name) (*ipns.Record, error) {
switch len(r.routers) {
case 0:
return nil, routing.ErrNotFound
case 1:
return r.routers[0].GetIPNS(ctx, name)
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
results := make(chan struct {
val *ipns.Record
err error
})
for _, ri := range r.routers {
go func(ri router) {
value, err := ri.GetIPNS(ctx, name)
select {
case results <- struct {
val *ipns.Record
err error
}{
val: value,
err: err,
}:
case <-ctx.Done():
}
}(ri)
}
var errs error
for range r.routers {
select {
case res := <-results:
switch res.err {
case nil:
return res.val, nil
case routing.ErrNotFound, routing.ErrNotSupported:
continue
}
// If the context has expired, just return that error
// and ignore the other errors.
if ctx.Err() != nil {
return nil, ctx.Err()
}
errs = errors.Join(errs, res.err)
case <-ctx.Done():
return nil, ctx.Err()
}
}
if errs == nil {
return nil, routing.ErrNotFound
}
return nil, errs
}
func (r parallelRouter) PutIPNS(ctx context.Context, name ipns.Name, record *ipns.Record) error {
switch len(r.routers) {
case 0:
return nil
case 1:
return r.routers[0].PutIPNS(ctx, name, record)
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
var wg sync.WaitGroup
results := make([]error, len(r.routers))
wg.Add(len(r.routers))
for i, ri := range r.routers {
go func(ri router, i int) {
results[i] = ri.PutIPNS(ctx, name, record)
wg.Done()
}(ri, i)
}
wg.Wait()
var errs error
for _, err := range results {
errs = errors.Join(errs, err)
}
return errs
}
//lint:ignore SA1019 // ignore staticcheck
func (r parallelRouter) ProvideBitswap(ctx context.Context, req *server.BitswapWriteProvideRequest) (time.Duration, error) {
return 0, routing.ErrNotSupported
}
var _ router = libp2pRouter{}
type libp2pRouter struct {
routing routing.Routing
}
func (d libp2pRouter) FindProviders(ctx context.Context, key cid.Cid, limit int) (iter.ResultIter[types.Record], error) {
ctx, cancel := context.WithCancel(ctx)
ch := d.routing.FindProvidersAsync(ctx, key, limit)
return iter.ToResultIter(&peerChanIter{
ch: ch,
cancel: cancel,
}), nil
}
func (d libp2pRouter) FindPeers(ctx context.Context, pid peer.ID, limit int) (iter.ResultIter[*types.PeerRecord], error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
addr, err := d.routing.FindPeer(ctx, pid)
if err != nil {
return nil, err
}
rec := &types.PeerRecord{
Schema: types.SchemaPeer,
ID: &addr.ID,
}
for _, addr := range addr.Addrs {
rec.Addrs = append(rec.Addrs, types.Multiaddr{Multiaddr: addr})
}
return iter.ToResultIter(iter.FromSlice([]*types.PeerRecord{rec})), nil
}
func (d libp2pRouter) GetIPNS(ctx context.Context, name ipns.Name) (*ipns.Record, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
raw, err := d.routing.GetValue(ctx, string(name.RoutingKey()))
if err != nil {
return nil, err
}
return ipns.UnmarshalRecord(raw)
}
func (d libp2pRouter) PutIPNS(ctx context.Context, name ipns.Name, record *ipns.Record) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
raw, err := ipns.MarshalRecord(record)
if err != nil {
return err
}
return d.routing.PutValue(ctx, string(name.RoutingKey()), raw)
}
type peerChanIter struct {
ch <-chan peer.AddrInfo
cancel context.CancelFunc
next *peer.AddrInfo
}
func (it *peerChanIter) Next() bool {
addr, ok := <-it.ch
if ok {
it.next = &addr
return true
}
it.next = nil
return false
}
func (it *peerChanIter) Val() types.Record {
if it.next == nil {
return nil
}
rec := &types.PeerRecord{
Schema: types.SchemaPeer,
ID: &it.next.ID,
}
for _, addr := range it.next.Addrs {
rec.Addrs = append(rec.Addrs, types.Multiaddr{Multiaddr: addr})
}
return rec
}
func (it *peerChanIter) Close() error {
it.cancel()
return nil
}
var _ router = clientRouter{}
type clientRouter struct {
*client.Client
}
func (d clientRouter) FindProviders(ctx context.Context, cid cid.Cid, limit int) (iter.ResultIter[types.Record], error) {
return d.Client.FindProviders(ctx, cid)
}
func (d clientRouter) FindPeers(ctx context.Context, pid peer.ID, limit int) (iter.ResultIter[*types.PeerRecord], error) {
return d.Client.FindPeers(ctx, pid)
}
var _ server.ContentRouter = sanitizeRouter{}
type sanitizeRouter struct {
router
}
func (r sanitizeRouter) FindProviders(ctx context.Context, key cid.Cid, limit int) (iter.ResultIter[types.Record], error) {
it, err := r.router.FindProviders(ctx, key, limit)
if err != nil {
return nil, err
}
return iter.Map(it, func(v iter.Result[types.Record]) iter.Result[types.Record] {
if v.Err != nil || v.Val == nil {
return v
}
switch v.Val.GetSchema() {
case types.SchemaPeer:
result, ok := v.Val.(*types.PeerRecord)
if !ok {
logger.Errorw("problem casting find providers result", "Schema", v.Val.GetSchema(), "Type", reflect.TypeOf(v).String())
return v
}
result.Addrs = filterPrivateMultiaddr(result.Addrs)
v.Val = result
//lint:ignore SA1019 // ignore staticcheck
case types.SchemaBitswap:
//lint:ignore SA1019 // ignore staticcheck
result, ok := v.Val.(*types.BitswapRecord)
if !ok {
logger.Errorw("problem casting find providers result", "Schema", v.Val.GetSchema(), "Type", reflect.TypeOf(v).String())
return v
}
result.Addrs = filterPrivateMultiaddr(result.Addrs)
v.Val = result
}
return v
}), nil
}
func (r sanitizeRouter) FindPeers(ctx context.Context, pid peer.ID, limit int) (iter.ResultIter[*types.PeerRecord], error) {
it, err := r.router.FindPeers(ctx, pid, limit)
if err != nil {
return nil, err
}
return iter.Map(it, func(v iter.Result[*types.PeerRecord]) iter.Result[*types.PeerRecord] {
if v.Err != nil || v.Val == nil {
return v
}
v.Val.Addrs = filterPrivateMultiaddr(v.Val.Addrs)
return v
}), nil
}
//lint:ignore SA1019 // ignore staticcheck
func (r sanitizeRouter) ProvideBitswap(ctx context.Context, req *server.BitswapWriteProvideRequest) (time.Duration, error) {
return 0, routing.ErrNotSupported
}
func filterPrivateMultiaddr(a []types.Multiaddr) []types.Multiaddr {
b := make([]types.Multiaddr, 0, len(a))
for _, addr := range a {
if manet.IsPrivateAddr(addr.Multiaddr) {
continue
}
b = append(b, addr)
}
return b
}