RoutedHost: Embed the underlying host instead of hiding it#2969
RoutedHost: Embed the underlying host instead of hiding it#2969burdiyan wants to merge 1 commit intolibp2p:masterfrom
Conversation
| type Routing interface { | ||
| FindPeer(context.Context, peer.ID) (peer.AddrInfo, error) | ||
| } | ||
|
|
There was a problem hiding this comment.
Can you deprecate this with a comment saying use routing.PeerRouting
@MarcoPolo Not sure if I agree it would be better. Requiring people to learn and use Fx seems like an overkill for something that can be done with existing language features. I personally stopped using Fx and now prefer doing things explicitly. But of course exposing more Fx-related flexibilities in Libp2p could be helpful too. |
When using a libp2p host with routing, the underlying concrete type is RoutedHost. This type wraps the original Host type and hides it into a private field, which makes it hard to make interface type assertions on the host. E.g. sometimes it's useful to have access to the underlying instance of the IDService, which is exposed by BasicHost as a method `IDService() identityIDService`, but is inaccessible for type assertions because RoutedHost hides the BasicHost in its private field. This commit uses type embedding, instead of a private field to fix it.
|
The problem is that we don't want the Fx solves this problem in a fairly way. It lets you get a reference to a service without having to expose that service in the Host. It also won't create the service if nothing needs it. |
|
Are the Fx affordances for doing that in libp2p exposed to users? I haven't seen how could I do that. Anyway, I'm not feeling strongly about this change, so feel free to close it :) |
|
I agree with @MarcoPolo about not wanting users to rely on type assertions for access to services like Identify. I still think the routed host should embed and wrap the provided host. Why shouldn't it? |
|
You'll be able to get the ID service in the next release. Refer to this test for an example: Lines 50 to 60 in c4c3a34 |
When using a libp2p host with routing, the underlying concrete type is RoutedHost. This type wraps the original Host type and hides it into a private field, which makes it hard to make interface type assertions on the host.
E.g. sometimes it's useful to have access to the underlying instance of the IDService, which is exposed by BasicHost as a method
IDService() identityIDService, but is inaccessible for type assertions because RoutedHost hides the BasicHost in its private field.This commit uses type embedding, instead of a private field, to fix it.