Skip to content

fix(delivery): stop polling the database every second while nothing is configured - #631

Merged
Bccorb merged 2 commits into
mainfrom
fix/idle-delivery-polling
Dec 2, 2026
Merged

Bccorb merged 2 commits into
mainfrom
fix/idle-delivery-polling

Conversation

@Bccorb

@Bccorb Bccorb commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Closes #630.

Problem

Since 0.23.0, startWebhookDelivery and startAuditSinkDelivery each run a one second loop on every replica, even when no webhook endpoint or audit sink exists. Each idle webhook tick opens two transactions (advisory lock and cursor read, then a FOR UPDATE SKIP LOCKED claim). On a serverless Postgres this keeps pooled connections open and the database from scaling down. On our own Aurora Serverless v2 cluster with four tasks, connections went from 6 to 12 the moment the tasks moved to 0.23.0, and the database bill roughly doubled.

Change

  • src/lib/deliveryPoller.ts: a setTimeout loop that runs the next tick after 1 second when the last one reported work to watch for and after 30 seconds when it did not. Ticks never overlap, wake() runs one at once (or right after the one in flight), and a rejected tick counts as idle.
  • Webhooks: a tick first counts endpoints. With none it returns idle without opening a transaction. Deliveries cascade with their endpoint, so there is nothing to send or prune in that case. Pruning moves from every 3600th tick to once an hour of elapsed time.
  • Creating the first endpoint moves the dispatch cursor to the newest event, inside the create's transaction and under the dispatch advisory lock (blocking, not try). Without this, the cursor would lag while dispatch is idle and a new endpoint would receive events from before it existed, which breaks ADR 0005's "turning webhooks on does not replay history". Creating an endpoint, or re-enabling one, wakes the local loop.
  • Audit sinks: a tick reads enabled sinks and drains the due ones. It is busy while any sink is enabled (including one backing off, so retries keep their timing) and idle otherwise. Creating or updating a sink wakes the local loop, which matches updateSink's "tried at once" contract.
  • ADR 0005 and a patch changeset describe the new cadence.

Latency is unchanged once something is configured. Other replicas notice a new endpoint or sink within 30 seconds, and the replica that took the write starts at once.

Checks

  • npm run typecheck, npm run lint, npm run format:check, npm run build: pass
  • npm run coverage: 206 files, 3662 tests pass; lines 99.32%, branches 95.94%
  • New tests/unit/lib/deliveryPoller.spec.ts covers cadence, no overlap, wake while idle and while running, rejection and stop. Service tests cover the idle tick reading only a count, the first-endpoint cursor reset, the cursor left alone when an endpoint exists, a sink in backoff staying busy, hourly pruning, and wake on create.

Dependency fix

CI's production audit (npm audit --omit=dev --audit-level=high) started failing on new advisories for minimatch (GHSA-cf2h-pxjq-68xw and others, high) and glob (GHSA-qxr8-q827-836c, moderate), reached through sequelize-cli > js-beautify. This fails dependabot's #609 too. The second commit is a lockfile-only npm audit fix: minimatch 11.0.3 to 11.0.10, glob 14.0.3 to 14.0.7, @isaacs/nonrecursive-glob-match 1.0.3 to 1.1.0. Build, typecheck, lint and the full suite pass on it, and the audit reports 0 vulnerabilities.

…s configured

Webhook and audit sink delivery each ran a one second loop on every replica
since 0.23.0, even with no endpoint or sink, which kept pooled connections
open and a serverless Postgres from scaling down. Both loops now poll every
second only while an endpoint exists or a sink is enabled, and every 30
seconds otherwise with one read. Creating or enabling an endpoint or sink
wakes the local loop at once.

Dispatch no longer runs while no endpoint exists, so creating the first
endpoint moves the dispatch cursor to the newest event under the dispatch
lock, keeping the rule that turning webhooks on replays no history.

Closes #630
@Bccorb Bccorb added this to the v0.24 milestone Dec 2, 2026
@Bccorb
Bccorb merged commit 9d1ce04 into main Dec 2, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Webhook and audit sink delivery poll the database every second while unused

1 participant