Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type Controller struct {
config *Config
reporter reporter.Reporter
tracer *tracer.Tracer

cancelFunc context.CancelFunc
}

// New creates a new controller
Expand All @@ -48,6 +50,8 @@ func (c *Controller) Start(ctx context.Context) error {
intervals := times.New(c.config.ReporterInterval, c.config.MonitorInterval,
c.config.ProbabilisticInterval)

ctx, c.cancelFunc = context.WithCancel(ctx)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid shadowing conflicts, the new context should be named differently to the context that is given as argument.

Copy link
Contributor Author

@rogercoll rogercoll Jan 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case we intentionally want to override the parent context and prevent its use further down the function. I think that reusing ctx here aligns with Google best practices (“It’s OK to do this when the original value is no longer needed”). Since the derived context fully replaces the parent, keeping the same name makes that intent clear.

Nonetheless, I don't have a strong opinion. I can name it differently if that feels clearer or easier to follow.


// Start periodic synchronization with the realtime clock
times.StartRealtimeSync(ctx, c.config.ClockSyncInterval)

Expand Down Expand Up @@ -151,6 +155,10 @@ func (c *Controller) Start(ctx context.Context) error {
// Shutdown stops the controller
func (c *Controller) Shutdown() {
log.Info("Stop processing ...")
if c.cancelFunc != nil {
c.cancelFunc()
}

if c.reporter != nil {
c.reporter.Stop()
}
Expand Down