# HG changeset patch # User drewp@bigasterisk.com # Date 1725672332 25200 # Node ID e8164bd2f9a1807c4bf2d47eb6d662dbff6578c0 # Parent 30ad34850ef102c9a41fc498e90aba3d8468e4ab hoist startupJitter to main diff -r 30ad34850ef1 -r e8164bd2f9a1 calsync/main.go --- 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) } diff -r 30ad34850ef1 -r e8164bd2f9a1 calsync/sync_event.go --- 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) }