fix(plugin): pause every cache purge drain while Cloudflare rate-limits purges

Merged
RI
1h ago
+336-1

Summary

Follow-up to #4303, found while monitoring it in prod (capgo-23.2.1). #4303 made each drain stop after a failed batch, but the /updates cache purge queue still grew: 739, then 747, then 756 due apps over 80 s. The API worker kept logging hundreds of Cloudflare 429s a minute, mostly on the second zone.

The cause is that a failed batch only requeued its own apps. Every other drain kept claiming a batch per second, up to 6,000 tags a minute per zone. Each channel or version update nudges a drain, so about 480 drains started in 90 s. All of them called the rate-limited purge API, so its quota never recovered.

  • Global back-off: migration 20261201120200_updates_cache_purge_global_backoff changes ack_updates_cache_purge(..., p_success => false, ...). A failed ack now also moves updates_cache_purge_state.last_claim_at to the end of the Retry-After window. claim_updates_cache_purge then answers throttled to every drain until it ends. Same function signature, and successful acks are unchanged.
  • Longer wait on 429: Cloudflare's purge API answers 429 without Retry-After. A 429 now waits 30 s instead of 5 s; other failures stay at 5 s.

Failed apps are still requeued, never dropped. Once the limit clears, the next claim resumes the queue.

Test plan

  • New pgTAP test supabase/tests/99_test_updates_cache_purge_backoff.sql checks four things:
    • A failed ack requeues the app at its Retry-After.
    • A failed ack moves the shared throttle.
    • A second claim is throttled while another app is due.
    • A successful ack leaves the throttle alone.
  • Unit test: a 429 without Retry-After gives 30 s; a 500 gives 5 s.
  • bun run test:unit (679 files), typecheck:backend, lint:backend, migration order check
  • CI, including pgTAP against fresh Postgres
  • After deploy: 429 logs drop, and updates_cache_purge_pending drains.

No rendered UI changes, so there are no screenshots.

šŸ¤– Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved cache-purge retry handling for rate-limited requests: when no valid retry delay is provided, the system waits 30 seconds before trying again. Other failed requests continue to use a 5-second default.
    • Coordinated retry timing across purge requests to reduce repeated attempts during backoff periods.