-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
RoutedHost: Embed the underlying host instead of hiding it #2969
Conversation
p2p/host/routed/routed.go
Outdated
type Routing interface { | ||
FindPeer(context.Context, peer.ID) (peer.AddrInfo, error) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you deprecate this with a comment saying use routing.PeerRouting
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
@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.