I implemented DialTimeout on a TLS connection and trying to define my source (laddr) address. In previous versions of Go this was possible with the laddr option in the Dial command, now this option is only available in DialTCP but that function does not have a timeout option.
Any ideas how I can make a TLS connection from a defined laddr with a timeout?
type Result struct {
*net.TCPConn
error
}
channel := make(chan *Result, 1)
go func() {
var localAddr, remoteAddr *net.TCPAddr
var err error
var tcpConn *net.TCPConn
if localAddress != "" {
localAddr, err = net.ResolveTCPAddr("tcp", localAddress)
if err != nil {
goto finished
}
}
remoteAddr, err = net.ResolveTCPAddr("tcp", remoteAddress)
if err != nil {
goto finished
}
tcpConn, err = net.DialTCP("tcp", localAddr, remoteAddr)
if err != nil {
goto finished
}
finished:
channel <- &Result{TCPConn: tcpConn, error: err}
}()
select {
case <-timer.C:
return nil, errors.New("connect timeout")
case result := <-channel:
if result.error != nil {
return nil, result.error
}
return result.TCPConn, nil
}
panic("unreachable")
}
On Fri, Sep 7, 2012 at 4:39 PM, Paul van Brouwershaven
> I implemented DialTimeout on a TLS connection and trying to define my source
> (laddr) address. In previous versions of Go this was possible with the laddr
> option in the Dial command, now this option is only available in DialTCP but
> that function does not have a timeout option.
> Any ideas how I can make a TLS connection from a defined laddr with a
> timeout?
Thanks, you code looks good, this would be a simple solution.
I see your code looks a like the original DialTimeout where the following comment is listed:
121 // TODO(bradfitz): the timeout should be pushed down into the 122 // net package's event loop, so on timeout to dead hosts we 123 // don't have a goroutine sticking around for the default of 124 // ~3 minutes.
So this basically means that the goroutine keeps running and the TCP session keeps open. Is there a way to kill the goroutine and linked TCP session?
On Fri, Sep 7, 2012 at 5:08 PM, Paul van Brouwershaven
<p...@vanbrouwershaven.com> wrote:
> I see your code looks a like the original DialTimeout where the following
> comment is listed:
Yeah, I "steal" the idea from Brad's DialTimeout() implementation :p
> 121 // TODO(bradfitz): the timeout should be pushed down into the
> 122 // net package's event loop, so on timeout to dead hosts we
> 123 // don't have a goroutine sticking around for the default of
> 124 // ~3 minutes.
> So this basically means that the goroutine keeps running and the TCP session
> keeps open. Is there a way to kill the goroutine and linked TCP session?
I'm worrying about this too. Maybe Brad will have an idea :)
-- Best regards,
Jingcheng Zhang
Beijing, P.R.China
On Friday, September 7, 2012 12:02:43 PM UTC+2, Jingcheng Zhang wrote:
> On Fri, Sep 7, 2012 at 5:08 PM, Paul van Brouwershaven > <pa...@vanbrouwershaven.com <javascript:>> wrote: > > I see your code looks a like the original DialTimeout where the > following > > comment is listed:
> Yeah, I "steal" the idea from Brad's DialTimeout() implementation :p
> > 121 // TODO(bradfitz): the timeout should be pushed > down into the > > 122 // net package's event loop, so on timeout to dead > hosts we > > 123 // don't have a goroutine sticking around for the > default of > > 124 // ~3 minutes.
> > So this basically means that the goroutine keeps running and the TCP > session > > keeps open. Is there a way to kill the goroutine and linked TCP session?
> I'm worrying about this too. Maybe Brad will have an idea :)