Skip to content

Commit 964309e

Browse files
committed
net: DialTimeout
Fixes #240 R=adg, dsymonds, rsc, r, mikioh.mikioh CC=golang-dev https://golang.org/cl/5491062
1 parent 4869996 commit 964309e

File tree

2 files changed

+153
-1
lines changed

2 files changed

+153
-1
lines changed

src/pkg/net/dial.go

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
package net
66

7+
import (
8+
"time"
9+
)
10+
711
func resolveNetAddr(op, net, addr string) (a Addr, err error) {
812
if addr == "" {
913
return nil, &OpError{op, net, nil, errMissingAddress}
@@ -42,11 +46,15 @@ func resolveNetAddr(op, net, addr string) (a Addr, err error) {
4246
// Dial("tcp", "google.com:80")
4347
// Dial("tcp", "[de:ad:be:ef::ca:fe]:80")
4448
//
45-
func Dial(net, addr string) (c Conn, err error) {
49+
func Dial(net, addr string) (Conn, error) {
4650
addri, err := resolveNetAddr("dial", net, addr)
4751
if err != nil {
4852
return nil, err
4953
}
54+
return dialAddr(net, addr, addri)
55+
}
56+
57+
func dialAddr(net, addr string, addri Addr) (c Conn, err error) {
5058
switch ra := addri.(type) {
5159
case *TCPAddr:
5260
c, err = DialTCP(net, nil, ra)
@@ -65,6 +73,62 @@ func Dial(net, addr string) (c Conn, err error) {
6573
return
6674
}
6775

76+
// DialTimeout acts like Dial but takes a timeout.
77+
// The timeout includes name resolution, if required.
78+
func DialTimeout(net, addr string, timeout time.Duration) (Conn, error) {
79+
// TODO(bradfitz): the timeout should be pushed down into the
80+
// net package's event loop, so on timeout to dead hosts we
81+
// don't have a goroutine sticking around for the default of
82+
// ~3 minutes.
83+
t := time.NewTimer(timeout)
84+
defer t.Stop()
85+
type pair struct {
86+
Conn
87+
error
88+
}
89+
ch := make(chan pair, 1)
90+
resolvedAddr := make(chan Addr, 1)
91+
go func() {
92+
addri, err := resolveNetAddr("dial", net, addr)
93+
if err != nil {
94+
ch <- pair{nil, err}
95+
return
96+
}
97+
resolvedAddr <- addri // in case we need it for OpError
98+
c, err := dialAddr(net, addr, addri)
99+
ch <- pair{c, err}
100+
}()
101+
select {
102+
case <-t.C:
103+
// Try to use the real Addr in our OpError, if we resolved it
104+
// before the timeout. Otherwise we just use stringAddr.
105+
var addri Addr
106+
select {
107+
case a := <-resolvedAddr:
108+
addri = a
109+
default:
110+
addri = &stringAddr{net, addr}
111+
}
112+
err := &OpError{
113+
Op: "dial",
114+
Net: net,
115+
Addr: addri,
116+
Err: &timeoutError{},
117+
}
118+
return nil, err
119+
case p := <-ch:
120+
return p.Conn, p.error
121+
}
122+
panic("unreachable")
123+
}
124+
125+
type stringAddr struct {
126+
net, addr string
127+
}
128+
129+
func (a stringAddr) Network() string { return a.net }
130+
func (a stringAddr) String() string { return a.addr }
131+
68132
// Listen announces on the local network address laddr.
69133
// The network string net must be a stream-oriented
70134
// network: "tcp", "tcp4", "tcp6", or "unix", or "unixpacket".

src/pkg/net/dial_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright 2011 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package net
6+
7+
import (
8+
"runtime"
9+
"testing"
10+
"time"
11+
)
12+
13+
func newLocalListener(t *testing.T) Listener {
14+
ln, err := Listen("tcp", "127.0.0.1:0")
15+
if err != nil {
16+
ln, err = Listen("tcp6", "[::1]:0")
17+
}
18+
if err != nil {
19+
t.Fatal(err)
20+
}
21+
return ln
22+
}
23+
24+
func TestDialTimeout(t *testing.T) {
25+
ln := newLocalListener(t)
26+
defer ln.Close()
27+
28+
errc := make(chan error)
29+
30+
const SOMAXCONN = 0x80 // copied from syscall, but not always available
31+
const numConns = SOMAXCONN + 10
32+
33+
// TODO(bradfitz): It's hard to test this in a portable
34+
// way. This is unforunate, but works for now.
35+
switch runtime.GOOS {
36+
case "linux":
37+
// The kernel will start accepting TCP connections before userspace
38+
// gets a chance to not accept them, so fire off a bunch to fill up
39+
// the kernel's backlog. Then we test we get a failure after that.
40+
for i := 0; i < numConns; i++ {
41+
go func() {
42+
_, err := DialTimeout("tcp", ln.Addr().String(), 200*time.Millisecond)
43+
errc <- err
44+
}()
45+
}
46+
case "darwin":
47+
// At least OS X 10.7 seems to accept any number of
48+
// connections, ignoring listen's backlog, so resort
49+
// to connecting to a hopefully-dead 127/8 address.
50+
go func() {
51+
_, err := DialTimeout("tcp", "127.0.71.111:80", 200*time.Millisecond)
52+
errc <- err
53+
}()
54+
default:
55+
// TODO(bradfitz): this probably doesn't work on
56+
// Windows? SOMAXCONN is huge there. I'm not sure how
57+
// listen works there.
58+
// OpenBSD may have a reject route to 10/8.
59+
// FreeBSD likely works, but is untested.
60+
t.Logf("skipping test on %q; untested.", runtime.GOOS)
61+
return
62+
}
63+
64+
connected := 0
65+
for {
66+
select {
67+
case <-time.After(15 * time.Second):
68+
t.Fatal("too slow")
69+
case err := <-errc:
70+
if err == nil {
71+
connected++
72+
if connected == numConns {
73+
t.Fatal("all connections connected; expected some to time out")
74+
}
75+
} else {
76+
terr, ok := err.(timeout)
77+
if !ok {
78+
t.Fatalf("got error %q; want error with timeout interface", err)
79+
}
80+
if !terr.Timeout() {
81+
t.Fatalf("got error %q; not a timeout", err)
82+
}
83+
// Pass. We saw a timeout error.
84+
return
85+
}
86+
}
87+
}
88+
}

0 commit comments

Comments
 (0)