github.com/roboticscm/goman@v0.0.0-20210203095141-87c07b4a0a55/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 (
    10  	"time"
    11  )
    12  
    13  var testingIssue5349 bool // used during tests
    14  
    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())
    22  	}
    23  	if timeout <= 0 {
    24  		return dialer(noDeadline)
    25  	}
    26  	t := time.NewTimer(timeout)
    27  	defer t.Stop()
    28  	type racer struct {
    29  		Conn
    30  		error
    31  	}
    32  	ch := make(chan racer, 1)
    33  	go func() {
    34  		if testingIssue5349 {
    35  			time.Sleep(time.Millisecond)
    36  		}
    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
    45  	}
    46  }