From 6e8ac5265c396efb9f5ed1571069205fcd7784ad Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Tue, 24 Nov 2026 01:27:20 +0000 Subject: [PATCH] fix(prewarming): run a manual cycle on the worker context TriggerPrewarm passed the request context to a background goroutine, so the cycle stopped when the HTTP request returned. The cycle now uses the worker context and Stop waits for it. --- pkg/app/handlers_prewarming.go | 5 ++-- pkg/prewarming/worker.go | 17 +++++++++-- pkg/prewarming/worker_test.go | 54 ++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 4 deletions(-) diff --git a/pkg/app/handlers_prewarming.go b/pkg/app/handlers_prewarming.go index 804ab66..0b11d46 100644 --- a/pkg/app/handlers_prewarming.go +++ b/pkg/app/handlers_prewarming.go @@ -1,6 +1,8 @@ package app import ( + "context" + "github.com/gofiber/fiber/v3" "github.com/rs/zerolog/log" ) @@ -18,8 +20,7 @@ func (a *App) handlePrewarmingStatus(c fiber.Ctx) error { func (a *App) handlePrewarmingTrigger(c fiber.Ctx) error { c.Set("Content-Type", "application/json") - ctx := c.Context() - a.prewarmWorker.TriggerPrewarm(ctx) + a.prewarmWorker.TriggerPrewarm(context.Background()) log.Info().Msg("Pre-warming manually triggered via API") diff --git a/pkg/prewarming/worker.go b/pkg/prewarming/worker.go index e71d0d4..9072c5a 100644 --- a/pkg/prewarming/worker.go +++ b/pkg/prewarming/worker.go @@ -43,6 +43,7 @@ type Worker struct { popularity Popularity fetcher Fetcher cancel context.CancelFunc + runCtx context.Context wg sync.WaitGroup mu sync.Mutex interval time.Duration @@ -112,6 +113,7 @@ func (w *Worker) Start(ctx context.Context) { } runCtx, cancel := context.WithCancel(ctx) w.cancel = cancel + w.runCtx = runCtx w.wg.Add(1) go w.run(runCtx) log.Info().Msg("Pre-warming worker started") @@ -264,7 +266,8 @@ func (w *Worker) Stop() { w.wg.Wait() } -// TriggerPrewarm manually triggers a pre-warming cycle +// TriggerPrewarm runs one cycle in the background. The cycle outlives ctx, which is +// usually a request context, and ends when the worker stops. func (w *Worker) TriggerPrewarm(ctx context.Context) { if !w.enabled || w.fetcher == nil || w.popularity == nil { log.Warn().Msg("Cannot trigger pre-warm: worker is disabled or unconfigured") @@ -272,7 +275,17 @@ func (w *Worker) TriggerPrewarm(ctx context.Context) { } log.Info().Msg("Manual pre-warming triggered") - go w.prewarmPopularPackages(ctx) + w.mu.Lock() + cycleCtx := w.runCtx + w.mu.Unlock() + if cycleCtx == nil { + cycleCtx = context.WithoutCancel(ctx) + } + w.wg.Add(1) + go func() { + defer w.wg.Done() + w.prewarmPopularPackages(cycleCtx) + }() } // PrewarmPackage pre-warms a specific package diff --git a/pkg/prewarming/worker_test.go b/pkg/prewarming/worker_test.go index b07972f..1500fb2 100644 --- a/pkg/prewarming/worker_test.go +++ b/pkg/prewarming/worker_test.go @@ -132,3 +132,57 @@ func TestPrewarmPackagePassesExplicitVersionAndReturnsError(t *testing.T) { require.ErrorContains(t, err, "boom") assert.Equal(t, "4.17.21", f.got["npm:lodash"].Version) } + +func drain(ch <-chan PackageInfo, want int) int { + n := 0 + for n < want { + select { + case <-ch: + n++ + case <-time.After(2 * time.Second): + return n + } + } + return n +} + +func TestTriggerPrewarmOutlivesACancelledCallerContext(t *testing.T) { + f := newFakeFetcher() + w := NewWorker(Config{Enabled: true, Fetcher: f, Popularity: samplePopularity(), Interval: time.Hour}) + w.Start(context.Background()) + defer w.Stop() + require.Equal(t, 3, drain(f.calls, 3), "initial cycle") + + reqCtx, cancel := context.WithCancel(context.Background()) + cancel() + w.TriggerPrewarm(reqCtx) + + assert.Equal(t, 3, drain(f.calls, 3), "a request context ending must not cancel the manual cycle") +} + +func TestTriggerPrewarmWithoutStartIgnoresCallerCancellation(t *testing.T) { + f := newFakeFetcher() + w := NewWorker(Config{Enabled: true, Fetcher: f, Popularity: samplePopularity()}) + + reqCtx, cancel := context.WithCancel(context.Background()) + cancel() + w.TriggerPrewarm(reqCtx) + + assert.Equal(t, 3, drain(f.calls, 3)) + w.wg.Wait() +} + +func TestStopEndsAManualCycle(t *testing.T) { + f := newFakeFetcher() + w := NewWorker(Config{Enabled: true, Fetcher: f, Popularity: samplePopularity(), Interval: time.Hour}) + w.Start(context.Background()) + w.TriggerPrewarm(context.Background()) + + done := make(chan struct{}) + go func() { w.Stop(); close(done) }() + select { + case <-done: + case <-time.After(5 * time.Second): + t.Fatal("Stop did not wait for the manual cycle to end") + } +}