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

proposal: add net.DialUnixContext #38506

Open
kevinburkemeter opened this issue Apr 17, 2020 · 1 comment
Open

proposal: add net.DialUnixContext #38506

kevinburkemeter opened this issue Apr 17, 2020 · 1 comment
Labels
Milestone

Comments

@kevinburkemeter
Copy link

@kevinburkemeter kevinburkemeter commented Apr 17, 2020

If I write net.Dial, eventually net.Dialer.DialContext will be called. The latter invocation allows users to pass a context.Context to the Dial function, and cancel the Dial at the time the context is canceled.

If I call net.DialUnix, under the hood a context.Context is immediately invoked:

func DialUnix(network string, laddr, raddr *UnixAddr) (*UnixConn, error) {
	switch network {
	case "unix", "unixgram", "unixpacket":
	default:
		return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: UnknownNetworkError(network)}
	}
	sd := &sysDialer{network: network, address: raddr.String()}
	c, err := sd.dialUnix(context.Background(), laddr, raddr)
	if err != nil {
		return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: err}
	}
	return c, nil
}

However, as currently written you can't plumb through your own context.

It would be nice if a DialUnixContext(context.Context, network string, laddr, raddr *UnixAddr) function was added to the API that indicated to end users that a blocking I/O operation was about to proceed, and allowed callers to cancel the request on demand.

Alternatively, a *UnixDialer could be added, with a single DialContext method. This would allow the possibility of later configuring the behavior of the dialer.

Alternatively, there is another call to dialUnix buried deep in the guts of the *sysDialer code, which gets called from Dial... perhaps if there were clearer instructions on how to use net.Dial to achieve the same behavior as the DialUnix function, we could accomplish it without a separate API.

@gopherbot gopherbot added this to the Proposal milestone Apr 17, 2020
@gopherbot gopherbot added the Proposal label Apr 17, 2020
@acln0
Copy link
Contributor

@acln0 acln0 commented Apr 23, 2020

I believe that if you currently have this code:

laddr := &net.UnixAddr{...}
raddr := &net.UnixAddr{...}
uconn, err := net.DialUnix("unix", laddr, raddr)
if err != nil {
	// ...
}
// use uconn

You can replace it with the equivalent context-aware invocation:

laddr := &net.UnixAddr{...}
raddr := &net.UnixAddr{...}
d := &net.Dialer{LocalAddr: laddr}
conn, err := d.DialContext(ctx, "unix", raddr.String())
if err != nil {
	// ...
}
uconn := conn.(*net.UnixConn)
// use uconn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.