Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Group Concurrent getClient Requests #1848

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/auth/googleauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ export class GoogleAuth<T extends AuthClient = JSONClient> {
apiKey: string | null;

cachedCredential: AnyAuthClient | T | null = null;
/**
* A pending {@link AuthClient}. Used for concurrent {@link GoogleAuth.getClient} calls.
*/
#pendingAuthClient: Promise<AnyAuthClient | T> | null = null;

/**
* Scopes populated by the client library by default. We differentiate between
Expand Down Expand Up @@ -1007,7 +1011,22 @@ export class GoogleAuth<T extends AuthClient = JSONClient> {
async getClient(): Promise<AnyAuthClient | T> {
if (this.cachedCredential) {
return this.cachedCredential;
} else if (this.jsonContent) {
}

// Use an existing auth client request, or cache a new one
this.#pendingAuthClient =
this.#pendingAuthClient || this.#determineClient();

try {
return await this.#pendingAuthClient;
} finally {
// reset the pending auth client in case it is changed later
this.#pendingAuthClient = null;
}
}

async #determineClient() {
if (this.jsonContent) {
return this._cacheClientFromJSON(this.jsonContent, this.clientOptions);
} else if (this.keyFilename) {
const filePath = path.resolve(this.keyFilename);
Expand Down
22 changes: 22 additions & 0 deletions test/test.googleauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2617,6 +2617,28 @@ describe('googleauth', () => {

assert(actualClient instanceof ExternalAccountAuthorizedUserClient);
});

it('should return the same instance for concurrent requests', async () => {
// Set up a mock to return path to a valid credentials file.
mockEnvVar(
'GOOGLE_APPLICATION_CREDENTIALS',
'./test/fixtures/external-account-authorized-user-cred.json'
);
const auth = new GoogleAuth();

let client: AuthClient | null = null;
const getClientCalls = await Promise.all([
auth.getClient(),
auth.getClient(),
auth.getClient(),
]);

for (const resClient of getClientCalls) {
if (!client) client = resClient;

assert(client === resClient);
}
});
});

describe('sign()', () => {
Expand Down
Loading