Skip to content

[COMMEX-2324] Once-only helper with SQL store - #1543

Open
jolinger-godaddy wants to merge 1 commit into
godaddy:mainfrom
jolinger-godaddy:COMMEX-2324-once-only
Open

jolinger-godaddy wants to merge 1 commit into
godaddy:mainfrom
jolinger-godaddy:COMMEX-2324-once-only

Conversation

@jolinger-godaddy

Copy link
Copy Markdown

Jira

https://godaddy-corp.atlassian.net/browse/COMMEX-2324

This adds a once-only helper to app-connect, so an app handling platform events or vendor webhooks doesn't need its own ledger to avoid repeating a side effect on redelivery. It's the claim and lease logic the Stripe sync had in its MySQL ledger, made generic.

How it works:

  • once(store, key, fn, { leaseMs }) claims the key, runs fn, and records the outcome. It answers done (it ran now), duplicate (it already finished, with that outcome), in_flight (another instance holds it, retry later) or refused (fn threw Refused, recorded so it's never retried). Any other error releases the claim and is rethrown, so a retry runs the work again. onReleaseError hears about a release that failed after that. A store answering a claim status once doesn't know never gets the work run.
  • A claim whose lease has passed is taken over, so work a crashed instance held is delayed, not lost. That also means work that outlives its lease can run twice, so it should still send an idempotency key. Each claim carries a token, so only the caller holding it can record or release it. If the work finished but the outcome couldn't be recorded, once throws OnceNotRecordedError (with the work's return value, kept out of serialized logs) and the delivery should be answered as failed.
  • OnceStore is the port. createMysqlOnceStore takes a mysql2/promise pool on MySQL 8 or later. Give it a pool, not a connection you use for transactions, since its writes have to commit on their own. It creates its table on first use, or you can run onceTableDdl() from a migration and use a user that can't create tables.
  • The MySQL store copes with lock contention: a claim retries a deadlock and answers in_flight for a lock wait, complete and release retry a deadlock, a claim that waited on a lock gets its lease renewed so it isn't short, and a key released mid-claim is claimed rather than reported busy. Keys use a binary, no-pad collation, so keys that differ only in case, accents or trailing spaces stay apart. Rows hold the key, claim and outcome only; they're kept, and the README has a batched prune query with a note on how long to keep them. createMemoryOnceStore is for tests and local runs.
  • deriveId(namespace, name) builds RFC 9562 version 5 ids for stable keys and vendor idempotency keys. Both it and once reject strings that aren't well-formed UTF-16.
  • Exported from the package root and from @godaddy/app-connect/once. The once subpath has no runtime dependencies beyond node:crypto.

Testing: one store contract suite runs against the memory store, a statement-level MySQL fake, and a real MySQL 8.4 server. The MySQL run is skipped unless APP_CONNECT_TEST_MYSQL_URL is set, so CI doesn't run it; I ran it locally against mysql:8.4 in Docker. It checks that only one of twelve concurrent claims wins, that claims racing a release never throw, the binary collation, and a reserved-word table name. The fake covers the lock-conflict, deadlock-retry and lease-renewal paths. deriveId is checked against the RFC 9562 example. Package tests, typecheck, biome check and build pass. mysql2 is a dev dependency only.

No dependencies. It's independent of the webhook verification PR (COMMEX-2359); the event handler builds on both.

once(store, key, fn, { leaseMs }) records one outcome per key and runs the
work one caller at a time, with the outcomes done, duplicate, in_flight and
refused. Claims live in an OnceStore: a MySQL 8 store over a mysql2/promise
pool (with its DDL) and an in-memory store for tests. A claim whose lease
passes is taken over; each claim carries a token so only its holder records
the outcome. deriveId builds RFC 9562 version 5 ids.
@jolinger-godaddy
jolinger-godaddy requested a review from a team as a code owner November 23, 2026 22:16
@changeset-bot

changeset-bot Bot commented Nov 23, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: b5c513d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@godaddy/app-connect Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

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.

1 participant

⌂ Proxy