Retro68/gcc/libgo/go/net/dial_gen.go

41 lines
1.1 KiB
Go
Raw Normal View History

2014-09-21 17:33:12 +00:00
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build windows plan9
package net
2017-04-10 11:32:00 +00:00
import "time"
2014-09-21 17:33:12 +00:00
// dialChannel is the simple pure-Go implementation of dial, still
// used on operating systems where the deadline hasn't been pushed
// down into the pollserver. (Plan 9 and some old versions of Windows)
func dialChannel(net string, ra Addr, dialer func(time.Time) (Conn, error), deadline time.Time) (Conn, error) {
2017-04-10 11:32:00 +00:00
if deadline.IsZero() {
return dialer(noDeadline)
2014-09-21 17:33:12 +00:00
}
2017-04-10 11:32:00 +00:00
timeout := deadline.Sub(time.Now())
2014-09-21 17:33:12 +00:00
if timeout <= 0 {
2017-04-10 11:32:00 +00:00
return nil, &OpError{Op: "dial", Net: net, Source: nil, Addr: ra, Err: errTimeout}
2014-09-21 17:33:12 +00:00
}
t := time.NewTimer(timeout)
defer t.Stop()
type racer struct {
Conn
error
}
ch := make(chan racer, 1)
go func() {
2017-04-10 11:32:00 +00:00
testHookDialChannel()
2014-09-21 17:33:12 +00:00
c, err := dialer(noDeadline)
ch <- racer{c, err}
}()
select {
case <-t.C:
2017-04-10 11:32:00 +00:00
return nil, &OpError{Op: "dial", Net: net, Source: nil, Addr: ra, Err: errTimeout}
2014-09-21 17:33:12 +00:00
case racer := <-ch:
return racer.Conn, racer.error
}
}