Skip to content

Commit 4481362

Browse files
moredureMikhail Faraponov
authored and
Mikhail Faraponov
committed
net: add context-aware Dialer methods DialTCP, DialUDP, DialIP, DialUnix
Fixes #49097
1 parent 6df0957 commit 4481362

File tree

4 files changed

+71
-4
lines changed

4 files changed

+71
-4
lines changed

src/net/iprawsock.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,28 @@ func newIPConn(fd *netFD) *IPConn { return &IPConn{conn{fd}} }
209209
// If the IP field of raddr is nil or an unspecified IP address, the
210210
// local system is assumed.
211211
func DialIP(network string, laddr, raddr *IPAddr) (*IPConn, error) {
212+
return DialIPContext(context.Background(), network, laddr, raddr)
213+
}
214+
215+
// DialIPContext acts like DialIP but connects using
216+
// the provided context.
217+
//
218+
// The provided Context must be non-nil.
219+
//
220+
// The network must be an IP network name; see func Dial for details.
221+
//
222+
// If laddr is nil, a local address is automatically chosen.
223+
// If the IP field of raddr is nil or an unspecified IP address, the
224+
// local system is assumed.
225+
func DialIPContext(ctx context.Context, network string, laddr, raddr *IPAddr) (*IPConn, error) {
226+
if ctx == nil {
227+
panic("nil context")
228+
}
212229
if raddr == nil {
213230
return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: nil, Err: errMissingAddress}
214231
}
215232
sd := &sysDialer{network: network, address: raddr.String()}
216-
c, err := sd.dialIP(context.Background(), laddr, raddr)
233+
c, err := sd.dialIP(ctx, laddr, raddr)
217234
if err != nil {
218235
return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: err}
219236
}

src/net/tcpsock.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,23 @@ func newTCPConn(fd *netFD) *TCPConn {
231231
// If the IP field of raddr is nil or an unspecified IP address, the
232232
// local system is assumed.
233233
func DialTCP(network string, laddr, raddr *TCPAddr) (*TCPConn, error) {
234+
return DialTCPContext(context.Background(), network, laddr, raddr)
235+
}
236+
237+
// DialTCPContext acts like DialTCP but connects using
238+
// the provided context.
239+
//
240+
// The provided Context must be non-nil.
241+
//
242+
// The network must be a TCP network name; see func Dial for details.
243+
//
244+
// If laddr is nil, a local address is automatically chosen.
245+
// If the IP field of raddr is nil or an unspecified IP address, the
246+
// local system is assumed.
247+
func DialTCPContext(ctx context.Context, network string, laddr, raddr *TCPAddr) (*TCPConn, error) {
248+
if ctx == nil {
249+
panic("nil context")
250+
}
234251
switch network {
235252
case "tcp", "tcp4", "tcp6":
236253
default:
@@ -240,7 +257,7 @@ func DialTCP(network string, laddr, raddr *TCPAddr) (*TCPConn, error) {
240257
return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: nil, Err: errMissingAddress}
241258
}
242259
sd := &sysDialer{network: network, address: raddr.String()}
243-
c, err := sd.dialTCP(context.Background(), laddr, raddr)
260+
c, err := sd.dialTCP(ctx, laddr, raddr)
244261
if err != nil {
245262
return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: err}
246263
}

src/net/udpsock.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,23 @@ func newUDPConn(fd *netFD) *UDPConn { return &UDPConn{conn{fd}} }
284284
// If the IP field of raddr is nil or an unspecified IP address, the
285285
// local system is assumed.
286286
func DialUDP(network string, laddr, raddr *UDPAddr) (*UDPConn, error) {
287+
return DialUDPContext(context.Background(), network, laddr, raddr)
288+
}
289+
290+
// DialUDPContext acts like DialUDP but connects using
291+
// the provided context.
292+
//
293+
// The provided Context must be non-nil.
294+
//
295+
// The network must be a UDP network name; see func Dial for details.
296+
//
297+
// If laddr is nil, a local address is automatically chosen.
298+
// If the IP field of raddr is nil or an unspecified IP address, the
299+
// local system is assumed.
300+
func DialUDPContext(ctx context.Context, network string, laddr, raddr *UDPAddr) (*UDPConn, error) {
301+
if ctx == nil {
302+
panic("nil context")
303+
}
287304
switch network {
288305
case "udp", "udp4", "udp6":
289306
default:
@@ -293,7 +310,7 @@ func DialUDP(network string, laddr, raddr *UDPAddr) (*UDPConn, error) {
293310
return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: nil, Err: errMissingAddress}
294311
}
295312
sd := &sysDialer{network: network, address: raddr.String()}
296-
c, err := sd.dialUDP(context.Background(), laddr, raddr)
313+
c, err := sd.dialUDP(ctx, laddr, raddr)
297314
if err != nil {
298315
return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: err}
299316
}

src/net/unixsock.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,29 @@ func newUnixConn(fd *netFD) *UnixConn { return &UnixConn{conn{fd}} }
201201
// If laddr is non-nil, it is used as the local address for the
202202
// connection.
203203
func DialUnix(network string, laddr, raddr *UnixAddr) (*UnixConn, error) {
204+
return DialUnixContext(context.Background(), network, laddr, raddr)
205+
}
206+
207+
// DialUnixContext acts like DialUnix but connects using
208+
// the provided context.
209+
//
210+
// The provided Context must be non-nil.
211+
//
212+
// The network must be a Unix network name; see func Dial for details.
213+
//
214+
// If laddr is non-nil, it is used as the local address for the
215+
// connection.
216+
func DialUnixContext(ctx context.Context, network string, laddr, raddr *UnixAddr) (*UnixConn, error) {
217+
if ctx == nil {
218+
panic("nil context")
219+
}
204220
switch network {
205221
case "unix", "unixgram", "unixpacket":
206222
default:
207223
return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: UnknownNetworkError(network)}
208224
}
209225
sd := &sysDialer{network: network, address: raddr.String()}
210-
c, err := sd.dialUnix(context.Background(), laddr, raddr)
226+
c, err := sd.dialUnix(ctx, laddr, raddr)
211227
if err != nil {
212228
return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: err}
213229
}

0 commit comments

Comments
 (0)