Rebase.
[official-gcc.git] / libgo / go / net / dial_gen.go
blobada6233003fececed91924146c274f5d2661a6ed
1 // Copyright 2012 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.
5 // +build windows plan9
7 package net
9 import (
10 "time"
13 var testingIssue5349 bool // used during tests
15 // dialChannel is the simple pure-Go implementation of dial, still
16 // used on operating systems where the deadline hasn't been pushed
17 // down into the pollserver. (Plan 9 and some old versions of Windows)
18 func dialChannel(net string, ra Addr, dialer func(time.Time) (Conn, error), deadline time.Time) (Conn, error) {
19 var timeout time.Duration
20 if !deadline.IsZero() {
21 timeout = deadline.Sub(time.Now())
23 if timeout <= 0 {
24 return dialer(noDeadline)
26 t := time.NewTimer(timeout)
27 defer t.Stop()
28 type racer struct {
29 Conn
30 error
32 ch := make(chan racer, 1)
33 go func() {
34 if testingIssue5349 {
35 time.Sleep(time.Millisecond)
37 c, err := dialer(noDeadline)
38 ch <- racer{c, err}
39 }()
40 select {
41 case <-t.C:
42 return nil, &OpError{Op: "dial", Net: net, Addr: ra, Err: errTimeout}
43 case racer := <-ch:
44 return racer.Conn, racer.error