• Brad Fitzpatrick's avatar
    net: add Dialer.Cancel to cancel pending dials · 24a83d35
    Brad Fitzpatrick authored
    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/17821Reviewed-by: 's avatarRuss Cox <rsc@golang.org>
    Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
    TryBot-Result: Gobot Gobot <gobot@golang.org>
    24a83d35
tcpsock_posix.go 10.7 KB