Skip to content

An open-source, faster Devin alternative

TypeScript

perf(webhooks): gate event recording on destination existence (true dormancy)

Igor Zalutskia3f55291c239
diff --git a/internal/db/migrations/049_sandbox_webhooks.up.sql b/internal/db/migrations/049_sandbox_webhooks.up.sql
index a3495b87..fc3574d2 100644
--- a/internal/db/migrations/049_sandbox_webhooks.up.sql
+++ b/internal/db/migrations/049_sandbox_webhooks.up.sql
@@ -25,6 +25,11 @@ CREATE INDEX IF NOT EXISTS webhook_dest_org_idx
ON webhook_destinations (org_id) WHERE enabled AND deleted_at IS NULL;
CREATE UNIQUE INDEX IF NOT EXISTS webhook_dest_name_uq
ON webhook_destinations (org_id, name) WHERE name IS NOT NULL AND deleted_at IS NULL;
+-- The dormancy gate: "does this org have ANY live destination (enabled OR paused)
+-- that could receive this event?" — leading org_id, sandbox_id for the scope test.
+-- (webhook_dest_org_idx above is enabled-only, for the dispatcher's claim.)
+CREATE INDEX IF NOT EXISTS webhook_dest_org_live_idx
+ ON webhook_destinations (org_id, sandbox_id) WHERE deleted_at IS NULL;
-- Canonical lifecycle event store — the merge point for both origins. The single
-- recordLifecycleEvent primitive inserts here (CP in-tx; worker via the stream
diff --git a/internal/db/webhooks.go b/internal/db/webhooks.go
index 4bae380a..dbee3f07 100644
--- a/internal/db/webhooks.go
+++ b/internal/db/webhooks.go
@@ -56,6 +56,23 @@ type LifecycleEvent struct {
// exactly one place. Both origins call it (CP in-tx; worker via the ingress's
// own tx through RecordLifecycleEvent).
func recordLifecycleEvent(ctx context.Context, tx pgx.Tx, ev LifecycleEvent) error {
+ // Dormancy gate: only record an event if the org has a live destination that
+ // could receive it (org-wide, or scoped to this sandbox). A destination's
+ // watermark only ever delivers events created AFTER it, so skipping events
+ // for an org with no matching destination can never cause a miss — and it
+ // keeps sandbox_lifecycle_events empty for the (vast majority of) orgs not
+ // using webhooks. Paused (disabled) destinations count, since pause queues.
+ var relevant bool
+ if err := tx.QueryRow(ctx,
+ `SELECT EXISTS(SELECT 1 FROM webhook_destinations
+ WHERE org_id = $1 AND deleted_at IS NULL
+ AND (sandbox_id IS NULL OR sandbox_id = $2))`,
+ ev.OrgID, ev.SandboxID).Scan(&relevant); err != nil {
+ return fmt.Errorf("webhook destination check: %w", err)
+ }
+ if !relevant {
+ return nil // dormant: nothing could receive this event
+ }
if len(ev.Data) == 0 {
ev.Data = json.RawMessage("{}")
}