Skip to content
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

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

master255
Copy link

No description provided.

Copy link
Contributor

@MarcoPolo MarcoPolo left a 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.

@master255
Copy link
Author

I don't know how this can be tested. It's just input data. I don't have any ideas.

@@ -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 {
Copy link
Contributor

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.

Copy link
Author

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?

Copy link
Author

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?

Copy link
Contributor

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Wondertan Why not?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

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?

Copy link
Contributor

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?

Copy link
Author

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.

Copy link
Author

@master255 master255 May 30, 2023

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.

@@ -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)
Copy link
Contributor

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.

Suggested change
NATManager func(network.Network, Option) (NATManager, error)
NATManager func(network.Network, ...Option) (NATManager, error)

Copy link
Author

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?

Copy link
Author

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?

Copy link
Author

@master255 master255 Jun 10, 2023

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.

@@ -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) {
Copy link
Contributor

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?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are the alternatives?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Introducing a new func.

Copy link
Author

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.

Copy link
Contributor

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.

Copy link
Author

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?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

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.

Copy link
Member

@sukunrt sukunrt Sep 27, 2023

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

Copy link
Author

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.

@master255 master255 changed the title Adding UserAgent to Nat for description of port mapping adding UserAgent to Nat for description of port mapping Sep 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants