changeset 75:5354739c9b0a

refactor jitter code
author drewp@bigasterisk.com
date Fri, 06 Sep 2024 17:52:37 -0700
parents ed7f655f22b8
children 470eacf452cb
files calsync/sync_event.go
diffstat 1 files changed, 9 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/calsync/sync_event.go	Fri Sep 06 17:51:15 2024 -0700
+++ b/calsync/sync_event.go	Fri Sep 06 17:52:37 2024 -0700
@@ -65,10 +65,8 @@
 	return &calEventsReader{mc, gc, router, cal, t1, t2, ""}
 }
 
-func (r *calEventsReader) watchForUpdates() {
-	jitteredStartup := time.Duration(rand.Int()%30) * time.Second
-	log.Println(M.Prefix(r.cal), "watchForEvents starting in", jitteredStartup)
-	time.Sleep(jitteredStartup)
+func (r *calEventsReader) watchForUpdates() error {
+	r.watchForUpdatesJitteredStartup()
 
 	err := r.updateInitialRange()
 	if err != nil {
@@ -87,12 +85,18 @@
 	go r.renewWatchEventsForever(watchLifetime, syncFunc)
 }
 
+func (r *calEventsReader) watchForUpdatesJitteredStartup() {
+	jitteredStartup := time.Duration(rand.Int()%30) * time.Second
+	log.Println(M.Prefix(r.cal), "watchForEvents starting in", jitteredStartup)
+	time.Sleep(jitteredStartup)
+}
+
 func (r *calEventsReader) renewWatchEventsForever(lifetime time.Duration, syncFunc notificationrouter.SyncFunc) {
 	log.Println(M.Prefix(r.cal), "renewWatchEventsForever")
 
 	for {
 		watchId := notificationrouter.NewWatchId()
-		jitterScale := rand.Float64()*1 + 0.5
+		jitterScale := 0.5 + 1.0*rand.Float64()
 		jitteredLifetime := time.Duration(lifetime.Seconds()*jitterScale) * time.Second
 
 		r.router.AddHandler(watchId, syncFunc, jitteredLifetime+grace)