Skip to content

Commit ed66120

Browse files
authored
fix(codewhisperer): latency caused by credentials (#2946)
Fixes performance issue from cognito credentials
1 parent 576bd95 commit ed66120

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "Bug Fix",
3+
"description": "latency caused by unnecessary refreshing of credentials"
4+
}

src/codewhisperer/client/codewhisperer.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import { AWSError, Service } from 'aws-sdk'
6+
import { AWSError, CognitoIdentityCredentials, Service } from 'aws-sdk'
77
import apiConfig = require('./service-2.json')
88
import globals from '../../shared/extensionGlobals'
99
import * as CodeWhispererClient from './codewhispererclient'
@@ -39,8 +39,23 @@ export type ListCodeScanFindingsRequest = Readonly<CodeWhispererClient.ListCodeS
3939
export type ArtifactType = Readonly<CodeWhispererClient.ArtifactType>
4040
export type ArtifactMap = Readonly<CodeWhispererClient.ArtifactMap>
4141
export class DefaultCodeWhispererClient {
42+
private credentials?: CognitoIdentityCredentials
43+
4244
private async createSdkClient(): Promise<CodeWhispererClient> {
43-
const credentials = !isCloud9() ? await getCognitoCredentials() : undefined
45+
try {
46+
if (!this.credentials && !isCloud9()) {
47+
this.credentials = await getCognitoCredentials()
48+
}
49+
if (this.credentials && this.credentials.needsRefresh()) {
50+
await new Promise<void>((resolve, reject) =>
51+
this.credentials?.refresh(e => (e ? reject(e) : resolve()))
52+
)
53+
}
54+
} catch (e) {
55+
getLogger().debug('Error when getting or refreshing Cognito credentials', e)
56+
throw new CognitoCredentialsError('Error when getting or refreshing Cognito credentials')
57+
}
58+
4459
const accessToken = globals.context.globalState.get<string | undefined>(CodeWhispererConstants.accessToken)
4560
const isOptedOut = CodeWhispererSettings.instance.isOptoutEnabled()
4661

@@ -49,7 +64,7 @@ export class DefaultCodeWhispererClient {
4964
{
5065
apiConfig: apiConfig,
5166
region: CodeWhispererConstants.region,
52-
credentials: credentials,
67+
credentials: this.credentials,
5368
endpoint: CodeWhispererConstants.endpoint,
5469
onRequestSetup: [
5570
req => {
@@ -132,3 +147,5 @@ export class DefaultCodeWhispererClient {
132147
return (await this.createSdkClient()).listCodeScanFindings(request).promise()
133148
}
134149
}
150+
151+
export class CognitoCredentialsError extends Error {}

src/codewhisperer/service/recommendationHandler.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55

66
import * as vscode from 'vscode'
77
import { extensionVersion } from '../../shared/vscode/env'
8-
import { RecommendationsList, DefaultCodeWhispererClient, Recommendation } from '../client/codewhisperer'
8+
import {
9+
RecommendationsList,
10+
DefaultCodeWhispererClient,
11+
Recommendation,
12+
CognitoCredentialsError,
13+
} from '../client/codewhisperer'
914
import * as EditorContext from '../util/editorContext'
1015
import * as CodeWhispererConstants from '../models/constants'
1116
import { ConfigurationEntry } from '../models/model'
@@ -192,6 +197,9 @@ export class RecommendationHandler {
192197
}
193198
}
194199
} catch (error) {
200+
if (error instanceof CognitoCredentialsError) {
201+
shouldRecordServiceInvocation = false
202+
}
195203
if (latency === 0) {
196204
latency = startTime !== 0 ? performance.now() - startTime : 0
197205
}

src/codewhisperer/util/cognitoIdentity.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ export const getCognitoCredentials = async (): Promise<CognitoIdentityCredential
2929
}
3030

3131
const credentials = new CognitoIdentityCredentials({ IdentityId: identityId }, { region })
32-
await credentials.getPromise()
3332
return credentials
3433
} catch (err) {
3534
getLogger().error(`Failed to initialize Cognito identity for CodeWhisperer: ${err} in region: ${region}`)

0 commit comments

Comments
 (0)