net: add Dialer.Cancel to cancel pending dials
Dialer.Cancel is a new optional <-chan struct{} channel whose closure
indicates that the dial should be canceled. It is compatible with the
x/net/context and http.Request.Cancel types.
Tested by hand with:
package main
import (
"log"
"net"
"time"
)
func main() {
log.Printf("start.")
var d net.Dialer
cancel := make(chan struct{})
time.AfterFunc(2*time.Second, func() {
log.Printf("timeout firing")
close(cancel)
})
d.Cancel = cancel
c, err := d.Dial("tcp", "192.168.0.1:22")
if err != nil {
log.Print(err)
return
}
log.Fatalf("unexpected connect: %v", c)
}
Which says:
2015/12/14 22:24:58 start.
2015/12/14 22:25:00 timeout firing
2015/12/14 22:25:00 dial tcp 192.168.0.1:22: operation was canceled
Fixes #11225
Change-Id: I2ef39e3a540e29fe6bfec03ab7a629a6b187fcb3
Reviewed-on: https://go-review.googlesource.com/17821
Reviewed-by: Russ Cox <rsc@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
diff --git a/src/net/net.go b/src/net/net.go
index 89212e6..d9d23fa 100644
--- a/src/net/net.go
+++ b/src/net/net.go
@@ -426,7 +426,16 @@
return s
}
-var noDeadline = time.Time{}
+var (
+ // aLongTimeAgo is a non-zero time, far in the past, used for
+ // immediate cancelation of dials.
+ aLongTimeAgo = time.Unix(233431200, 0)
+
+ // nonDeadline and noCancel are just zero values for
+ // readability with functions taking too many parameters.
+ noDeadline = time.Time{}
+ noCancel = (chan struct{})(nil)
+)
type timeout interface {
Timeout() bool