Skip to content

Commit 7cf6f4b

Browse files
moredureMikhail Faraponov
authored and
Mikhail Faraponov
committed
net: add DialUDPContext version of DialUDP
Fixes #49097
1 parent 6df0957 commit 7cf6f4b

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

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
}

0 commit comments

Comments
 (0)