From d54fd937c254a3ef0a778f27e43573356bd098c4 Mon Sep 17 00:00:00 2001 From: Sean Rankine Date: Fri, 6 Dec 2024 11:33:30 +0000 Subject: [PATCH] Fix periodic reload Currently, it reloads every minute regardless if a reload has occured within that minute. This is because we never set the last reload time. This also improves the responsivness by checking for a periodic reload every 5 seconds, instead of a minute (which could potentially mean a reload could take up to 2 minutes). --- lib/load_routes.go | 7 ++++--- lib/router.go | 24 ++++++++++++------------ 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/lib/load_routes.go b/lib/load_routes.go index 18f04dd0..51ae561a 100644 --- a/lib/load_routes.go +++ b/lib/load_routes.go @@ -149,9 +149,9 @@ func (rt *Router) listenForContentStoreUpdates(ctx context.Context) error { } func (rt *Router) PeriodicCSRouteUpdates() { - tick := time.Tick(time.Minute) + tick := time.Tick(5 * time.Second) for range tick { - if time.Since(rt.csLastReloadTime) > time.Minute { + if time.Since(rt.csLastAttemptReloadTime) > time.Minute { // This is a non-blocking send, if there is already a notification to reload we don't need to send another one select { case rt.CsReloadChan <- true: @@ -187,6 +187,8 @@ func (rt *Router) reloadCsRoutes(pool PgxIface) { timer.ObserveDuration() }() + rt.csLastAttemptReloadTime = time.Now() + logInfo("router: reloading routes from content store") newmux := triemux.NewMux() @@ -204,5 +206,4 @@ func (rt *Router) reloadCsRoutes(pool PgxIface) { logInfo(fmt.Sprintf("router: reloaded %d routes from content store", routeCount)) routesCountMetric.WithLabelValues("content-store").Set(float64(routeCount)) - } diff --git a/lib/router.go b/lib/router.go index 6109e4ec..fa7baae6 100644 --- a/lib/router.go +++ b/lib/router.go @@ -42,18 +42,18 @@ const ( // come from, Route and Backend should not contain bson fields. // MongoReplicaSet, MongoReplicaSetMember etc. should move out of this module. type Router struct { - backends map[string]http.Handler - mux *triemux.Mux - csMux *triemux.Mux - lock sync.RWMutex - mongoReadToOptime bson.MongoTimestamp - logger logger.Logger - opts Options - ReloadChan chan bool - CsReloadChan chan bool - csMuxSampleRate float64 - csLastReloadTime time.Time - pool *pgxpool.Pool + backends map[string]http.Handler + mux *triemux.Mux + csMux *triemux.Mux + lock sync.RWMutex + mongoReadToOptime bson.MongoTimestamp + logger logger.Logger + opts Options + ReloadChan chan bool + CsReloadChan chan bool + csMuxSampleRate float64 + csLastAttemptReloadTime time.Time + pool *pgxpool.Pool } type Options struct {