Conversation
|
Important Review skippedAuto incremental reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the Use the checkbox below for a quick retry:
WalkthroughImplements deduplication of concurrent token refresh requests in Credentials by caching an in-flight promise. When multiple callers request access tokens simultaneously with no cached token available, they now await the same token refresh promise instead of triggering multiple simultaneous token exchanges. Includes test verification for concurrent request behavior. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ 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✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #333 +/- ##
==========================================
+ Coverage 89.67% 90.31% +0.63%
==========================================
Files 25 25
Lines 1492 1507 +15
Branches 279 263 -16
==========================================
+ Hits 1338 1361 +23
+ Misses 94 88 -6
+ Partials 60 58 -2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR addresses issue #332 by deduplicating concurrent access token refreshes so multiple callers share a single in-flight token exchange instead of triggering parallel requests.
Changes:
- Added an in-flight refresh lock (
refreshAccessTokenPromise) inCredentialsto dedupe concurrent refresh calls. - Added a regression test asserting 5 concurrent
getAccessTokenHeader()calls only trigger 1 token request.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| tests/credentials.test.ts | Adds concurrency regression test for single token exchange under parallel access-token header reads. |
| credentials/credentials.ts | Introduces a shared in-flight refresh promise to prevent concurrent token refresh thundering herd. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| export class Credentials { | ||
| private accessToken?: string; | ||
| private accessTokenExpiryDate?: Date; | ||
| private refreshAccessTokenPromise?: Promise<string | undefined>; | ||
|
|
There was a problem hiding this comment.
This file is marked as autogenerated (header) and CONTRIBUTING.md notes autogenerated files should not be modified directly; changes should be made in the sdk-generator templates/in tandem to avoid the fix being overwritten on regen. Please ensure this concurrency-lock change is also applied in the generator source (or confirm this file is intentionally maintained manually despite the header).
tests/credentials.test.ts
Outdated
| nock(expectedBaseUrl) | ||
| .post(expectedPath) | ||
| .times(5) | ||
| .delay(20) | ||
| .reply(() => { | ||
| tokenRequestCount += 1; | ||
| return [200, { | ||
| access_token: "shared-token", | ||
| expires_in: 300, | ||
| }]; | ||
| }); |
There was a problem hiding this comment.
The test name asserts a single token request, but the nock interceptor is configured with .times(5) and the scope isn't asserted with scope.isDone(). Consider making the mock expect exactly one call (e.g., .once()/default) and asserting isDone() so the test intent is enforced by nock as well as by tokenRequestCount.
|
Addressed the actionable test-review comment in commit Updated the concurrency regression test to enforce intent directly with nock:
This now verifies both:
Validated locally with:
On the autogenerated-file note: agreed in principle; this PR remains scoped to the runtime fix and test behavior. |
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
tests/credentials.test.ts
Outdated
| let tokenRequestCount = 0; | ||
|
|
||
| const scope = nock(expectedBaseUrl) | ||
| .post(expectedPath) | ||
| .once() |
There was a problem hiding this comment.
tokenRequestCount is redundant with nock(...).post(...).once()/scope.isDone() (a second token request would already fail the test). Consider removing the counter to simplify the test and avoid incidental state.
| if (!this.refreshAccessTokenPromise) { | ||
| this.refreshAccessTokenPromise = this.refreshAccessToken().finally(() => { | ||
| this.refreshAccessTokenPromise = undefined; | ||
| }); | ||
| } | ||
|
|
||
| return this.refreshAccessTokenPromise; | ||
| } |
There was a problem hiding this comment.
The new in-flight refresh lock is cleared via .finally(...), but there’s no regression test covering the failure path (e.g., token endpoint returns 500) to ensure: (1) all concurrent callers see the same rejection, and (2) a subsequent call retries and triggers a new token request after the promise is cleared. Adding a test for this would help prevent future regressions where the lock could get stuck on a rejected promise.
|
Addressed the actionable review feedback from #333 (review) in commit What changed:
Validation run:
|
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Summary
Ensures concurrent token header reads share one in-flight refresh instead of issuing parallel token exchange requests.
Changes
refreshAccessTokenPromiselock inCredentialsPotential Breaking Change
Fixes #332
Summary by CodeRabbit