fix(19.3-06): queue site-cache purges after commit and coalesce them (WR-01)

- PurgeSiteCacheAfterCommit queues the purge on a per-purger worker after
  commit: one request in flight, saves committed meanwhile collapse into
  one follow-up, so the admin request never waits for the site
- new cooler.SiteCachePurger.Wait drains the queue; Handle.Close waits for
  it at shutdown (timeout plus one second)
- Purge stays the synchronous send; exported names and signatures unchanged
This commit is contained in:
Jakub Zych
2026-10-15 22:36:24 +02:00
parent 50a584c757
commit db6a7f5c3f
6 changed files with 236 additions and 48 deletions

View File

@@ -94,21 +94,32 @@ type Cache interface {
Flush(ctx context.Context) error
}
// Handle is what Install set up. Close releases the Redis client Install
// opened, if any.
// Handle is what Install set up. Close drains the site-cache purger and
// releases the Redis client Install opened, if any.
type Handle struct {
client *redis.Client
prefix string
rateRedis bool
cache Cache
log *slog.Logger
purger *SiteCachePurger
}
// Close closes the Redis client Install opened. It is safe on a nil Handle
// and when no Redis driver was selected. A Redis-backed Cache that Install
// built on that client stops working; serve closes the handle on shutdown.
// Close first waits for the site-cache purges queued on the purger Install
// published, up to SiteCachePurgeTimeout plus one second, then closes the
// Redis client Install opened. It is safe on a nil Handle and when no Redis
// driver was selected. A Redis-backed Cache that Install built on that
// client stops working; serve closes the handle on shutdown.
func (h *Handle) Close() error {
if h == nil || h.client == nil {
if h == nil {
return nil
}
if h.purger != nil {
ctx, cancel := context.WithTimeout(context.Background(), SiteCachePurgeTimeout+time.Second)
_ = h.purger.Wait(ctx) // the deadline only bounds shutdown
cancel()
}
if h.client == nil {
return nil
}
err := h.client.Close()
@@ -169,9 +180,11 @@ func Install(ctx context.Context, app *backpack.App) (*Handle, error) {
}
cfg := app.Config
h := &Handle{prefix: keyPrefix(cfg), log: loggerFromApp(app)}
if err := installSiteCache(app, h.log); err != nil {
purger, err := installSiteCache(app, h.log)
if err != nil {
return nil, err
}
h.purger = purger
store, err := driverName(cfg, RateLimitStoreKey)
if err != nil {
return nil, err