Reduce number of synchronizations GODT-313

* [x] expononential cooldown of retries
* [x] do not trigger sync by counts
* [x] randomization of event poll interval
This commit is contained in:
Jakub Cuth
2020-04-28 14:20:37 +00:00
parent a1b01d5922
commit b15d22c8cc
9 changed files with 227 additions and 6 deletions

View File

@ -18,6 +18,7 @@
package store
import (
"math/rand"
"time"
bridgeEvents "github.com/ProtonMail/proton-bridge/internal/events"
@ -28,6 +29,7 @@ import (
)
const pollInterval = 30 * time.Second
const pollIntervalSpread = 5 * time.Second
type eventLoop struct {
cache *Cache
@ -132,7 +134,7 @@ func (loop *eventLoop) start() { // nolint[funlen]
loop.log.WithField("lastEventID", loop.currentEventID).Warn("Subscription stopped")
}()
t := time.NewTicker(pollInterval)
t := time.NewTicker(pollInterval - pollIntervalSpread)
defer t.Stop()
loop.hasInternet = true
@ -145,8 +147,11 @@ func (loop *eventLoop) start() { // nolint[funlen]
case <-loop.stopCh:
close(loop.notifyStopCh)
return
case eventProcessedCh = <-loop.pollCh:
case <-t.C:
// Randomise periodic calls within range pollInterval ± pollSpread to reduces potential load spikes on API.
time.Sleep(time.Duration(rand.Intn(2*int(pollIntervalSpread.Milliseconds()))) * time.Millisecond)
case eventProcessedCh = <-loop.pollCh:
// We don't want to wait here. Polling should happen instantly.
}
// Before we fetch the first event, check whether this is the first time we've
@ -545,7 +550,7 @@ func (loop *eventLoop) processMessageCounts(l *logrus.Entry, messageCounts []*pm
return err
}
if !isSynced {
loop.store.triggerSync()
log.Error("The counts between DB and API are not matching")
}
return nil