⚡ Prevent cache stampede in OG image fetcher#480
Conversation
Co-authored-by: is0692vs <135803462+is0692vs@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
|
The latest updates on your projects. Learn more about Vercel for GitHub. 1 Skipped Deployment
|
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
|
Warning Review limit reached
Next review available in: 54 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the 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 reviews. How do review 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 refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (1)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Code Review
This pull request introduces an in-flight requests cache (inFlightRequests) to prevent cache stampedes (thundering herd) when fetching GitHub profiles. The feedback suggests defining a strongly-typed GitHubProfile interface instead of using any with an ESLint disable comment. Additionally, it recommends removing the redundant .catch(() => {}) on the detached promise chain (since errors are already caught and logged when awaiting the promise) and removing the unnecessary async keyword from the .then callback.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| // In-flight requests cache to prevent cache stampedes (thundering herd) | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| const inFlightRequests = new Map<string, Promise<any>>(); |
There was a problem hiding this comment.
Instead of using any and disabling the ESLint rule, we should define a strongly-typed interface for the GitHub profile data. This improves type safety, readability, and eliminates the need for the eslint-disable-next-line comment.
interface GitHubProfile {
name?: string | null;
bio?: string | null;
avatar_url?: string | null;
followers?: number;
public_repos?: number;
}
// In-flight requests cache to prevent cache stampedes (thundering herd)
const inFlightRequests = new Map<string, Promise<GitHubProfile>>();
| }).then(async (res) => { | ||
| if (!res.ok) { | ||
| throw new Error(`GitHub API responded with ${res.status}`); | ||
| } | ||
| return res.json(); | ||
| }); | ||
| inFlightRequests.set(username, dataPromise); | ||
| dataPromise.catch(() => {}).finally(() => { | ||
| inFlightRequests.delete(username); | ||
| }); |
There was a problem hiding this comment.
The .catch(() => {}) block silently suppresses promise rejections on this detached promise chain. Since dataPromise is always awaited within the main try-catch block (where any errors are properly caught and logged), this .catch(() => {}) is redundant. Removing it avoids silent suppression and adheres to the repository's general rules. Additionally, we can remove the unnecessary async keyword from the .then callback and cast the JSON response to our strongly-typed GitHubProfile interface.
| }).then(async (res) => { | |
| if (!res.ok) { | |
| throw new Error(`GitHub API responded with ${res.status}`); | |
| } | |
| return res.json(); | |
| }); | |
| inFlightRequests.set(username, dataPromise); | |
| dataPromise.catch(() => {}).finally(() => { | |
| inFlightRequests.delete(username); | |
| }); | |
| }).then((res) => { | |
| if (!res.ok) { | |
| throw new Error(`GitHub API responded with ${res.status}`); | |
| } | |
| return res.json() as Promise<GitHubProfile>; | |
| }); | |
| inFlightRequests.set(username, dataPromise); | |
| dataPromise.finally(() => { | |
| inFlightRequests.delete(username); | |
| }); |
References
- When suppressing unhandled promise rejections, log the error instead of silently ignoring it to facilitate debugging.
| dataPromise = fetch(`https://api.github.com/users/${encodeURIComponent(username)}`, { | ||
| headers: { | ||
| Accept: "application/vnd.github.v3+json", | ||
| "User-Agent": "github-user-summary", | ||
| }, | ||
| next: { revalidate: ONE_DAY_IN_SECONDS }, | ||
| }).then(async (res) => { | ||
| if (!res.ok) { | ||
| throw new Error(`GitHub API responded with ${res.status}`); | ||
| } | ||
| return res.json(); | ||
| }); |
There was a problem hiding this comment.
同じ username への同時リクエスト中に、先頭の GitHub fetch が一時的な 5xx/403 や JSON パース失敗になると、全 waiter が同じ rejection を受けてデフォルト値の OG 画像を返します。以前は各リクエストが独立していたため成功するリクエストが残る余地がありましたが、この変更後は 1 回の上流失敗で同時リクエスト全体が空の画像に寄り、成功時と同じ長いキャッシュ設定で配信されます。
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/app/api/og/[username]/route.tsx
Line: 54-65
Comment:
**共有 Promise の失敗伝播**
同じ username への同時リクエスト中に、先頭の GitHub fetch が一時的な 5xx/403 や JSON パース失敗になると、全 waiter が同じ rejection を受けてデフォルト値の OG 画像を返します。以前は各リクエストが独立していたため成功するリクエストが残る余地がありましたが、この変更後は 1 回の上流失敗で同時リクエスト全体が空の画像に寄り、成功時と同じ長いキャッシュ設定で配信されます。
How can I resolve this? If you propose a fix, please make it concise.| next: { revalidate: ONE_DAY_IN_SECONDS }, | ||
| }).then(async (res) => { | ||
| if (!res.ok) { | ||
| throw new Error(`GitHub API responded with ${res.status}`); |
There was a problem hiding this comment.
!res.ok を例外にしたため、存在しないユーザーの 404 や GitHub の通常の rate limit 403 でも catch 側の logger.error が走るようになります。以前は非 2xx では静かにデフォルト表示へフォールバックしていたので、typo やスキャン、rate limit のような通常運用の入力で error ログが増え、監視ノイズになります。
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/app/api/og/[username]/route.tsx
Line: 62
Comment:
**想定内レスポンスのエラーログ化**
`!res.ok` を例外にしたため、存在しないユーザーの 404 や GitHub の通常の rate limit 403 でも `catch` 側の `logger.error` が走るようになります。以前は非 2xx では静かにデフォルト表示へフォールバックしていたので、typo やスキャン、rate limit のような通常運用の入力で error ログが増え、監視ノイズになります。
How can I resolve this? If you propose a fix, please make it concise.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
|
Superseded by test-backed 5/5 PR #495 for the same OG request deduplication. |
💡 What: Added an in-flight request cache (using a
Mapof promises) to the OG image API route.🎯 Why: To prevent a cache stampede (thundering herd) where multiple concurrent requests for the same user profile all result in a call to the GitHub API, which could easily lead to rate limiting.
📊 Measured Improvement: In a local benchmark of 50 concurrent requests for the same user, the fetch count was reduced from 50 to 1, and the response time dropped from 398ms to 136ms.
PR created automatically by Jules for task 2419461658988423283 started by @is0692vs
Greptile Summary
この PR は OG 画像 API の GitHub profile 取得を同時リクエスト間で共有します。
Confidence Score: 4/5
同時リクエスト中の共有 fetch 失敗が、全リクエストの fallback 画像に広がります。
src/app/api/og/[username]/route.tsx
Important Files Changed
Sequence Diagram
%%{init: {'theme': 'neutral'}}%% sequenceDiagram participant R1 as Request 1 participant R2 as Request 2 participant M as inFlightRequests participant GH as GitHub API R1->>M: get(username) miss R1->>GH: fetch profile R1->>M: set(username, dataPromise) R2->>M: get(username) hit GH-->>M: non-ok or parse failure M-->>R1: shared promise rejects M-->>R2: shared promise rejects R1-->>R1: fallback OG image R2-->>R2: fallback OG image M->>M: delete(username)%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%% sequenceDiagram participant R1 as Request 1 participant R2 as Request 2 participant M as inFlightRequests participant GH as GitHub API R1->>M: get(username) miss R1->>GH: fetch profile R1->>M: set(username, dataPromise) R2->>M: get(username) hit GH-->>M: non-ok or parse failure M-->>R1: shared promise rejects M-->>R2: shared promise rejects R1-->>R1: fallback OG image R2-->>R2: fallback OG image M->>M: delete(username)Prompt To Fix All With AI
Reviews (1): Last reviewed commit: "perf: prevent cache stampede in OG image..." | Re-trigger Greptile