Send extra GET_UPDATES requests instead of IS_BUGLE_DEFAULT

This commit is contained in:
Tulir Asokan 2024-04-05 12:54:49 +03:00
parent f6a9f7811d
commit 3d0983203d
3 changed files with 27 additions and 33 deletions

View file

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

View file

@ -215,7 +215,7 @@ func (c *Client) handleUpdatesEvent(msg *IncomingRPCMessage) {
return return
} }
if !msg.IsOld { if !msg.IsOld {
c.bumpNextBugleDefaultCheck(DefaultBugleDefaultCheckInterval) c.bumpNextDataReceiveCheck(DefaultBugleDefaultCheckInterval)
} }
data, ok := msg.DecryptedMessage.(*gmproto.UpdateEvents) data, ok := msg.DecryptedMessage.(*gmproto.UpdateEvents)
if !ok { if !ok {

View file

@ -183,7 +183,7 @@ func (dp *dittoPinger) Ping(pingID uint64, timeout time.Duration, timeoutCount i
} }
} }
const DefaultBugleDefaultCheckInterval = 55 * time.Minute const DefaultBugleDefaultCheckInterval = 1*time.Hour + 55*time.Minute
func (dp *dittoPinger) Loop() { func (dp *dittoPinger) Loop() {
for { for {
@ -199,48 +199,42 @@ func (dp *dittoPinger) Loop() {
case <-dp.stop: case <-dp.stop:
return return
} }
if dp.client.shouldDoBugleDefaultCheck() { if dp.client.shouldDoDataReceiveCheck() {
go dp.BugleDefaultCheck() go dp.HandleNoRecentUpdates()
} }
} }
} }
func (dp *dittoPinger) BugleDefaultCheck() { func (dp *dittoPinger) HandleNoRecentUpdates() {
dp.log.Debug().Msg("Doing bugle default check") dp.log.Debug().Msg("No data received recently, sending extra GET_UPDATES call")
start := time.Now() err := dp.client.sessionHandler.sendMessageNoResponse(SendMessageParams{
resp, err := dp.client.IsBugleDefault() Action: gmproto.ActionType_GET_UPDATES,
OmitTTL: true,
RequestID: dp.client.sessionHandler.sessionID,
})
if err != nil { if err != nil {
dp.log.Err(err). dp.log.Err(err).Msg("Failed to send extra GET_UPDATES call")
Dur("check_duration", time.Since(start)).
Msg("Failed to do bugle default check")
} else { } else {
lvl := zerolog.DebugLevel dp.log.Debug().Msg("Sent extra GET_UPDATES call")
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) shouldDoBugleDefaultCheck() bool { func (c *Client) shouldDoDataReceiveCheck() bool {
c.nextBugleDefaultCheckLock.Lock() c.nextDataReceiveCheckLock.Lock()
defer c.nextBugleDefaultCheckLock.Unlock() defer c.nextDataReceiveCheckLock.Unlock()
if time.Until(c.nextBugleDefaultCheck) <= 0 { if time.Until(c.nextDataReceiveCheck) <= 0 {
c.nextBugleDefaultCheck = time.Now().Add(DefaultBugleDefaultCheckInterval) c.nextDataReceiveCheck = time.Now().Add(DefaultBugleDefaultCheckInterval)
return true return true
} }
return false return false
} }
func (c *Client) bumpNextBugleDefaultCheck(after time.Duration) { func (c *Client) bumpNextDataReceiveCheck(after time.Duration) {
c.nextBugleDefaultCheckLock.Lock() c.nextDataReceiveCheckLock.Lock()
if time.Until(c.nextBugleDefaultCheck) < after { if time.Until(c.nextDataReceiveCheck) < after {
c.nextBugleDefaultCheck = time.Now().Add(after) c.nextDataReceiveCheck = time.Now().Add(after)
} }
c.nextBugleDefaultCheckLock.Unlock() c.nextDataReceiveCheckLock.Unlock()
} }
func tryReadBody(resp io.ReadCloser) []byte { func tryReadBody(resp io.ReadCloser) []byte {