Support connection timeout and retries

This commit is contained in:
Mickael Remond 2016-02-17 13:45:39 +01:00
parent 82c01de54b
commit a2aab652a9
3 changed files with 25 additions and 8 deletions

View file

@ -7,6 +7,7 @@ import (
"fmt"
"net"
"strings"
"time"
)
// Client is the main structure use to connect as a client on an XMPP
@ -47,6 +48,9 @@ func NewClient(options Options) (c *Client, err error) {
return
}
if c.options.ConnectTimeout == 0 {
c.options.ConnectTimeout = 15 // 15 second as default
}
return
}
@ -71,10 +75,21 @@ func checkAddress(addr string) (string, error) {
func (c *Client) Connect() (*Session, error) {
var tcpconn net.Conn
var err error
if tcpconn, err = net.Dial("tcp", c.options.Address); err != nil {
// TODO: Refactor = abstract retry loop in capped exponential back-off function
var try = 0
var success bool
for try <= c.options.Retry || !success {
if tcpconn, err = net.DialTimeout("tcp", c.options.Address, time.Duration(c.options.ConnectTimeout)*time.Second); err == nil {
success = true
}
try++
}
if err != nil {
return nil, err
}
// Connection is ok, we now open XMPP session
c.conn = tcpconn
if c.conn, c.Session, err = NewSession(c.conn, c.options); err != nil {
return c.Session, err

View file

@ -9,4 +9,6 @@ type Options struct {
Password string
PacketLogger *os.File // Used for debugging
Lang string // TODO: should default to 'en'
Retry int // Number of retries for connect
ConnectTimeout int // Connection timeout in seconds. Default to 15
}

View file

@ -80,7 +80,7 @@ func next(p *xml.Decoder) (xml.Name, interface{}, error) {
case nsClient + " message":
nv = &ClientMessage{}
case nsClient + " presence":
nv = &clientPresence{}
nv = &ClientPresence{}
case nsClient + " iq":
nv = &ClientIQ{}
default: