Add the ability to customize the connection timeout (#122)

Fixes #116
This commit is contained in:
Steven Santos Erenst 2021-01-21 00:26:29 -08:00 committed by GitHub
parent da2b7586cd
commit 42ee290fc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

10
xmpp.go
View File

@ -76,7 +76,7 @@ func containsIgnoreCase(s, substr string) bool {
return strings.Contains(s, substr) return strings.Contains(s, substr)
} }
func connect(host, user, passwd string) (net.Conn, error) { func connect(host, user, passwd string, timeout time.Duration) (net.Conn, error) {
addr := host addr := host
if strings.TrimSpace(host) == "" { if strings.TrimSpace(host) == "" {
@ -117,7 +117,7 @@ func connect(host, user, passwd string) (net.Conn, error) {
} }
} }
c, err := net.Dial("tcp", addr) c, err := net.DialTimeout("tcp", addr, timeout)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -153,6 +153,10 @@ type Options struct {
// Password supplies the password to use for authentication with the remote server. // Password supplies the password to use for authentication with the remote server.
Password string Password string
// DialTimeout is the time limit for establishing a connection. A
// DialTimeout of zero means no timeout.
DialTimeout time.Duration
// Resource specifies an XMPP client resource, like "bot", instead of accepting one // Resource specifies an XMPP client resource, like "bot", instead of accepting one
// from the server. Use "" to let the server generate one for your client. // from the server. Use "" to let the server generate one for your client.
Resource string Resource string
@ -221,7 +225,7 @@ func (o Options) NewClient() (*Client, error) {
} }
} }
} }
c, err := connect(host, o.User, o.Password) c, err := connect(host, o.User, o.Password, o.DialTimeout)
if err != nil { if err != nil {
return nil, err return nil, err
} }