-
Notifications
You must be signed in to change notification settings - Fork 138
routing/http: add support for GetClosestPeers (IPIP-476) #1021
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 12 commits
220a690
f7788a7
5d4e99e
d52bcb1
0264929
39432b9
384effb
d53886a
0a8bc14
ce702e0
bdb5505
cd7be90
07e74fb
5162f7c
713b7c0
ce6837e
3256401
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,12 +23,16 @@ var logger = logging.Logger("routing/http/contentrouter") | |
|
|
||
| const ttl = 24 * time.Hour | ||
|
|
||
| // A Client provides HTTP Delegated Routing methods. See also [server.DelegatedRouter]. | ||
| type Client interface { | ||
| FindProviders(ctx context.Context, key cid.Cid) (iter.ResultIter[types.Record], error) | ||
| ProvideBitswap(ctx context.Context, keys []cid.Cid, ttl time.Duration) (time.Duration, error) | ||
| FindPeers(ctx context.Context, pid peer.ID) (peers iter.ResultIter[*types.PeerRecord], err error) | ||
| GetIPNS(ctx context.Context, name ipns.Name) (*ipns.Record, error) | ||
| PutIPNS(ctx context.Context, name ipns.Name, record *ipns.Record) error | ||
| // GetClosestPeers returns the DHT closest peers to the given peer ID. | ||
| // If empty, it will use the content router's peer ID (self). | ||
| GetClosestPeers(ctx context.Context, peerID peer.ID) (iter.ResultIter[*types.PeerRecord], error) | ||
| } | ||
|
|
||
| type contentRouter struct { | ||
|
|
@@ -43,6 +47,7 @@ var ( | |
| _ routing.ValueStore = (*contentRouter)(nil) | ||
| _ routinghelpers.ProvideManyRouter = (*contentRouter)(nil) | ||
| _ routinghelpers.ReadyAbleRouter = (*contentRouter)(nil) | ||
| _ routinghelpers.DHTRouter = (*contentRouter)(nil) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems to be the only use case of Since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It breaks consistency? do we need a ReadyAbleRouter etc? IUUC the contentRouter is not a Client, but a Router implementation, the problem being that Router doesn't have a unified interface, which is ugly, but placing this new interface in a different place than the others is also weird. |
||
| ) | ||
|
|
||
| type option func(c *contentRouter) | ||
|
|
@@ -59,6 +64,8 @@ func WithMaxProvideBatchSize(max int) option { | |
| } | ||
| } | ||
|
|
||
| // NewContentRoutingClient returns a client that conforms to the | ||
| // ContentRouting interfaces. | ||
| func NewContentRoutingClient(c Client, opts ...option) *contentRouter { | ||
| cr := &contentRouter{ | ||
| client: c, | ||
|
|
@@ -300,3 +307,44 @@ func (c *contentRouter) SearchValue(ctx context.Context, key string, opts ...rou | |
|
|
||
| return ch, nil | ||
| } | ||
|
|
||
| func (c *contentRouter) GetClosestPeers(ctx context.Context, pid peer.ID) (<-chan peer.AddrInfo, error) { | ||
| iter, err := c.client.GetClosestPeers(ctx, pid) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| infos := make(chan peer.AddrInfo) | ||
| go func() { | ||
| defer iter.Close() | ||
| defer close(infos) | ||
| for iter.Next() { | ||
| res := iter.Val() | ||
| if res.Err != nil { | ||
| logger.Warnf("error iterating peer responses: %s", res.Err) | ||
| continue | ||
| } | ||
|
|
||
| var addrs []multiaddr.Multiaddr | ||
| for _, a := range res.Val.Addrs { | ||
| addrs = append(addrs, a.Multiaddr) | ||
| } | ||
|
|
||
| // If there are no addresses there's nothing of value to return | ||
| if len(addrs) == 0 { | ||
| continue | ||
| } | ||
|
|
||
| select { | ||
| case <-ctx.Done(): | ||
| logger.Warnf("aborting GetClosestPeers: %s", ctx.Err()) | ||
| return | ||
| case infos <- peer.AddrInfo{ | ||
| ID: *res.Val.ID, | ||
| Addrs: addrs, | ||
| }: | ||
| } | ||
| } | ||
| }() | ||
|
|
||
| return infos, nil | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.