Make keepalive interval configurable

This fixes #133
This commit is contained in:
Wichert Akkerman 2019-12-04 22:17:58 +01:00
parent 6a3833b27d
commit f41177775a
2 changed files with 14 additions and 10 deletions

View File

@ -108,6 +108,9 @@ Setting up the client / Checking the parameters
// If host is not specified, the DNS SRV should be used to find the host from the domainpart of the JID. // If host is not specified, the DNS SRV should be used to find the host from the domainpart of the JID.
// Default the port to 5222. // Default the port to 5222.
func NewClient(config Config, r *Router) (c *Client, err error) { func NewClient(config Config, r *Router) (c *Client, err error) {
if config.KeepaliveInterval == 0 {
config.KeepaliveInterval = time.Second * 30
}
// Parse JID // Parse JID
if config.parsedJid, err = NewJid(config.Jid); err != nil { if config.parsedJid, err = NewJid(config.Jid); err != nil {
err = errors.New("missing jid") err = errors.New("missing jid")
@ -185,7 +188,7 @@ func (c *Client) Resume(state SMState) error {
// Start the keepalive go routine // Start the keepalive go routine
keepaliveQuit := make(chan struct{}) keepaliveQuit := make(chan struct{})
go keepalive(c.transport, keepaliveQuit) go keepalive(c.transport, c.config.KeepaliveInterval, keepaliveQuit)
// Start the receiver go routine // Start the receiver go routine
state = c.Session.SMState state = c.Session.SMState
// Leaving this channel here for later. Not used atm. We should return this instead of an error because right // Leaving this channel here for later. Not used atm. We should return this instead of an error because right
@ -312,9 +315,8 @@ func (c *Client) recv(state SMState, keepaliveQuit chan<- struct{}, errChan chan
// Loop: send whitespace keepalive to server // Loop: send whitespace keepalive to server
// This is use to keep the connection open, but also to detect connection loss // This is use to keep the connection open, but also to detect connection loss
// and trigger proper client connection shutdown. // and trigger proper client connection shutdown.
func keepalive(transport Transport, quit <-chan struct{}) { func keepalive(transport Transport, interval time.Duration, quit <-chan struct{}) {
// TODO: Make keepalive interval configurable ticker := time.NewTicker(interval)
ticker := time.NewTicker(30 * time.Second)
for { for {
select { select {
case <-ticker.C: case <-ticker.C:

View File

@ -2,6 +2,7 @@ package xmpp
import ( import (
"os" "os"
"time"
) )
// Config & TransportConfiguration must not be modified after having been passed to NewClient. Any // Config & TransportConfiguration must not be modified after having been passed to NewClient. Any
@ -14,6 +15,7 @@ type Config struct {
Credential Credential Credential Credential
StreamLogger *os.File // Used for debugging StreamLogger *os.File // Used for debugging
Lang string // TODO: should default to 'en' Lang string // TODO: should default to 'en'
KeepaliveInterval time.Duration // Interval between keepalive packets
ConnectTimeout int // Client timeout in seconds. Default to 15 ConnectTimeout int // Client timeout in seconds. Default to 15
// Insecure can be set to true to allow to open a session without TLS. If TLS // Insecure can be set to true to allow to open a session without TLS. If TLS
// is supported on the server, we will still try to use it. // is supported on the server, we will still try to use it.