Add periodic bugle default check

This commit is contained in:
Tulir Asokan 2024-04-03 17:21:48 +03:00
parent fab55c21b9
commit 03db8d9296
3 changed files with 46 additions and 1 deletions

View file

@ -9,6 +9,7 @@ import (
"net"
"net/http"
"net/url"
"sync"
"time"
"github.com/google/uuid"
@ -69,6 +70,8 @@ type Client struct {
disconnecting bool
pingShortCircuit chan struct{}
nextBugleDefaultCheck time.Time
nextBugleDefaultCheckLock sync.Mutex
recentUpdates [8]updateDedupItem
recentUpdatesPtr int
@ -153,6 +156,7 @@ func (c *Client) Connect() error {
if err != nil {
return fmt.Errorf("failed to refresh auth token: %w", err)
}
c.bumpNextBugleDefaultCheck(10 * time.Minute)
//webEncryptionKeyResponse, err := c.GetWebEncryptionKey()
//if err != nil {

View file

@ -214,6 +214,7 @@ func (c *Client) handleUpdatesEvent(msg *IncomingRPCMessage) {
c.triggerEvent(&events.GaiaLoggedOut{})
return
}
c.bumpNextBugleDefaultCheck(DefaultBugleDefaultCheckInterval)
data, ok := msg.DecryptedMessage.(*gmproto.UpdateEvents)
if !ok {
c.Logger.Error().

View file

@ -183,6 +183,8 @@ func (dp *dittoPinger) Ping(pingID uint64, timeout time.Duration, timeoutCount i
}
}
const DefaultBugleDefaultCheckInterval = 55 * time.Minute
func (dp *dittoPinger) Loop() {
for {
select {
@ -197,8 +199,46 @@ func (dp *dittoPinger) Loop() {
case <-dp.stop:
return
}
if time.Until(dp.client.getNextBugleDefaultCheck()) <= 0 {
go dp.BugleDefaultCheck()
dp.client.bumpNextBugleDefaultCheck(DefaultBugleDefaultCheckInterval)
}
}
}
func (dp *dittoPinger) BugleDefaultCheck() {
dp.log.Debug().Msg("Doing bugle default check")
start := time.Now()
resp, err := dp.client.IsBugleDefault()
if err != nil {
dp.log.Err(err).
Dur("check_duration", time.Since(start)).
Msg("Failed to do bugle default check")
} else {
lvl := zerolog.DebugLevel
if !resp.Success {
lvl = zerolog.WarnLevel
}
dp.log.WithLevel(lvl).
Dur("check_duration", time.Since(start)).
Bool("bugle_default", resp.Success).
Msg("Got bugle default check response")
}
}
func (c *Client) getNextBugleDefaultCheck() time.Time {
c.nextBugleDefaultCheckLock.Lock()
defer c.nextBugleDefaultCheckLock.Unlock()
return c.nextBugleDefaultCheck
}
func (c *Client) bumpNextBugleDefaultCheck(after time.Duration) {
c.nextBugleDefaultCheckLock.Lock()
if time.Until(c.nextBugleDefaultCheck) < after {
c.nextBugleDefaultCheck = time.Now().Add(after)
}
c.nextBugleDefaultCheckLock.Unlock()
}
func tryReadBody(resp io.ReadCloser) []byte {
data, _ := io.ReadAll(resp)