diff --git a/libgm/client.go b/libgm/client.go index 905b100..2da2c86 100644 --- a/libgm/client.go +++ b/libgm/client.go @@ -9,6 +9,7 @@ import ( "net" "net/http" "net/url" + "sync" "time" "github.com/google/uuid" @@ -68,7 +69,9 @@ type Client struct { skipCount int disconnecting bool - pingShortCircuit chan struct{} + 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 { diff --git a/libgm/event_handler.go b/libgm/event_handler.go index fea6cbf..3b19423 100644 --- a/libgm/event_handler.go +++ b/libgm/event_handler.go @@ -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(). diff --git a/libgm/longpoll.go b/libgm/longpoll.go index 409b858..b9e1f63 100644 --- a/libgm/longpoll.go +++ b/libgm/longpoll.go @@ -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,9 +199,47 @@ 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) _ = resp.Close()