dial_gen.go 1.1 KB

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