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
adding UserAgent to Nat for description of port mapping #2310
base: master
Are you sure you want to change the base?
Conversation
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.
Nice, thank you. Could we include some tests so that we don't break this in the future.
I don't know how this can be tested. It's just input data. I don't have any ideas. |
p2p/host/basic/natmgr.go
Outdated
@@ -25,8 +25,8 @@ type NATManager interface { | |||
} | |||
|
|||
// NewNATManager creates a NAT manager. | |||
func NewNATManager(net network.Network) NATManager { | |||
return newNATManager(net) | |||
func NewNATManager(net network.Network, userAgent string) NATManager { |
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.
Nicer: use the option pattern.
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.
@marten-seemann What does that mean? In what place? Where is it better to take the variable from?
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.
@marten-seemann Ok. Maybe this is the right way?
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.
Marten likely meant defining a new options type for the NATManager
instead of extending Swarm's options set and Network interface with UserAgant getter.
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.
@Wondertan Why not?
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.
I was talking about the option pattern, like here: https://github.com/libp2p/go-libp2p/blob/master/p2p/protocol/circuitv2/relay/options.go
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.
@marten-seemann And you think putting the UserAgent in the Swarm - Network is not right?
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.
Yeah, it doesn't really belong in the swarm. What made you put it there?
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.
@marten-seemann I don't know. I just read the comments about what the swarm was doing and looked at which direction the data was going. If the Network is establishing connections, it needs a UserAgent.
Okay, if this idea of mine is wrong, I'll fix the code. I'll try to make an options.
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.
@marten-seemann The new version with options is ready. But it seems to me it is too much for one parameter.
I like the second version. Through Network.
p2p/host/basic/basic_host.go
Outdated
@@ -131,7 +131,7 @@ type HostOpts struct { | |||
|
|||
// NATManager takes care of setting NAT port mappings, and discovering external addresses. | |||
// If omitted, this will simply be disabled. | |||
NATManager func(network.Network) NATManager | |||
NATManager func(network.Network, Option) (NATManager, 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.
The beauty of the option pattern is that the options are optional. You need to make them variadic though.
NATManager func(network.Network, Option) (NATManager, error) | |
NATManager func(network.Network, ...Option) (NATManager, 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.
@marten-seemann Ready. But I still think it's redundant to add an Option for a single parameter. Perhaps there will be more parameters in the future?
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.
@marten-seemann Maybe something else? Or is it done?
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.
Fixed the error. Now it works.
p2p/net/nat/nat.go
Outdated
@@ -34,7 +34,7 @@ type entry struct { | |||
var discoverGateway = nat.DiscoverGateway | |||
|
|||
// DiscoverNAT looks for a NAT device in the network and returns an object that can manage port mappings. | |||
func DiscoverNAT(ctx context.Context) (*NAT, error) { | |||
func DiscoverNAT(ctx context.Context, userAgent string) (*NAT, 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.
Do we want to change the interface of this public method?
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.
What are the alternatives?
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.
Introducing a new func.
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.
If we create a new function, we need to designate a default userAgent somewhere in this file. The default value is set in another file.
So this constructor has no sense without the userAgent string.
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.
The default should be in this file. That was the old behavior. If users want to change it they can call the new function (maybe call it DiscoverNATWithUserAgent
).
This change as is is a breaking API change. I like to avoid them unless necessary. I don't think it's necessary here.
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.
@MarcoPolo Okay. Then what to do with this one?
https://github.com/libp2p/go-libp2p/pull/2310/files#diff-a5aa28aae96d02e4316747f7950909f36d447f7833e5a70121c3808852e5e440R80
What to pass in this line? What will be the default value here?
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.
And then what will be in this place? "if" and two constructors?
https://github.com/libp2p/go-libp2p/pull/2310/files#diff-a5aa28aae96d02e4316747f7950909f36d447f7833e5a70121c3808852e5e440R117
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.
@MarcoPolo Okay. Why shouldn't there be an incoming userAgent parameter in this function? After all, we cannot communicate with NAT without userAgent.
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.
Why shouldn't there be an incoming userAgent parameter in this function?
Because it's a breaking API change. Users of this function, when they update go-libp2p will need to update their usage of this function and pass in a userAgent.
It'd be nicer if we add another function DiscoverNATWithUserAgent
and NatManager
switches between DiscoverNAT
and DiscoverNATWithUserAgent
based on whether it has a user configured UserAgent or not
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.
@sukunrt Okay. I fixed it.
No description provided.