Skip to content

Commit

Permalink
Merge pull request #161 from remicorniere/Issue_160
Browse files Browse the repository at this point in the history
Fix issue #160
  • Loading branch information
remicorniere authored Mar 16, 2020
2 parents ef6de60 + 0a4acd1 commit fe4c366
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
4 changes: 2 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ func (scs *SyncConnState) setState(cs ConnState) {
// This is a the list of events happening on the connection that the
// client can be notified about.
const (
InitialPresence = "<presence/>"
StateDisconnected ConnState = iota
StateResuming
StateSessionEstablished
StateStreamError
StatePermanentError
InitialPresence = "<presence/>"
)

// Event is a structure use to convey event changes related to client state. This
Expand Down Expand Up @@ -285,7 +285,7 @@ func (c *Client) Resume() error {
if err != nil {
return err
}
// Execute post reconnect hook. This can be different from the first connection hook, and not trigger roster retrival
// Execute post reconnect hook. This can be different from the first connection hook, and not trigger roster retrieval
// for example.
if c.PostResumeHook != nil {
err = c.PostResumeHook()
Expand Down
25 changes: 19 additions & 6 deletions stream_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ func (sm *StreamManager) Run() error {
sm.Metrics.setLoginTime()
case StateDisconnected:
// Reconnect on disconnection
return sm.resume(e.SMState)
return sm.resume()
case StateStreamError:
sm.client.Disconnect()
// Only try reconnecting if we have not been kicked by another session to avoid connection loop.
// TODO: Make this conflict exception a permanent error
if e.StreamError != "conflict" {
return sm.connect()
return sm.resume()
}
case StatePermanentError:
// Do not attempt to reconnect
Expand All @@ -113,19 +113,32 @@ func (sm *StreamManager) Stop() {
}

func (sm *StreamManager) connect() error {
var state SMState
return sm.resume(state)
if sm.client != nil {
if c, ok := sm.client.(*Client); ok {
if c.CurrentState.getState() == StateDisconnected {
sm.Metrics = initMetrics()
err := c.Connect()
if err != nil {
return err
}
if sm.PostConnect != nil {
sm.PostConnect(sm.client)
}
return nil
}
}
}
return errors.New("client is not disconnected")
}

// resume manages the reconnection loop and apply the define backoff to avoid overloading the server.
func (sm *StreamManager) resume(state SMState) error {
func (sm *StreamManager) resume() error {
var backoff backoff // TODO: Group backoff calculation features with connection manager?

for {
var err error
// TODO: Make it possible to define logger to log disconnect and reconnection attempts
sm.Metrics = initMetrics()

if err = sm.client.Resume(); err != nil {
var actualErr ConnError
if xerrors.As(err, &actualErr) {
Expand Down

0 comments on commit fe4c366

Please sign in to comment.