Skip to content

Commit

Permalink
Merge pull request #502 from alphagov/fix-periodic-reload
Browse files Browse the repository at this point in the history
Improve periodic reload
  • Loading branch information
theseanything authored Dec 6, 2024
2 parents ae7c575 + d54fd93 commit 42a55ab
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
7 changes: 4 additions & 3 deletions lib/load_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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()

Expand All @@ -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))

}
24 changes: 12 additions & 12 deletions lib/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 42a55ab

Please sign in to comment.