Repository navigation
fix(idempotency): shared bounded store + in-flight coalescing - #234
Merged
Jagadeeshftw merged 1 commit intoAug 29, 2026
Merged
Conversation
Close the per-middleware Map hole that let the same key execute twice across mounts, add a hard entry cap with soonest-expiry eviction, and coalesce concurrent same-key requests onto one in-flight handler. Replay semantics stay response-body based; headers are never cached. Cross-replica sharing waits on the separate persistence issue.
nyuiela
force-pushed
the
fix/221-idempotency-shared-store
branch
from
August 21, 2026 09:28
3cf7a91 to
a2570ac
Compare
7 tasks
Jagadeeshftw
added a commit
that referenced
this pull request
Aug 29, 2026
… (#231) * feat(config): fail-fast validation + standalone typecheck script (#230) * fix(#225): replace float arithmetic with bigint for exact monetary precision (#236) * fix(#225): replace float arithmetic with bigint for exact monetary precision * fix(#225): add all bigint migration files missing from previous commit * feat(metrics): protect metrics reads and bound history [Issue #228] (#235) Aggregate metrics (participant counts, liquidity totals, settlement volume and fees over time) describe the network's operational state. Exposing that publicly should be deliberate, not a side effect of the write-only auth middleware, whose MUTATING_METHODS set left every GET unauthenticated and unlimited. - Auth: new metricsAuth guards GET /api/v1/metrics and /history. When API_KEY or the new read-only METRICS_API_KEY is set, reads require a matching x-api-key (401 otherwise); when neither is set they stay open, matching the existing write-auth model. METRICS_API_KEY unlocks metrics only, so a scraper never needs the write key. - Rate limiting: opt-in limitReads flag on rateLimiter (default off, so global behaviour is unchanged) enabled only on the metrics mount via METRICS_RATE_LIMIT_MAX (default 120/min), so the history endpoint is not an unlimited load generator. Global read limiting and the shared store remain owned by the separate rate-limiter issue. - Retention: history stays bounded at MAX_HISTORY = 50, now pinned by route-level tests (eviction of the oldest entry). - openapi.ts declares an ApiKeyAuth scheme and marks both metrics operations as protected; README/CHANGELOG document the scraper path. npm run lint, npm run build and npm test (43 suites, 509 tests) pass. Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com> * fix(idempotency): share a bounded in-process replay store (#234) Close the per-middleware Map hole that let the same key execute twice across mounts, add a hard entry cap with soonest-expiry eviction, and coalesce concurrent same-key requests onto one in-flight handler. Replay semantics stay response-body based; headers are never cached. Cross-replica sharing waits on the separate persistence issue. * fix: make audit log an explicit operator convenience buffer (#233) * feat: implement memory-bounded bucket eviction for rate limiter and add operational documentation (#232) * Add fail-fast config validation and standalone typecheck (#230) (#237) - validateConfig() enforces required values before the server binds: API_KEY required in production, PORT valid 1-65535, non-negative rate-limit/idempotency values; warns loudly (non-prod) on open access. - Wire validateConfig into createApp/getConfig in app.ts. - Add typecheck script (tsc --noEmit) and a distinct CI Typecheck step. - Extend config.test.ts with a validateConfig suite. closes #230 Co-authored-by: ChainBid Developer <developer@chain-bid.io> --------- Co-authored-by: Paranoa-dev <paranoa-dev@users.noreply.github.com> Co-authored-by: Mauricio Gil | GramSeo Studio <gramseostudio@gmail.com> Co-authored-by: jahswillb-dev <tech.jahswillb@gmail.com> Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com> Co-authored-by: kaleel <128490484+nyuiela@users.noreply.github.com> Co-authored-by: martinshub-tech <jambemail2003@gmail.com> Co-authored-by: Oyakhilome Gift A <asekhamegift@gmail.com> Co-authored-by: ChainBid Developer <developer@chain-bid.io> Co-authored-by: Jagadeeshftw <92681651+Jagadeeshftw@users.noreply.github.com>
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 #221.
Replaces the per-middleware closed-over
Mapwith a process-wideMemoryIdempotencyStorethat is shared across mounts, hard-capped, and coalesces concurrent same-key requests onto a single handler execution.Design decisions
Where the state belongs. Process-wide in-memory store shared by every
idempotency()mount that does not inject its ownstore. This service still has no persistence layer (compression/cors/expressonly), so bolting on Redis here would invent a second stack ahead of the tracked persistence issue. Cross-replica duplicates remain a known limit until that store lands; this PR closes the within-process holes (cross-mount + concurrent) without an external dependency. Sequencing: swapIdempotencyStorefor a Redis/DB implementation once persistence exists — the middleware options already accept an injectablestore.What a replay returns. Keep stored-response replay (status + JSON body). Clients of settlement APIs expect a safe retry to return the original outcome, not a conflict that forces a separate GET. Returning 409/422 on every duplicate would break the existing contract and the curl walkthrough in the README.
Eviction and bounds. Default TTL unchanged (
IDEMPOTENCY_TTL_MS/ 24h). Hard cap default 1024 entries: purge expired on write, then drop soonest-to-expire live entries until under the cap.MemoryIdempotencyStore(maxEntries)is injectable for tighter bounds in tests.Sensitive header audit
Cached entries store only
{ status, body, expiresAt, bodyHash }. Response headers (Set-Cookie,Authorization, custom secrets) are never written into the cache. Covered by a regression test.Public options
IdempotencyOptionsstays backward compatible:ttlMsunchanged. Additive optional fields:store,maxEntries(used when constructing a private store in tests; production continues to use the process-wide default).Test plan
3 * maxEntriesdistinct keys →store.size() <= maxEntriesnpm run lint && npm run build && npm test(42 suites / 493 tests)