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

Closed
kevinburke1 opened this issue Apr 17, 2020 · 2 comments
Closed

proposal: add net.DialUnixContext #38506

kevinburke1 opened this issue Apr 17, 2020 · 2 comments

Comments

@kevinburke1
Copy link

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

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

@gopherbot
Copy link

Change https://golang.org/cl/263417 mentions this issue: net: add Example for Unix dialer with context

@golang golang locked and limited conversation to collaborators Oct 21, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants