From 42ee290fc520c243bbb5fd6e5a75e0725117e422 Mon Sep 17 00:00:00 2001 From: Steven Santos Erenst Date: Thu, 21 Jan 2021 00:26:29 -0800 Subject: [PATCH] Add the ability to customize the connection timeout (#122) Fixes #116 --- xmpp.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/xmpp.go b/xmpp.go index 3fa689d..281f123 100644 --- a/xmpp.go +++ b/xmpp.go @@ -76,7 +76,7 @@ func containsIgnoreCase(s, substr string) bool { 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 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 { return nil, err } @@ -153,6 +153,10 @@ type Options struct { // Password supplies the password to use for authentication with the remote server. 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 // from the server. Use "" to let the server generate one for your client. 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 { return nil, err }