2019-06-18 14:28:30 +00:00
|
|
|
package xmpp
|
2019-06-06 09:58:50 +00:00
|
|
|
|
|
|
|
import (
|
2019-10-31 19:08:39 +00:00
|
|
|
"context"
|
2019-06-08 16:52:19 +00:00
|
|
|
"errors"
|
2019-06-13 15:22:39 +00:00
|
|
|
"sync"
|
2019-06-06 09:58:50 +00:00
|
|
|
"time"
|
2019-06-07 13:23:23 +00:00
|
|
|
|
|
|
|
"golang.org/x/xerrors"
|
2019-06-26 15:14:52 +00:00
|
|
|
"gosrc.io/xmpp/stanza"
|
2019-06-06 09:58:50 +00:00
|
|
|
)
|
|
|
|
|
2019-06-08 16:09:22 +00:00
|
|
|
// The Fluux XMPP lib can manage client or component XMPP streams.
|
|
|
|
// The StreamManager handles the stream workflow handling the common
|
|
|
|
// stream events and doing the right operations.
|
|
|
|
//
|
|
|
|
// It can handle:
|
2019-06-08 16:52:19 +00:00
|
|
|
// - Client
|
2019-06-08 16:09:22 +00:00
|
|
|
// - Stream establishment workflow
|
|
|
|
// - Reconnection strategies, with exponential backoff. It also takes into account
|
|
|
|
// permanent errors to avoid useless reconnection loops.
|
|
|
|
// - Metrics processing
|
|
|
|
|
2019-06-14 07:37:38 +00:00
|
|
|
// StreamClient is an interface used by StreamManager to control Client lifecycle,
|
|
|
|
// set callback and trigger reconnection.
|
2019-06-08 16:52:19 +00:00
|
|
|
type StreamClient interface {
|
2019-06-08 16:09:22 +00:00
|
|
|
Connect() error
|
2020-03-06 15:44:01 +00:00
|
|
|
Resume() error
|
2019-06-29 07:35:33 +00:00
|
|
|
Send(packet stanza.Packet) error
|
2020-01-31 10:48:03 +00:00
|
|
|
SendIQ(ctx context.Context, iq *stanza.IQ) (chan stanza.IQ, error)
|
2019-06-29 07:35:33 +00:00
|
|
|
SendRaw(packet string) error
|
2019-12-26 13:47:02 +00:00
|
|
|
Disconnect() error
|
2019-06-08 16:09:22 +00:00
|
|
|
SetHandler(handler EventHandler)
|
|
|
|
}
|
2019-06-06 09:58:50 +00:00
|
|
|
|
2019-06-14 07:37:38 +00:00
|
|
|
// Sender is an interface provided by Stream clients to allow sending XMPP data.
|
2019-06-29 07:35:33 +00:00
|
|
|
// It is mostly use in callback to pass a limited subset of the stream client interface
|
2019-06-14 07:37:38 +00:00
|
|
|
type Sender interface {
|
2019-06-26 15:14:52 +00:00
|
|
|
Send(packet stanza.Packet) error
|
2020-01-31 10:48:03 +00:00
|
|
|
SendIQ(ctx context.Context, iq *stanza.IQ) (chan stanza.IQ, error)
|
2019-06-14 07:37:38 +00:00
|
|
|
SendRaw(packet string) error
|
|
|
|
}
|
|
|
|
|
2019-06-08 16:09:22 +00:00
|
|
|
// StreamManager supervises an XMPP client connection. Its role is to handle connection events and
|
2019-06-06 09:58:50 +00:00
|
|
|
// apply reconnection strategy.
|
2019-06-08 16:09:22 +00:00
|
|
|
type StreamManager struct {
|
2019-06-08 16:52:19 +00:00
|
|
|
client StreamClient
|
2019-06-06 09:58:50 +00:00
|
|
|
PostConnect PostConnect
|
|
|
|
|
|
|
|
// Store low level metrics
|
|
|
|
Metrics *Metrics
|
2019-06-13 15:22:39 +00:00
|
|
|
|
|
|
|
wg sync.WaitGroup
|
2019-06-06 09:58:50 +00:00
|
|
|
}
|
|
|
|
|
2019-06-29 07:35:33 +00:00
|
|
|
type PostConnect func(c Sender)
|
2019-06-08 16:09:22 +00:00
|
|
|
|
|
|
|
// NewStreamManager creates a new StreamManager structure, intended to support
|
2019-06-06 09:58:50 +00:00
|
|
|
// handling XMPP client state event changes and auto-trigger reconnection
|
2019-06-08 16:09:22 +00:00
|
|
|
// based on StreamManager configuration.
|
2019-06-08 16:52:19 +00:00
|
|
|
// TODO: Move parameters to Start and remove factory method
|
|
|
|
func NewStreamManager(client StreamClient, pc PostConnect) *StreamManager {
|
2019-06-08 16:09:22 +00:00
|
|
|
return &StreamManager{
|
2019-06-08 16:52:19 +00:00
|
|
|
client: client,
|
2019-06-06 09:58:50 +00:00
|
|
|
PostConnect: pc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-29 07:35:33 +00:00
|
|
|
// Run launches the connection of the underlying client or component
|
2019-06-13 15:22:39 +00:00
|
|
|
// and wait until Disconnect is called, or for the manager to terminate due
|
|
|
|
// to an unrecoverable error.
|
|
|
|
func (sm *StreamManager) Run() error {
|
2019-06-08 16:52:19 +00:00
|
|
|
if sm.client == nil {
|
|
|
|
return errors.New("missing stream client")
|
|
|
|
}
|
|
|
|
|
2019-11-28 16:15:15 +00:00
|
|
|
handler := func(e Event) error {
|
2020-03-06 15:44:01 +00:00
|
|
|
switch e.State.state {
|
2019-06-06 09:58:50 +00:00
|
|
|
case StateSessionEstablished:
|
2019-06-08 16:52:19 +00:00
|
|
|
sm.Metrics.setLoginTime()
|
2019-06-06 09:58:50 +00:00
|
|
|
case StateDisconnected:
|
|
|
|
// Reconnect on disconnection
|
2020-03-16 15:20:54 +00:00
|
|
|
return sm.resume()
|
2019-06-08 09:15:51 +00:00
|
|
|
case StateStreamError:
|
2019-06-08 16:52:19 +00:00
|
|
|
sm.client.Disconnect()
|
2019-06-08 09:15:51 +00:00
|
|
|
// Only try reconnecting if we have not been kicked by another session to avoid connection loop.
|
2019-11-08 11:07:55 +00:00
|
|
|
// TODO: Make this conflict exception a permanent error
|
2019-06-08 09:15:51 +00:00
|
|
|
if e.StreamError != "conflict" {
|
2020-03-16 15:20:54 +00:00
|
|
|
return sm.resume()
|
2019-06-08 09:15:51 +00:00
|
|
|
}
|
2019-11-08 11:07:55 +00:00
|
|
|
case StatePermanentError:
|
|
|
|
// Do not attempt to reconnect
|
2019-06-06 09:58:50 +00:00
|
|
|
}
|
2019-11-28 16:15:15 +00:00
|
|
|
return nil
|
2019-06-06 09:58:50 +00:00
|
|
|
}
|
2019-06-08 16:52:19 +00:00
|
|
|
sm.client.SetHandler(handler)
|
2019-06-07 13:23:23 +00:00
|
|
|
|
2019-06-13 15:22:39 +00:00
|
|
|
sm.wg.Add(1)
|
|
|
|
if err := sm.connect(); err != nil {
|
|
|
|
sm.wg.Done()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sm.wg.Wait()
|
|
|
|
return nil
|
2019-06-06 09:58:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop cancels pending operations and terminates existing XMPP client.
|
2019-06-08 16:52:19 +00:00
|
|
|
func (sm *StreamManager) Stop() {
|
2019-06-06 09:58:50 +00:00
|
|
|
// Remove on disconnect handler to avoid triggering reconnect
|
2019-06-08 16:52:19 +00:00
|
|
|
sm.client.SetHandler(nil)
|
|
|
|
sm.client.Disconnect()
|
2019-06-13 15:22:39 +00:00
|
|
|
sm.wg.Done()
|
2019-06-06 09:58:50 +00:00
|
|
|
}
|
|
|
|
|
2019-06-08 16:52:19 +00:00
|
|
|
func (sm *StreamManager) connect() error {
|
2020-03-16 15:20:54 +00:00
|
|
|
if sm.client != nil {
|
2021-12-18 15:33:13 +00:00
|
|
|
var scs *SyncConnState
|
|
|
|
if client, ok := sm.client.(*Client); ok {
|
|
|
|
scs = &client.CurrentState
|
|
|
|
}
|
|
|
|
if component, ok := sm.client.(*Component); ok {
|
|
|
|
scs = &component.CurrentState
|
|
|
|
}
|
|
|
|
if scs != nil && scs.getState() == StateDisconnected {
|
|
|
|
sm.Metrics = initMetrics()
|
|
|
|
err := sm.client.Connect()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if sm.PostConnect != nil {
|
|
|
|
sm.PostConnect(sm.client)
|
2020-03-16 15:20:54 +00:00
|
|
|
}
|
2021-12-18 15:33:13 +00:00
|
|
|
return nil
|
2020-03-16 15:20:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return errors.New("client is not disconnected")
|
2019-07-31 16:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// resume manages the reconnection loop and apply the define backoff to avoid overloading the server.
|
2020-03-16 15:20:54 +00:00
|
|
|
func (sm *StreamManager) resume() error {
|
2019-06-22 09:13:33 +00:00
|
|
|
var backoff backoff // TODO: Group backoff calculation features with connection manager?
|
2019-06-06 09:58:50 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
var err error
|
2019-06-07 13:23:23 +00:00
|
|
|
// TODO: Make it possible to define logger to log disconnect and reconnection attempts
|
2019-06-08 16:52:19 +00:00
|
|
|
sm.Metrics = initMetrics()
|
2020-03-06 15:44:01 +00:00
|
|
|
if err = sm.client.Resume(); err != nil {
|
2019-06-07 13:23:23 +00:00
|
|
|
var actualErr ConnError
|
|
|
|
if xerrors.As(err, &actualErr) {
|
|
|
|
if actualErr.Permanent {
|
2019-07-15 10:18:35 +00:00
|
|
|
return xerrors.Errorf("unrecoverable connect error %#v", actualErr)
|
2019-06-07 13:23:23 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-22 09:13:33 +00:00
|
|
|
backoff.wait()
|
2019-06-08 09:15:51 +00:00
|
|
|
} else { // We are connected, we can leave the retry loop
|
2019-06-06 09:58:50 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-08 16:52:19 +00:00
|
|
|
if sm.PostConnect != nil {
|
|
|
|
sm.PostConnect(sm.client)
|
2019-06-06 09:58:50 +00:00
|
|
|
}
|
2019-06-07 13:23:23 +00:00
|
|
|
return nil
|
2019-06-06 09:58:50 +00:00
|
|
|
}
|
|
|
|
|
2019-06-08 16:09:22 +00:00
|
|
|
// Stream Metrics
|
2019-06-06 09:58:50 +00:00
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
type Metrics struct {
|
|
|
|
startTime time.Time
|
|
|
|
// LoginTime returns the between client initiation of the TCP/IP
|
|
|
|
// connection to the server and the return of the login result.
|
2019-09-04 17:16:44 +00:00
|
|
|
// This includes ConnectTime, but also XMPP level protocol negotiation
|
2019-06-06 09:58:50 +00:00
|
|
|
// like starttls.
|
|
|
|
LoginTime time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
// initMetrics set metrics with default value and define the starting point
|
|
|
|
// for duration calculation (connect time, login time, etc).
|
|
|
|
func initMetrics() *Metrics {
|
|
|
|
return &Metrics{
|
|
|
|
startTime: time.Now(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Metrics) setLoginTime() {
|
|
|
|
m.LoginTime = time.Since(m.startTime)
|
|
|
|
}
|