Repository navigation
[COMMEX-2324] Once-only helper with SQL store - #1543
Open
jolinger-godaddy wants to merge 1 commit into
Open
jolinger-godaddy wants to merge 1 commit into
jolinger-godaddy wants to merge 1 commit into
Conversation
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.
🦋 Changeset detectedLatest commit: b5c513d The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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, runsfn, and records the outcome. It answersdone(it ran now),duplicate(it already finished, with that outcome),in_flight(another instance holds it, retry later) orrefused(fnthrewRefused, recorded so it's never retried). Any other error releases the claim and is rethrown, so a retry runs the work again.onReleaseErrorhears about a release that failed after that. A store answering a claim statusoncedoesn't know never gets the work run.oncethrowsOnceNotRecordedError(with the work's return value, kept out of serialized logs) and the delivery should be answered as failed.OnceStoreis the port.createMysqlOnceStoretakes amysql2/promisepool 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 runonceTableDdl()from a migration and use a user that can't create tables.in_flightfor a lock wait,completeandreleaseretry 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.createMemoryOnceStoreis for tests and local runs.deriveId(namespace, name)builds RFC 9562 version 5 ids for stable keys and vendor idempotency keys. Both it andoncereject strings that aren't well-formed UTF-16.@godaddy/app-connect/once. Theoncesubpath has no runtime dependencies beyondnode: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_URLis set, so CI doesn't run it; I ran it locally againstmysql:8.4in 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.deriveIdis checked against the RFC 9562 example. Package tests, typecheck, biome check and build pass.mysql2is a dev dependency only.No dependencies. It's independent of the webhook verification PR (COMMEX-2359); the event handler builds on both.