diff options
author | Wim <wim@42.be> | 2022-06-11 23:07:42 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-11 23:07:42 +0200 |
commit | 8751fb4bb1eb7cd34ed63be9b3801b8aeac71a1d (patch) | |
tree | 601d2616b05b5b197bd2a3ae7cb245b1a0ea17e7 /vendor/go.mau.fi/whatsmeow/keepalive.go | |
parent | 3819062574ac7e4af6a562bf40a425469a7752fb (diff) | |
download | matterbridge-msglm-8751fb4bb1eb7cd34ed63be9b3801b8aeac71a1d.tar.gz matterbridge-msglm-8751fb4bb1eb7cd34ed63be9b3801b8aeac71a1d.tar.bz2 matterbridge-msglm-8751fb4bb1eb7cd34ed63be9b3801b8aeac71a1d.zip |
Update dependencies (#1841)
Diffstat (limited to 'vendor/go.mau.fi/whatsmeow/keepalive.go')
-rw-r--r-- | vendor/go.mau.fi/whatsmeow/keepalive.go | 28 |
1 files changed, 22 insertions, 6 deletions
diff --git a/vendor/go.mau.fi/whatsmeow/keepalive.go b/vendor/go.mau.fi/whatsmeow/keepalive.go index ec05df1d..d5e40286 100644 --- a/vendor/go.mau.fi/whatsmeow/keepalive.go +++ b/vendor/go.mau.fi/whatsmeow/keepalive.go @@ -13,6 +13,7 @@ import ( waBinary "go.mau.fi/whatsmeow/binary" "go.mau.fi/whatsmeow/types" + "go.mau.fi/whatsmeow/types/events" ) var ( @@ -25,12 +26,27 @@ var ( ) func (cli *Client) keepAliveLoop(ctx context.Context) { + var lastSuccess time.Time + var errorCount int for { interval := rand.Int63n(KeepAliveIntervalMax.Milliseconds()-KeepAliveIntervalMin.Milliseconds()) + KeepAliveIntervalMin.Milliseconds() select { case <-time.After(time.Duration(interval) * time.Millisecond): - if !cli.sendKeepAlive(ctx) { + isSuccess, shouldContinue := cli.sendKeepAlive(ctx) + if !shouldContinue { return + } else if !isSuccess { + errorCount++ + go cli.dispatchEvent(&events.KeepAliveTimeout{ + ErrorCount: errorCount, + LastSuccess: lastSuccess, + }) + } else { + if errorCount > 0 { + errorCount = 0 + go cli.dispatchEvent(&events.KeepAliveRestored{}) + } + lastSuccess = time.Now() } case <-ctx.Done(): return @@ -38,7 +54,7 @@ func (cli *Client) keepAliveLoop(ctx context.Context) { } } -func (cli *Client) sendKeepAlive(ctx context.Context) bool { +func (cli *Client) sendKeepAlive(ctx context.Context) (isSuccess, shouldContinue bool) { respCh, err := cli.sendIQAsync(infoQuery{ Namespace: "w:p", Type: "get", @@ -47,16 +63,16 @@ func (cli *Client) sendKeepAlive(ctx context.Context) bool { }) if err != nil { cli.Log.Warnf("Failed to send keepalive: %v", err) - return true + return false, true } select { case <-respCh: // All good + return true, true case <-time.After(KeepAliveResponseDeadline): - // TODO disconnect websocket? cli.Log.Warnf("Keepalive timed out") + return false, true case <-ctx.Done(): - return false + return false, false } - return true } |