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

Extract helper for connecting to Unix sockets #2922

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tamird
Copy link
Contributor

@tamird tamird commented Nov 19, 2024

See commit message.

@tamird
Copy link
Contributor Author

tamird commented Nov 19, 2024

r? @nirs

pkg/networks/usernet/client.go Outdated Show resolved Hide resolved
pkg/vz/network_darwin.go Show resolved Hide resolved
pkg/vz/network_darwin.go Outdated Show resolved Hide resolved
@tamird tamird force-pushed the avoid-type-assertion branch 2 times, most recently from c1c8174 to 4bea537 Compare November 19, 2024 19:00
@AkihiroSuda AkihiroSuda added this to the v1.0.2 milestone Nov 20, 2024
@@ -27,7 +28,7 @@ func PassFDToUnix(unixSock string) (*os.File, error) {
if err != nil {
return nil, err
}
err = fd.Put(unixConn.(*net.UnixConn), server)
Copy link
Member

@AkihiroSuda AkihiroSuda Nov 20, 2024

Choose a reason for hiding this comment

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

I don't think we have to introduce a new netutil package just for avoiding this single type assertion.

If you want to avoid a potential panic, you can just check conn, ok := unixConn.(*net.UnixConn); if !ok { return errors.New(...)}

Copy link
Member

@AkihiroSuda AkihiroSuda left a comment

Choose a reason for hiding this comment

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

I don't think we have to introduce a new netutil package just for avoiding this single type assertion.

If you want to avoid a potential panic, you can just check conn, ok := unixConn.(*net.UnixConn); if !ok { return errors.New(...)}

@AkihiroSuda AkihiroSuda removed this from the v1.0.2 milestone Nov 20, 2024
@tamird
Copy link
Contributor Author

tamird commented Nov 20, 2024

I don't think we have to introduce a new netutil package just for avoiding this single type assertion.

Because this pattern occurs in a number of places (connecting to a UNIX socket, not the type assertion), this is reducing all that duplication.

If you want to avoid a potential panic, you can just check conn, ok := unixConn.(*net.UnixConn); if !ok { return errors.New(...)}

I don't think a panic is a real concern. This is just cleanup.

@tamird
Copy link
Contributor Author

tamird commented Nov 21, 2024

@nirs this is what you wanted, no?

@nirs
Copy link
Member

nirs commented Nov 21, 2024

@nirs this is what you wanted, no?

I suggested to add a helper which would solve the issue for the vz package. The current change is little bit bigger and handle all unix sockets in lima (good), but it is hard to sell it as a way to avoid type assertions since we have only one.

I did have time to review all the changes, but I think this will be easier to swallow if we change the commit/pr title/message to describe why creating unix socket in this way is better.

@tamird tamird changed the title Avoid net.UnixConn type assertions Extract helper for connecting to Unix sockets Nov 21, 2024
Extract netutil to house a new `DialUnix` that hides the messier
interface presented by the standard library. Use this new helper to
avoid a type assertion.

Signed-off-by: Tamir Duberstein <tamird@gmail.com>
@tamird
Copy link
Contributor Author

tamird commented Nov 21, 2024

Updated both the PR title and the commit message.

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.

3 participants