Skip to content

fix(performance): fetch GitHub commits concurrently to prevent serverless timeouts - #3174

Open
nyxsky404 wants to merge 8 commits into
Priyanshu-byte-coder:mainfrom
nyxsky404:fix/github-fetch-timeout
Open

fix(performance): fetch GitHub commits concurrently to prevent serverless timeouts#3174
nyxsky404 wants to merge 8 commits into
Priyanshu-byte-coder:mainfrom
nyxsky404:fix/github-fetch-timeout

Conversation

@nyxsky404

Copy link
Copy Markdown
Contributor

This PR refactors the GitHub Search API pagination in fetchContributionsForAccount. Previously, the route fetched up to 10 pages sequentially in a while loop, which could take up to 20 seconds for highly active users and frequently caused 504 Gateway Timeouts on Vercel's edge/serverless architecture. The new implementation fetches the first page to determine the total_count, and then fetches the remaining required pages (up to 10 max) concurrently using Promise.all(). This drastically reduces the total execution time while correctly handling secondary rate limit (HTTP 429/403) fallbacks. Fixes #3170.

@github-actions github-actions Bot added gssoc26 GSSoC 2026 contribution type:bug GSSoC type bonus: bug fix type:performance GSSoC type bonus: performance (+15 pts) labels Jul 8, 2026
@github-actions

github-actions Bot commented Jul 8, 2026

Copy link
Copy Markdown

GSSoC Label Checklist 🏷️

@Priyanshu-byte-coder — please apply the appropriate labels before merging:

Difficulty (pick one):

  • level:beginner — 20 pts
  • level:intermediate — 35 pts
  • level:advanced — 55 pts
  • level:critical — 80 pts

Quality (optional):

  • quality:clean — ×1.2 multiplier
  • quality:exceptional — ×1.5 multiplier

Validation (required to score):

  • gssoc:approved — counts for points
  • gssoc:invalid / gssoc:spam / gssoc:ai-slop — does not score

Type labels (type:*) are auto-detected from files and title. Review and adjust if needed.
Points formula: (difficulty × quality_multiplier) + type_bonus

@github-actions github-actions Bot added the type:feature GSSoC type bonus: new feature label Jul 8, 2026
@Priyanshu-byte-coder

Copy link
Copy Markdown
Owner

Good instinct on the serverless timeout, but firing pages 2..N as fully-parallel Promise.all fans out up to ~9 concurrent GitHub Search calls on a single user token. GitHub's own guidance is explicit: "make requests for a single user serially… do not make them concurrently," and Search has a low secondary rate limit — a burst like this risks tripping a secondary-rate-limit block, which is worse than the sequential latency it replaces. Please cap concurrency to a small pool (2-3 at a time) instead of unbounded parallel, then I'll take another look. Also: rate-limited pages now silently drop their items — that's fine as partial results, just confirm that's intended.

…e limit

GitHub recommends against firing concurrent requests for a single user
token, since Search has a low secondary rate limit. The previous
implementation fired up to 9 pages via a single Promise.all, risking a
secondary-rate-limit block. Batch remaining pages through a bounded
pool of 3 concurrent requests instead, keeping the timeout mitigation
while staying within GitHub's guidance. Rate-limited pages continue to
contribute no items, falling back to whatever partial results were
already fetched, as intended.
@nyxsky404
nyxsky404 force-pushed the fix/github-fetch-timeout branch from 6fd7faf to d99c9f9 Compare July 20, 2026 14:50
@nyxsky404

nyxsky404 commented Jul 20, 2026

Copy link
Copy Markdown
Contributor Author

@Priyanshu-byte-coder

Updated:

  • Capped concurrency for pages 2..N to a bounded pool of 3 at a time (matches the same pattern used in prs/route.ts's CONCURRENCY_BATCH), instead of firing all remaining pages via a single Promise.all. This keeps us within GitHub's guidance against concurrent requests on a single token while still avoiding the sequential-latency timeout.
  • Confirmed: rate-limited pages intentionally return no items and the response falls back to whatever pages were already fetched, rather than failing the whole request. Added a comment in the code to make that explicit.
  • Rebased onto latest main.

Copilot AI review requested due to automatic review settings August 1, 2026 14:46

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors the GitHub commit Search API pagination in fetchContributionsForAccount to reduce latency and mitigate serverless/edge timeouts by fetching subsequent pages with bounded concurrency.

Changes:

  • Adds a bounded concurrency approach for fetching additional GitHub Search pages after the first page determines total_count.
  • Introduces PAGE_FETCH_CONCURRENCY to limit parallelism and reduce likelihood of triggering secondary rate limits.
  • Restructures pagination from a sequential loop to batched Promise.all() calls.
Suppressed comments (1)

src/app/api/metrics/contributions/route.ts:225

  • When a later page is rate-limited (or returns fewer than 100 items), the code currently keeps requesting subsequent batches. That can exacerbate secondary rate limits and adds unnecessary work; after a rate-limit response (or the first short page) you should stop issuing further page requests and just return the partial results collected so far.
        for (let i = 0; i < remainingPages.length; i += PAGE_FETCH_CONCURRENCY) {
          const batch = remainingPages.slice(i, i + PAGE_FETCH_CONCURRENCY);
          const batchResults = await Promise.all(batch.map((p) => fetchPage(p)));
          for (const res of batchResults) {
            // Rate-limited pages intentionally contribute no items; the

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/app/api/metrics/contributions/route.ts
@nyxsky404

Copy link
Copy Markdown
Contributor Author

@Priyanshu-byte-coder Please review, repo is already stared by me

@github-actions github-actions Bot added type:devops GSSoC type bonus: devops (+15 pts) type:testing GSSoC type bonus: tests (+10 pts) type:design GSSoC type bonus: UI/design (+10 pts) labels Aug 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gssoc26 GSSoC 2026 contribution type:bug GSSoC type bonus: bug fix type:design GSSoC type bonus: UI/design (+10 pts) type:devops GSSoC type bonus: devops (+15 pts) type:feature GSSoC type bonus: new feature type:performance GSSoC type bonus: performance (+15 pts) type:testing GSSoC type bonus: tests (+10 pts)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Severe API Timeout via Sequential O(N) GitHub Commits Fetching

3 participants