mirror of
https://github.com/block/buzz.git
synced 2026-08-18 06:50:31 +02:00
Signed-off-by: Tyler Longwell <tlongwell@block.xyz> Signed-off-by: npub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757 <5217c5c2f7bfb4333e46d17c98a9255a52dadee18dcd43a43536b95e6776dfa0@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67d <011987e296fd5006292d2f930b574be47c7801048d1983c46c425d3c95f0cffd@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Tyler Longwell <tlongwell@block.xyz> Co-authored-by: npub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757 <5217c5c2f7bfb4333e46d17c98a9255a52dadee18dcd43a43536b95e6776dfa0@sprout-oss.stage.blox.sqprod.co>
41 lines
1.8 KiB
PL/PgSQL
41 lines
1.8 KiB
PL/PgSQL
-- Refresh ephemeral-channel expiry in the transaction that makes a
|
|
-- channel-scoped event durable. Deferring to COMMIT closes the stale-prefetch
|
|
-- race: the UPDATE sees a TTL transition committed while ingest was in flight,
|
|
-- or waits on its row lock and rechecks after it commits, without restoring a
|
|
-- separate hot-path transaction.
|
|
CREATE FUNCTION refresh_channel_ttl_after_event_insert() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
-- Kind 9007 creates the channel and initializes its deadline itself.
|
|
IF NEW.channel_id IS NOT NULL AND NEW.kind <> 9007 THEN
|
|
BEGIN
|
|
-- Lock by identity before testing ttl_seconds. If a concurrent TTL
|
|
-- transition is uncommitted, this waits and follows its updated row
|
|
-- version instead of treating the old permanent version as final.
|
|
PERFORM 1 FROM channels
|
|
WHERE community_id = NEW.community_id AND id = NEW.channel_id
|
|
FOR UPDATE;
|
|
|
|
UPDATE channels
|
|
SET ttl_deadline = clock_timestamp() + make_interval(secs => ttl_seconds)
|
|
WHERE community_id = NEW.community_id
|
|
AND id = NEW.channel_id
|
|
AND ttl_seconds IS NOT NULL
|
|
AND archived_at IS NULL
|
|
AND deleted_at IS NULL;
|
|
EXCEPTION WHEN OTHERS THEN
|
|
-- Preserve the existing best-effort contract: a TTL refresh failure
|
|
-- must not reject an otherwise valid durable event.
|
|
RAISE WARNING 'channel TTL refresh failed for community %, channel %: %',
|
|
NEW.community_id, NEW.channel_id, SQLERRM;
|
|
END;
|
|
END IF;
|
|
RETURN NULL;
|
|
END
|
|
$$;
|
|
|
|
CREATE CONSTRAINT TRIGGER events_refresh_channel_ttl
|
|
AFTER INSERT ON events
|
|
DEFERRABLE INITIALLY DEFERRED
|
|
FOR EACH ROW EXECUTE FUNCTION refresh_channel_ttl_after_event_insert();
|