Repository navigation
feat(swaps): idempotency-key support with concurrency-safe replay - #568
Closed
Simultech369 wants to merge 1 commit into
Closed
Simultech369 wants to merge 1 commit into
Simultech369 wants to merge 1 commit into
Conversation
Contributor
|
Closing as part of a repository reset; new issues will be opened shortly. Thanks for contributing! |
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.
Summary
Closes #548
This PR implements concurrency-safe idempotency support for the swap endpoint (
POST /api/v1/swaps) to prevent duplicate swap execution and balance debits under client retries and at-least-once delivery semantics.The implementation follows industry best practices (Stripe/PayPal style) using an atomic in-memory lock-and-replay engine, cryptographic payload fingerprinting, multi-tenant scoping, and structured error responses.
Architecture & Implementation
1. Atomic Idempotency Engine (
src/idempotency/)IdempotencyStoreInterface (src/idempotency/types.ts): Decoupled interface supporting drop-in distributed backends (e.g. Redis) in the future.InMemoryIdempotencyStore(src/idempotency/store.ts):acquire(lock-if-absent) rather than read-then-write."in_progress"during execution and"completed"upon response generation.IDEMPOTENCY_TTL_MS).IDEMPOTENCY_CACHE_MAX).release) for graceful error handling during unhandled exceptions.src/idempotency/fingerprint.ts):METHOD,PATH,tenantId, and recursively sorted canonical JSON body keys.src/idempotency/middleware.ts):X-Tenant-Id,Authorization: Bearer <key>, orX-API-Keyheaders.Idempotency-Key(1–200 printable ASCII characters; rejects whitespace/control chars with400 invalid_request).409 request_in_progressif a concurrent request with the same key is underway.409 idempotency_conflictif a key is reused with a divergent payload.2. Swap Execution & Endpoints (
src/swaps/&src/index.ts)POST /api/v1/swaps:pauseGuard(rejects with 503 during pause/read-only mode prior to lock acquisition).201 Createdwith the executedSwapRecord.GET /api/v1/swaps&GET /api/v1/swaps/:id: Query historical swaps with optional asset filters.request_in_progress(HTTP 409) added toApiErrorCodeandAPI_ERROR_DEFINITIONS./api/v1/swapsand/api/v1/swaps/{id}insrc/openapi.ts.docs/swaps-idempotency.mdand updateddocs/idempotency.md.Edge Case Coverage
Every edge case specified in Issue #548 is implemented and tested:
409 Conflict(idempotency_conflict).409 Conflict(request_in_progress).400 Bad Request(invalid_request).Verification & Test Results
src/__tests__/idempotencyStore.unit.test.ts(9/9 pass)src/__tests__/swapsIdempotency.test.ts(20/20 pass)src/__tests__/openapi.test.ts(route-drift validation pass)npm test: 37/37 test suites passed, 1014/1014 tests passednpm run lint: 0 errorsnpm run build:tsccompiles clean with 0 errors underexactOptionalPropertyTypes: true