feat(seats): limited seats with race-proof holds (#91) #94

Merged
amiryahaya merged 28 commits from feat/91-limited-seats into main 2026-10-25 13:37:39 +01:00
Owner

Closes #91. Limited-seat products can never oversell: the database enforces the cap (counters change only via an append-only ledger trigger), same approach as the wallet ledger (ADR 0009).

What's in it

  • Migrations 0014/0015: seat pools, capacity rules, per-day counters, holds, ledger, drift and stuck-hold views
  • internal/seats (+ pgstore): Hold, Take, Confirm, Release, Return; idempotent keys, one 3 s lock budget, ascending lock order, lazy expiry of expired holds
  • Settings seat_hold_minutes (10), seat_pending_alert_minutes (30)
  • API: public GET /v1/seats/availability; admin seat pools, flag, capacity rules, GET /v1/admin/seats/stuck-holds; OpenAPI + live contract test updated
  • Docs: ADR 0009, spec rule R-SEAT-1, "Seats" section in CLAUDE.md

Verification

  • make cover: 94.3% total (minimum 80%); seats 100%, seats/pgstore 91.6%
  • go test ./... -race, make lint, make contract: clean
  • Dual-pass review (ponytail + correctness): Must-fix clear
  • 1,000 concurrent buyers on a cap of 100: exactly 100 succeed; opposite-order multi-day stays do not deadlock

Needs owner attention

  • 700 holds/s not proven: dev host median ~892 (16 runs, 492-1,075); re-measure on production-like hardware before launch.
  • Unflag/regroup rule as built (refused only for rules ending today or later, or held seats) differs from the spec text; spec line marked "as built, owner to confirm".
  • Worker sweep #29 is a hard prerequisite for #26 going live: until then "N left" under-counts abandoned holds.
  • #26 follow-ups: resolve product -> pool inside the sale tx, map ErrBusy to 503, READ COMMITTED only.
  • Parked minors: raw 500 instead of ErrBusy when under 1 ms of lock budget remains; merged-quantity int overflow on absurd internal inputs.

🤖 Generated with Claude Code

Closes #91. Limited-seat products can never oversell: the database enforces the cap (counters change only via an append-only ledger trigger), same approach as the wallet ledger (ADR 0009). ## What's in it - Migrations 0014/0015: seat pools, capacity rules, per-day counters, holds, ledger, drift and stuck-hold views - `internal/seats` (+ `pgstore`): Hold, Take, Confirm, Release, Return; idempotent keys, one 3 s lock budget, ascending lock order, lazy expiry of expired holds - Settings `seat_hold_minutes` (10), `seat_pending_alert_minutes` (30) - API: public `GET /v1/seats/availability`; admin seat pools, flag, capacity rules, `GET /v1/admin/seats/stuck-holds`; OpenAPI + live contract test updated - Docs: ADR 0009, spec rule R-SEAT-1, "Seats" section in CLAUDE.md ## Verification - `make cover`: **94.3%** total (minimum 80%); seats 100%, seats/pgstore 91.6% - `go test ./... -race`, `make lint`, `make contract`: clean - Dual-pass review (ponytail + correctness): Must-fix clear - 1,000 concurrent buyers on a cap of 100: exactly 100 succeed; opposite-order multi-day stays do not deadlock ## Needs owner attention - **700 holds/s not proven**: dev host median ~892 (16 runs, 492-1,075); re-measure on production-like hardware before launch. - **Unflag/regroup rule as built** (refused only for rules ending today or later, or held seats) differs from the spec text; spec line marked "as built, owner to confirm". - **Worker sweep #29 is a hard prerequisite for #26 going live**: until then "N left" under-counts abandoned holds. - #26 follow-ups: resolve product -> pool inside the sale tx, map `ErrBusy` to 503, READ COMMITTED only. - Parked minors: raw 500 instead of `ErrBusy` when under 1 ms of lock budget remains; merged-quantity int overflow on absurd internal inputs. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
seat_holds (held -> confirmed|released, final states, pending flag only
while held), the append-only seat_ledger whose BEFORE INSERT trigger locks
the seat_days row and moves held/sold (seat_sold_out, seat_over_release),
and the seat_counter_drift and seat_pending_holds views.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Drop the seat_ledger -> seat_days foreign key; the ledger trigger refuses a
missing day as seat_not_on_sale. A hold entry on a closed hold, or a confirm
on a released one, is seat_hold_final. Freeze a hold's expiry, index
seat_ledger(hold_id), rename seat_pending_holds.seats to seat_days.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
pgstore.Store implements seats.Reserver inside the caller's transaction:
Hold (replay by key, ErrKeyReused), Take, Confirm, Release, Return,
MarkPaymentPending and lazy expiry of expired, non-pending holds on a
sold-out day. Every call runs in a savepoint with lock_timeout 3s,
restored after; ledger rows go in ascending (pool, day) order.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Every statement that can wait on a lock sets lock_timeout to what is
left of the call's 3 s budget, batched with it. The pool FOR SHARE lock
is taken once in hold(), before the first attempt, and kept through lazy
expiry and the retry. Replaying a released hold is ErrHoldState.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Seat pool CRUD and PUT /v1/admin/park-products/{id}/seat-pool under
admin_portal.administration.manage. Leaving a pool locks it FOR UPDATE
and is refused while it has rules ending today or later or held seats;
a hold on a pool with no limited park product is ErrNotOnSale, so a
hold and an unflag never both succeed.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Capacity rule CRUD under /v1/admin/seat-pools/{id}/capacity-rules and
/v1/admin/seat-capacity-rules/{id}, and GET /v1/admin/seats/stuck-holds
(threshold from seat_pending_alert_minutes, 30 by default, or
?older_than_minutes=), all under admin_portal.administration.manage.
Overlap, removing a held or sold day, and lowering below held + sold
are 409; bad dates, spans over 366 days and capacities outside
1..1,000,000 are 422. Docs: R-SEAT-1, CLAUDE.md Seats section, ADR 0009
capacity rules section, spec Flag rule as built.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
btx, callerTimeout, lockExec/lockRow, bounded and mapErr move unchanged.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
- dateRange (availability and capacity rules) refuses year 0, which
  time.Parse accepts and PostgreSQL rejects (22008, a 500 before).
- validate bounds each merged (pool, day) total at 10,000, not only
  each line.
- Drop the unused seats.Reader interface and PoolsFor, and the store's
  duplicate range check with its 422 hook; the handler's dateRange is
  the one check.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
- seat_hold_outstanding: a hold moves to confirmed or released only when
  hold - confirm - release is 0 for it.
- seat_pool_has_ledger: a BEFORE DELETE trigger refuses deleting a pool
  that has seat_ledger rows (no foreign key, no lock on ledger inserts).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Unflag, regroup, and shortening, lowering or deleting a capacity rule
release the expired holds with no payment pending on the pool or the
rule's days first, in the same transaction and with the lazy-expiry
code (holds SKIP LOCKED, then days ascending), then check. A hold with a
payment pending, or not yet expired, still blocks. inTx and ruleTx are
one inTx.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
tryHold manages its savepoint by hand: it is created in the hold insert's
round trip and released with the call's savepoint. Hold's last ledger
insert also restores lock_timeout and releases the call's savepoint in
the same pipeline, so a single-line hold makes one round trip under the
day lock before the commit (three before). A failed statement stops the
pipeline, so the error path is unchanged. Lock order and the one 3 s
budget are unchanged. ledgerLines is inlined into Return.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
style(db): gofmt seat ledger test (#91)
Some checks failed
ci / changes (pull_request) Successful in 11s
ci / clients (pull_request) Successful in 2m25s
ci / api (pull_request) Failing after 16m31s
ci / ci-gate (pull_request) Failing after 14s
06464d8cb4
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
test(seats): fewer connections per node in the two-node hold test (#91)
All checks were successful
ci / changes (pull_request) Successful in 32s
ci / clients (pull_request) Successful in 2m39s
ci / api (pull_request) Successful in 23m18s
ci / ci-gate (pull_request) Successful in 2s
02152c3a8a
CI ran 20 queued commits against the 3 s lock budget on a slow disk and three holds got ErrBusy. No oversell: 100 held.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
primatekuntech/kws!94
No description provided.