Skip to content

fix(api): in-flight request coalescing for /chart/:mint [BUG-107] - #223

Open
Morenikeoa wants to merge 1 commit into
dcccrypto:mainfrom
Morenikeoa:fix/chart-inflight-coalescing
Open

fix(api): in-flight request coalescing for /chart/:mint [BUG-107]#223
Morenikeoa wants to merge 1 commit into
dcccrypto:mainfrom
Morenikeoa:fix/chart-inflight-coalescing

Conversation

@Morenikeoa

Copy link
Copy Markdown

Problem

/chart/:mint keeps its own separate cache Map, distinct from cache.ts/db-cache-fallback.ts (already fixed for in-flight de-dup) and from adl.ts's adlCache (issue #193). On a cache miss, the handler makes two sequential awaited fetch() calls to GeckoTerminal — getTopPool(mint) then fetchOhlcv(...) — with no in-flight de-duplication. Concurrent requests for the same mint:timeframe:aggregate:limit key all miss together and each independently fire both upstream calls.

Impact

GeckoTerminal is a third-party API consumed without an API key and rate-limited on egress, shared across this entire process for every mint. Duplicate concurrent calls for the same key risk exhausting that shared budget, which can degrade /chart for every mint being charted, not just the one whose concurrent requests caused the spike.

This is distinct from open issue #213, which is about the limit query param being included in the cache key (causing many distinct keys for the same mint) — a different root cause than this one (no coalescing within a single key).

Fix

Extracted the miss-path into fetchAndCache() and added an inflight map, mirroring the proven coalescing pattern already used in oracle-router.ts in this same repo: only the first concurrent caller for a given key actually calls fetchAndCache(); concurrent callers await the same promise instead of starting their own. The cache write happens once per miss-episode regardless of how many concurrent requests triggered it.

Proof of Fix

New test: two concurrent requests for the same key result in exactly one pool-resolution fetch and one OHLCV fetch (not two of each), and both requests receive the same candle data.

Verified this is a genuine regression test: reverted just the source change and reran — failed with 2 pool-resolution calls instead of 1. Restored the fix and it passes.

  • All existing tests pass — output attached.
  • New regression test passes against the fix, fails against pre-fix code (verified locally).
  • tsc --noEmit clean (no separate lint script in this repo).

Test Output

✓ tests/routes/chart.test.ts (12 tests) 62ms

Full suite: 295/296 passed (294 baseline + 1 new). The 1 failure (tests/sdk-smoke.test.ts) is pre-existing and unrelated — it asserts on an exact @percolatorct/sdk error-message string that has drifted from the locally-resolved SDK version in this environment.

Related

Found during a broader API audit. Distinct from open issue #213 (cache-key/limit-param fragmentation — a different root cause). No existing open issue/PR covers this specific coalescing gap.

/chart/:mint's own cache Map had no in-flight de-dup, distinct from the
already-fixed cache.ts/db-cache-fallback.ts coalescing (different cache
entirely) and from issue dcccrypto#213 (which is about the limit param polluting
the cache key — a different mechanism). On a miss, this route makes two
sequential awaited fetch() calls to GeckoTerminal (getTopPool then
fetchOhlcv) with no de-dup — concurrent requests for the same
mint/timeframe/aggregate/limit key all miss together and each
independently fire both upstream calls. GeckoTerminal is a third-party
API with rate limits shared across this whole process, so duplicate
concurrent calls risk exhausting that budget and degrading /chart for
every mint, not just the one being requested.

Extracted the miss-path into fetchAndCache() and added an `inflight` Map
mirroring the proven coalescing pattern already in oracle-router.ts: only
the first concurrent caller for a key runs fetchAndCache(); the rest await
the same promise and the cache write happens once per miss-episode.

Added a regression test: two concurrent requests for the same key result
in exactly one pool-resolution call and one OHLCV call (not two of each),
and both get the same candle data. Verified it fails against the pre-fix
code (2 pool calls instead of 1) and passes against the fix.

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
@vercel

vercel Bot commented Jun 26, 2026

Copy link
Copy Markdown

@Princessdada is attempting to deploy a commit to the Khubair Nasir's projects Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Jun 26, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@Morenikeoa, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 46 minutes and 24 seconds. Learn how PR review limits work.

Your organization has used up its prepaid credits, and credit purchases are no longer available. Enable the review add-on in the billing tab to keep reviews running — you're only billed for reviews past your plan's rate limits ($0.25/file).

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based credits.

🚦 How do rate limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: a71d595f-e7f6-4fef-9da0-049a9735b1b5

📥 Commits

Reviewing files that changed from the base of the PR and between b2751f4 and f8a0866.

📒 Files selected for processing (2)
  • src/routes/chart.ts
  • tests/routes/chart.test.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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