summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWim <wim@42.be>2017-07-01 23:28:16 +0200
committerWim <wim@42.be>2017-07-01 23:28:16 +0200
commita58a3e50000340c75f8c82a0421558cfa901b1e9 (patch)
treeae99add9715ebd56feeeece8eb6f98f191bf621f
parentba35212b673088be4096d25043094e3fb7a0d3ee (diff)
downloadmatterbridge-msglm-a58a3e50000340c75f8c82a0421558cfa901b1e9.tar.gz
matterbridge-msglm-a58a3e50000340c75f8c82a0421558cfa901b1e9.tar.bz2
matterbridge-msglm-a58a3e50000340c75f8c82a0421558cfa901b1e9.zip
Optimize StatusLoop. Execute function when specified in OnWsConnect
-rw-r--r--matterclient/matterclient.go26
1 files changed, 21 insertions, 5 deletions
diff --git a/matterclient/matterclient.go b/matterclient/matterclient.go
index 91795b37..d389f9bc 100644
--- a/matterclient/matterclient.go
+++ b/matterclient/matterclient.go
@@ -65,6 +65,7 @@ type MMClient struct {
WsSequence int64
WsPingChan chan *model.WebSocketResponse
ServerVersion string
+ OnWsConnect func()
}
func New(login, pass, team, server string) *MMClient {
@@ -724,6 +725,12 @@ func (m *MMClient) GetTeamId() string {
}
func (m *MMClient) StatusLoop() {
+ retries := 0
+ backoff := time.Second * 60
+ if m.OnWsConnect != nil {
+ m.OnWsConnect()
+ }
+ m.log.Debug("StatusLoop:", m.OnWsConnect)
for {
if m.WsQuit {
return
@@ -734,14 +741,23 @@ func (m *MMClient) StatusLoop() {
select {
case <-m.WsPingChan:
m.log.Debug("WS PONG received")
+ backoff = time.Second * 60
case <-time.After(time.Second * 5):
- m.Logout()
- m.WsQuit = false
- m.Login()
- go m.WsReceiver()
+ if retries > 3 {
+ m.Logout()
+ m.WsQuit = false
+ m.Login()
+ if m.OnWsConnect != nil {
+ m.OnWsConnect()
+ }
+ go m.WsReceiver()
+ } else {
+ retries++
+ backoff = time.Second * 5
+ }
}
}
- time.Sleep(time.Second * 60)
+ time.Sleep(backoff)
}
}