Improve client default timeout code

- Remove duplicate code.
- Don't update deprecated functions.
This commit is contained in:
Gary Burd 2018-03-06 09:21:16 -08:00
parent 8c40c0b5bd
commit 4835f71f2a
1 changed files with 4 additions and 5 deletions

View File

@ -41,7 +41,6 @@ func NewClient(netConn net.Conn, u *url.URL, requestHeader http.Header, readBufS
NetDial: func(net, addr string) (net.Conn, error) { NetDial: func(net, addr string) (net.Conn, error) {
return netConn, nil return netConn, nil
}, },
HandshakeTimeout: 45 * time.Second,
} }
return d.Dial(u.String(), requestHeader) return d.Dial(u.String(), requestHeader)
} }
@ -111,6 +110,9 @@ var DefaultDialer = &Dialer{
HandshakeTimeout: 45 * time.Second, HandshakeTimeout: 45 * time.Second,
} }
// nilDialer is dialer to use when receiver is nil.
var nilDialer Dialer = *DefaultDialer
// Dial creates a new client connection. Use requestHeader to specify the // Dial creates a new client connection. Use requestHeader to specify the
// origin (Origin), subprotocols (Sec-WebSocket-Protocol) and cookies (Cookie). // origin (Origin), subprotocols (Sec-WebSocket-Protocol) and cookies (Cookie).
// Use the response.Header to get the selected subprotocol // Use the response.Header to get the selected subprotocol
@ -123,10 +125,7 @@ var DefaultDialer = &Dialer{
func (d *Dialer) Dial(urlStr string, requestHeader http.Header) (*Conn, *http.Response, error) { func (d *Dialer) Dial(urlStr string, requestHeader http.Header) (*Conn, *http.Response, error) {
if d == nil { if d == nil {
d = &Dialer{ d = &nilDialer
Proxy: http.ProxyFromEnvironment,
HandshakeTimeout: 45 * time.Second,
}
} }
challengeKey, err := generateChallengeKey() challengeKey, err := generateChallengeKey()