Simplify (*Client).Dial handling of network type (#737)

* Simplify (*Client).Dial handling of network type

* Remove net.Dialer cast from (*Client).Dial
This commit is contained in:
Tom Thorogood 2018-09-09 01:57:21 +09:30 committed by Miek Gieben
parent bf6da3a5bd
commit 7da8f0db5c
1 changed files with 9 additions and 18 deletions

View File

@ -91,30 +91,20 @@ func (c *Client) Dial(address string) (conn *Conn, err error) {
if c.Dialer == nil {
d = net.Dialer{Timeout: c.getTimeoutForRequest(c.dialTimeout())}
} else {
d = net.Dialer(*c.Dialer)
d = *c.Dialer
}
network := "udp"
useTLS := false
switch c.Net {
case "tcp-tls":
network = "tcp"
useTLS = true
case "tcp4-tls":
network = "tcp4"
useTLS = true
case "tcp6-tls":
network = "tcp6"
useTLS = true
default:
if c.Net != "" {
network = c.Net
}
network := c.Net
if network == "" {
network = "udp"
}
useTLS := strings.HasPrefix(network, "tcp") && strings.HasSuffix(network, "-tls")
conn = new(Conn)
if useTLS {
network = strings.TrimSuffix(network, "-tls")
conn.Conn, err = tls.DialWithDialer(&d, network, address, c.TLSConfig)
} else {
conn.Conn, err = d.Dial(network, address)
@ -122,6 +112,7 @@ func (c *Client) Dial(address string) (conn *Conn, err error) {
if err != nil {
return nil, err
}
return conn, nil
}