@@ -25,12 +25,12 @@ type router interface {
25
25
26
26
type providersRouter interface {
27
27
FindProviders (ctx context.Context , cid cid.Cid , limit int ) (iter.ResultIter [types.Record ], error )
28
- Provide (ctx context.Context , req * server. ProvideRequest ) (time.Duration , error )
28
+ Provide (ctx context.Context , req * types. AnnouncementRecord ) (time.Duration , error )
29
29
}
30
30
31
31
type peersRouter interface {
32
32
FindPeers (ctx context.Context , pid peer.ID , limit int ) (iter.ResultIter [* types.PeerRecord ], error )
33
- ProvidePeer (ctx context.Context , req * server. ProvidePeerRequest ) (time.Duration , error )
33
+ ProvidePeer (ctx context.Context , req * types. AnnouncementRecord ) (time.Duration , error )
34
34
}
35
35
36
36
type ipnsRouter interface {
@@ -53,9 +53,9 @@ func (r composableRouter) FindProviders(ctx context.Context, key cid.Cid, limit
53
53
return r .providers .FindProviders (ctx , key , limit )
54
54
}
55
55
56
- func (r composableRouter ) Provide (ctx context.Context , req * server. ProvideRequest ) (time.Duration , error ) {
56
+ func (r composableRouter ) Provide (ctx context.Context , req * types. AnnouncementRecord ) (time.Duration , error ) {
57
57
if r .providers == nil {
58
- return req . TTL , nil
58
+ return 0 , nil
59
59
}
60
60
return r .providers .Provide (ctx , req )
61
61
}
@@ -67,9 +67,9 @@ func (r composableRouter) FindPeers(ctx context.Context, pid peer.ID, limit int)
67
67
return r .peers .FindPeers (ctx , pid , limit )
68
68
}
69
69
70
- func (r composableRouter ) ProvidePeer (ctx context.Context , req * server. ProvidePeerRequest ) (time.Duration , error ) {
70
+ func (r composableRouter ) ProvidePeer (ctx context.Context , req * types. AnnouncementRecord ) (time.Duration , error ) {
71
71
if r .peers == nil {
72
- return req . TTL , nil
72
+ return 0 , nil
73
73
}
74
74
return r .peers .ProvidePeer (ctx , req )
75
75
}
@@ -218,13 +218,13 @@ func (mi *manyIter[T]) Close() error {
218
218
return err
219
219
}
220
220
221
- func (r parallelRouter ) Provide (ctx context.Context , req * server. ProvideRequest ) (time.Duration , error ) {
221
+ func (r parallelRouter ) Provide (ctx context.Context , req * types. AnnouncementRecord ) (time.Duration , error ) {
222
222
return provide (ctx , r .routers , func (ctx context.Context , r router ) (time.Duration , error ) {
223
223
return r .Provide (ctx , req )
224
224
})
225
225
}
226
226
227
- func (r parallelRouter ) ProvidePeer (ctx context.Context , req * server. ProvidePeerRequest ) (time.Duration , error ) {
227
+ func (r parallelRouter ) ProvidePeer (ctx context.Context , req * types. AnnouncementRecord ) (time.Duration , error ) {
228
228
return provide (ctx , r .routers , func (ctx context.Context , r router ) (time.Duration , error ) {
229
229
return r .ProvidePeer (ctx , req )
230
230
})
@@ -374,7 +374,9 @@ func (d libp2pRouter) FindProviders(ctx context.Context, key cid.Cid, limit int)
374
374
}), nil
375
375
}
376
376
377
- func (d libp2pRouter ) Provide (ctx context.Context , req * server.ProvideRequest ) (time.Duration , error ) {
377
+ func (d libp2pRouter ) Provide (ctx context.Context , req * types.AnnouncementRecord ) (time.Duration , error ) {
378
+ // NOTE: this router cannot provide further to the DHT, since we can only
379
+ // announce CIDs that our own node has, which is not the case.
378
380
return 0 , routing .ErrNotSupported
379
381
}
380
382
@@ -399,7 +401,7 @@ func (d libp2pRouter) FindPeers(ctx context.Context, pid peer.ID, limit int) (it
399
401
return iter.ToResultIter [* types.PeerRecord ](iter.FromSlice [* types.PeerRecord ]([]* types.PeerRecord {rec })), nil
400
402
}
401
403
402
- func (r libp2pRouter ) ProvidePeer (ctx context.Context , req * server. ProvidePeerRequest ) (time.Duration , error ) {
404
+ func (r libp2pRouter ) ProvidePeer (ctx context.Context , req * types. AnnouncementRecord ) (time.Duration , error ) {
403
405
return 0 , routing .ErrNotSupported
404
406
}
405
407
@@ -475,14 +477,36 @@ func (d clientRouter) FindProviders(ctx context.Context, cid cid.Cid, limit int)
475
477
return d .Client .FindProviders (ctx , cid )
476
478
}
477
479
478
- func (d clientRouter ) Provide (ctx context.Context , req * server.ProvideRequest ) (time.Duration , error ) {
479
- return 0 , routing .ErrNotSupported
480
+ func (d clientRouter ) Provide (ctx context.Context , req * types.AnnouncementRecord ) (time.Duration , error ) {
481
+ return d .provide (func () (iter.ResultIter [* types.AnnouncementRecord ], error ) {
482
+ return d .Client .ProvideRecords (ctx , req )
483
+ })
480
484
}
481
485
482
486
func (d clientRouter ) FindPeers (ctx context.Context , pid peer.ID , limit int ) (iter.ResultIter [* types.PeerRecord ], error ) {
483
487
return d .Client .FindPeers (ctx , pid )
484
488
}
485
489
486
- func (d clientRouter ) ProvidePeer (ctx context.Context , req * server.ProvidePeerRequest ) (time.Duration , error ) {
487
- return 0 , routing .ErrNotSupported
490
+ func (d clientRouter ) ProvidePeer (ctx context.Context , req * types.AnnouncementRecord ) (time.Duration , error ) {
491
+ return d .provide (func () (iter.ResultIter [* types.AnnouncementRecord ], error ) {
492
+ return d .Client .ProvidePeerRecords (ctx , req )
493
+ })
494
+ }
495
+
496
+ func (d clientRouter ) provide (do func () (iter.ResultIter [* types.AnnouncementRecord ], error )) (time.Duration , error ) {
497
+ resIter , err := do ()
498
+ if err != nil {
499
+ return 0 , err
500
+ }
501
+
502
+ records , err := iter .ReadAllResults (resIter )
503
+ if err != nil {
504
+ return 0 , err
505
+ }
506
+
507
+ if len (records ) != 1 {
508
+ return 0 , errors .New ("invalid number of records returned" )
509
+ }
510
+
511
+ return records [0 ].Payload .TTL , nil
488
512
}
0 commit comments