Skip to content

Commit 11a9fcb

Browse files
committed
refactor: poll more frequently by leveraging cache
1 parent 80c25f3 commit 11a9fcb

File tree

5 files changed

+57
-15
lines changed

5 files changed

+57
-15
lines changed

packages/amazonq/src/lsp/auth.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import * as crypto from 'crypto'
1414
import { LanguageClient } from 'vscode-languageclient'
1515
import { AuthUtil } from 'aws-core-vscode/codewhisperer'
1616
import { Writable } from 'stream'
17+
import { onceChanged } from 'aws-core-vscode/utils'
18+
import { getLogger, oneMinute } from 'aws-core-vscode/shared'
1719

1820
export const encryptionKey = crypto.randomBytes(32)
1921

@@ -64,7 +66,7 @@ export const notificationTypes = {
6466
export class AmazonQLspAuth {
6567
constructor(private readonly client: LanguageClient) {}
6668

67-
async init() {
69+
async refreshConnection() {
6870
const activeConnection = AuthUtil.instance.auth.activeConnection
6971
if (activeConnection?.type === 'sso') {
7072
// send the token to the language server
@@ -73,7 +75,9 @@ export class AmazonQLspAuth {
7375
}
7476
}
7577

76-
private async updateBearerToken(token: string) {
78+
public updateBearerToken = onceChanged(this._updateBearerToken.bind(this))
79+
private async _updateBearerToken(token: string) {
80+
getLogger('amazonqLsp').info('Sending updated bearer token to LSP')
7781
const request = await this.createUpdateCredentialsRequest({
7882
token,
7983
})
@@ -83,6 +87,16 @@ export class AmazonQLspAuth {
8387
this.client.info(`UpdateBearerToken: ${JSON.stringify(request)}`)
8488
}
8589

90+
public startTokenRefreshInterval(pollingTime: number = oneMinute) {
91+
const interval = setInterval(async () => {
92+
await this.refreshConnection().catch((e) => {
93+
getLogger('amazonqLsp').error('Unable to update bearer token: %s', (e as Error).message)
94+
clearInterval(interval)
95+
})
96+
}, pollingTime)
97+
return interval
98+
}
99+
86100
private async createUpdateCredentialsRequest(data: any) {
87101
const payload = new TextEncoder().encode(JSON.stringify({ data }))
88102

packages/amazonq/src/lsp/client.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { InlineCompletionManager } from '../app/inline/completion'
1111
import { AmazonQLspAuth, encryptionKey, notificationTypes } from './auth'
1212
import { AuthUtil } from 'aws-core-vscode/codewhisperer'
1313
import { ConnectionMetadata } from '@aws/language-server-runtimes/protocol'
14-
import { Settings, oidcClientName, createServerOptions, globals, Experiments, getLogger } from 'aws-core-vscode/shared'
14+
import { Settings, oidcClientName, createServerOptions, globals, Experiments } from 'aws-core-vscode/shared'
1515
import { activate } from './chat/activation'
1616
import { AmazonQResourcePaths } from './lspInstaller'
1717

@@ -93,7 +93,7 @@ export async function startLanguageServer(
9393
const auth = new AmazonQLspAuth(client)
9494

9595
return client.onReady().then(async () => {
96-
await auth.init()
96+
await auth.refreshConnection()
9797
const inlineManager = new InlineCompletionManager(client)
9898
inlineManager.registerInlineCompletion()
9999
if (Experiments.instance.get('amazonqChatLSP', false)) {
@@ -109,24 +109,17 @@ export async function startLanguageServer(
109109
}
110110
})
111111

112-
// Temporary code for pen test. Will be removed when we switch to the real flare auth
113-
const authInterval = setInterval(async () => {
114-
try {
115-
await auth.init()
116-
} catch (e) {
117-
getLogger('amazonqLsp').error('Unable to update bearer token: %s', (e as Error).message)
118-
clearInterval(authInterval)
119-
}
120-
}, 300000) // every 5 minutes
112+
const refreshInterval = auth.startTokenRefreshInterval()
121113

122114
toDispose.push(
123115
AuthUtil.instance.auth.onDidChangeActiveConnection(async () => {
124-
await auth.init()
116+
await auth.refreshConnection()
125117
}),
126118
AuthUtil.instance.auth.onDidDeleteConnection(async () => {
127119
client.sendNotification(notificationTypes.deleteBearerToken.method)
128120
}),
129-
inlineManager
121+
inlineManager,
122+
{ dispose: () => clearInterval(refreshInterval) }
130123
)
131124
})
132125
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*!
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
import assert from 'assert'
6+
import { AmazonQLspAuth } from '../../../../src/lsp/auth'
7+
import { LanguageClient } from 'vscode-languageclient'
8+
9+
describe('AmazonQLspAuth', function () {
10+
describe('updateBearerToken', function () {
11+
it('makes request to LSP when token changes', async function () {
12+
// Note: this token will be encrypted
13+
let lastSentToken = {}
14+
const auth = new AmazonQLspAuth({
15+
sendRequest: (_method: string, param: any) => {
16+
lastSentToken = param
17+
},
18+
info: (_message: string, _data: any) => {},
19+
} as LanguageClient)
20+
21+
await auth.updateBearerToken('firstToken')
22+
assert.notDeepStrictEqual(lastSentToken, {})
23+
const encryptedFirstToken = lastSentToken
24+
25+
await auth.updateBearerToken('secondToken')
26+
assert.notDeepStrictEqual(lastSentToken, encryptedFirstToken)
27+
const encryptedSecondToken = lastSentToken
28+
29+
await auth.updateBearerToken('secondToken')
30+
assert.deepStrictEqual(lastSentToken, encryptedSecondToken)
31+
})
32+
})
33+
})

packages/core/src/shared/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,4 @@ export * from './lsp/utils/platform'
7272
export * as processUtils from './utilities/processUtils'
7373
export * as BaseLspInstaller from './lsp/baseLspInstaller'
7474
export * as collectionUtil from './utilities/collectionUtils'
75+
export * from './datetime'

packages/core/src/shared/utilities/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55

66
export { isExtensionInstalled, isExtensionActive } from './vsCodeUtils'
77
export { VSCODE_EXTENSION_ID } from '../extensions'
8+
export * from './functionUtils'

0 commit comments

Comments
 (0)