mirror of
https://github.com/netbirdio/gvisor.git
synced 2026-05-22 17:12:49 -07:00
Add gonet.DialContextTCP.
Allows cancellation and timeouts. PiperOrigin-RevId: 247090428 Change-Id: I91907f12e218677dcd0e0b6d72819deedbd9f20c
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
package gonet
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
@@ -495,6 +496,12 @@ func fullToUDPAddr(addr tcpip.FullAddress) *net.UDPAddr {
|
||||
|
||||
// DialTCP creates a new TCP Conn connected to the specified address.
|
||||
func DialTCP(s *stack.Stack, addr tcpip.FullAddress, network tcpip.NetworkProtocolNumber) (*Conn, error) {
|
||||
return DialContextTCP(context.Background(), s, addr, network)
|
||||
}
|
||||
|
||||
// DialContextTCP creates a new TCP Conn connected to the specified address
|
||||
// with the option of adding cancellation and timeouts.
|
||||
func DialContextTCP(ctx context.Context, s *stack.Stack, addr tcpip.FullAddress, network tcpip.NetworkProtocolNumber) (*Conn, error) {
|
||||
// Create TCP endpoint, then connect.
|
||||
var wq waiter.Queue
|
||||
ep, err := s.NewEndpoint(tcp.ProtocolNumber, network, &wq)
|
||||
@@ -509,9 +516,21 @@ func DialTCP(s *stack.Stack, addr tcpip.FullAddress, network tcpip.NetworkProtoc
|
||||
wq.EventRegister(&waitEntry, waiter.EventOut)
|
||||
defer wq.EventUnregister(&waitEntry)
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil, ctx.Err()
|
||||
default:
|
||||
}
|
||||
|
||||
err = ep.Connect(addr)
|
||||
if err == tcpip.ErrConnectStarted {
|
||||
<-notifyCh
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
ep.Close()
|
||||
return nil, ctx.Err()
|
||||
case <-notifyCh:
|
||||
}
|
||||
|
||||
err = ep.GetSockOpt(tcpip.ErrorOption{})
|
||||
}
|
||||
if err != nil {
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
package gonet
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
@@ -595,6 +596,48 @@ func TestTCPDialError(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDialContextTCPCanceled(t *testing.T) {
|
||||
s, err := newLoopbackStack()
|
||||
if err != nil {
|
||||
t.Fatalf("newLoopbackStack() = %v", err)
|
||||
}
|
||||
|
||||
addr := tcpip.FullAddress{NICID, tcpip.Address(net.IPv4(169, 254, 10, 1).To4()), 11211}
|
||||
s.AddAddress(NICID, ipv4.ProtocolNumber, addr.Addr)
|
||||
|
||||
ctx := context.Background()
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
cancel()
|
||||
|
||||
if _, err := DialContextTCP(ctx, s, addr, ipv4.ProtocolNumber); err != context.Canceled {
|
||||
t.Errorf("got DialContextTCP(...) = %v, want = %v", err, context.Canceled)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDialContextTCPTimeout(t *testing.T) {
|
||||
s, err := newLoopbackStack()
|
||||
if err != nil {
|
||||
t.Fatalf("newLoopbackStack() = %v", err)
|
||||
}
|
||||
|
||||
addr := tcpip.FullAddress{NICID, tcpip.Address(net.IPv4(169, 254, 10, 1).To4()), 11211}
|
||||
s.AddAddress(NICID, ipv4.ProtocolNumber, addr.Addr)
|
||||
|
||||
fwd := tcp.NewForwarder(s, 30000, 10, func(r *tcp.ForwarderRequest) {
|
||||
time.Sleep(time.Second)
|
||||
r.Complete(true)
|
||||
})
|
||||
s.SetTransportProtocolHandler(tcp.ProtocolNumber, fwd.HandlePacket)
|
||||
|
||||
ctx := context.Background()
|
||||
ctx, cancel := context.WithDeadline(ctx, time.Now().Add(100*time.Millisecond))
|
||||
defer cancel()
|
||||
|
||||
if _, err := DialContextTCP(ctx, s, addr, ipv4.ProtocolNumber); err != context.DeadlineExceeded {
|
||||
t.Errorf("got DialContextTCP(...) = %v, want = %v", err, context.DeadlineExceeded)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNetTest(t *testing.T) {
|
||||
nettest.TestConn(t, makePipe)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user