Mercurial > code > home > repos > gcalendarwatch
changeset 80:e8164bd2f9a1
hoist startupJitter to main
author | drewp@bigasterisk.com |
---|---|
date | Fri, 06 Sep 2024 18:25:32 -0700 |
parents | 30ad34850ef1 |
children | 79320eff10f2 |
files | calsync/main.go calsync/sync_event.go |
diffstat | 2 files changed, 12 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- a/calsync/main.go Fri Sep 06 18:24:38 2024 -0700 +++ b/calsync/main.go Fri Sep 06 18:25:32 2024 -0700 @@ -28,6 +28,8 @@ "github.com/gorilla/mux" ) +const startupJitter = 0 * time.Second + func main() { startLogging() ctx := context.Background() @@ -55,7 +57,7 @@ initialSyncBack := (7 * 24) * time.Hour initialSyncAhead := (14 * 24) * time.Hour - err = updateMongoEventsToMatchGoogleForever(mc, gc, initialSyncBack, initialSyncAhead, notifications) + err = updateMongoEventsToMatchGoogleForever(mc, gc, initialSyncBack, initialSyncAhead, notifications, startupJitter) if err != nil { log.Fatal(err) }
--- a/calsync/sync_event.go Fri Sep 06 18:24:38 2024 -0700 +++ b/calsync/sync_event.go Fri Sep 06 18:25:32 2024 -0700 @@ -16,6 +16,9 @@ // Time that we'll still accept the old watchId after we've registered a new one. const grace = 1 * time.Minute +// How often to renew watches (roughly). +const watchLifetime = time.Duration(120) * time.Minute + // Each calendar syncs like this: // 1. Full sync of events taking place between `now-initialSyncBack` to `now+initialSyncAhead`. // 2. Garbage-collect all events last-modified before `now-initialSyncBack` @@ -26,6 +29,7 @@ initialSyncBack time.Duration, initialSyncAhead time.Duration, router *notificationrouter.NotificationRouter, + startupJitter time.Duration, ) error { log.Println("starting updateMongoEventsToMatchGoogleForever") @@ -43,7 +47,7 @@ for _, cal := range cals { rd := newCalEventsReader(mc, gc, router, cal, initialSyncT1, initialSyncT2) go func() { - err := rd.watchForUpdates() + err := rd.watchForUpdates(startupJitter) if err != nil { log.Println("ERROR", M.Prefix(rd.cal), "watchForUpdates: ", err, "(aborting this calendar)") } @@ -71,16 +75,14 @@ return &calEventsReader{mc, gc, router, cal, t1, t2, ""} } -func (r *calEventsReader) watchForUpdates() error { - r.watchForUpdatesJitteredStartup() +func (r *calEventsReader) watchForUpdates(startupJitter time.Duration) error { + r.watchForUpdatesJitteredStartup(startupJitter) err := r.updateInitialRange() if err != nil { return err } - watchLifetime := time.Duration(3) * time.Minute - syncFunc := notificationrouter.SyncFunc(func() { err := r.sync() if err != nil { @@ -91,8 +93,8 @@ return nil } -func (r *calEventsReader) watchForUpdatesJitteredStartup() { - jitteredStartup := time.Duration(rand.Int()%30) * time.Second +func (r *calEventsReader) watchForUpdatesJitteredStartup(startupJitter time.Duration) { + jitteredStartup := time.Duration(startupJitter.Seconds()*rand.Float64()) * time.Second log.Println(M.Prefix(r.cal), "watchForEvents starting in", jitteredStartup) time.Sleep(jitteredStartup) }