github.com/mdempsky/go@v0.0.0-20151201204031-5dd372bd1e70/src/net/dial_gen.go (about)

     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.
     4  
     5  // +build windows plan9
     6  
     7  package net
     8  
     9  import "time"
    10  
    11  // dialChannel is the simple pure-Go implementation of dial, still
    12  // used on operating systems where the deadline hasn't been pushed
    13  // down into the pollserver. (Plan 9 and some old versions of Windows)
    14  func dialChannel(net string, ra Addr, dialer func(time.Time) (Conn, error), deadline time.Time) (Conn, error) {
    15  	if deadline.IsZero() {
    16  		return dialer(noDeadline)
    17  	}
    18  	timeout := deadline.Sub(time.Now())
    19  	if timeout <= 0 {
    20  		return nil, &OpError{Op: "dial", Net: net, Source: nil, Addr: ra, Err: errTimeout}
    21  	}
    22  	t := time.NewTimer(timeout)
    23  	defer t.Stop()
    24  	type racer struct {
    25  		Conn
    26  		error
    27  	}
    28  	ch := make(chan racer, 1)
    29  	go func() {
    30  		testHookDialChannel()
    31  		c, err := dialer(noDeadline)
    32  		ch <- racer{c, err}
    33  	}()
    34  	select {
    35  	case <-t.C:
    36  		return nil, &OpError{Op: "dial", Net: net, Source: nil, Addr: ra, Err: errTimeout}
    37  	case racer := <-ch:
    38  		return racer.Conn, racer.error
    39  	}
    40  }