Skip to content

Commit

Permalink
Use non-blocking send for route reloading
Browse files Browse the repository at this point in the history
This ensures we don't reload more than we need to. If a notification
already exists to reload, then we don't need to send another
notification, as all updates will be captured in the next reload.
  • Loading branch information
theseanything committed Dec 2, 2024
1 parent 0c38590 commit 1dc32f0
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions lib/load_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,18 @@ func (rt *Router) listenForContentStoreUpdates(ctx context.Context) error {
},
}

listener.Handle("route_changes", pgxlisten.HandlerFunc(
func(ctx context.Context, notification *pgconn.Notification, conn *pgx.Conn) error {
rt.CsReloadChan <- true
return nil
}),
listener.Handle(
"route_changes",
pgxlisten.HandlerFunc(
func(ctx context.Context, notification *pgconn.Notification, conn *pgx.Conn) error {
// 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:
default:
}
return nil
},
),
)

err := listener.Listen(ctx)
Expand All @@ -145,7 +152,11 @@ func (rt *Router) PeriodicCSRouteUpdates() {
tick := time.Tick(time.Minute)
for range tick {
if time.Since(rt.csLastReloadTime) > time.Minute {
rt.CsReloadChan <- true
// 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:
default:
}
}
}
}
Expand Down

0 comments on commit 1dc32f0

Please sign in to comment.