Skip to content

Commit ce410ea

Browse files
fix(auth): debounce getToken() function
Problem: The Identity team noticed a large spike in token refreshes for specific users. One user would trigger refresh over 50 times within a few seconds. Solution: The telemetry showed that `getChatAuthState()` was being called many times in a short period. This eventually triggered the token refresh logic many times, if the token was expired. The solution is to add a debounce to `getToken()` which calls the refresh logic. - `debounce()` only accepts functions without any args, the refresh logic requires args - `getToken()` will also load from disk is the token is not expired, so debouncing here saves disk I/O as well. The current debounce interval is 100 milliseconds, which based on telemetry should be enough to capture the barrage of calls. Signed-off-by: nkomonen-amazon <[email protected]>
1 parent df7a3b6 commit ce410ea

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

packages/core/src/auth/sso/ssoAccessTokenProvider.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import { randomUUID } from '../../shared/crypto'
3333
import { getExtRuntimeContext } from '../../shared/vscode/env'
3434
import { showInputBox } from '../../shared/ui/inputPrompter'
3535
import { AmazonQPromptSettings, DevSettings, PromptSettings, ToolkitPromptSettings } from '../../shared/settings'
36-
import { onceChanged } from '../../shared/utilities/functionUtils'
36+
import { debounce, onceChanged } from '../../shared/utilities/functionUtils'
3737
import { NestedMap } from '../../shared/utilities/map'
3838
import { asStringifiedStack } from '../../shared/telemetry/spans'
3939
import { showViewLogsMessage } from '../../shared/utilities/messages'
@@ -97,7 +97,13 @@ export abstract class SsoAccessTokenProvider {
9797
this.reAuthState.set(this.profile, { reAuthReason: `invalidate():${reason}` })
9898
}
9999

100-
public async getToken(): Promise<SsoToken | undefined> {
100+
/**
101+
* Sometimes we get many calls at once and this
102+
* can trigger redundant disk reads, or token refreshes.
103+
* We debounce to avoid this.
104+
*/
105+
public getToken = debounce(this._getToken.bind(this), 100)
106+
public async _getToken(): Promise<SsoToken | undefined> {
101107
const data = await this.cache.token.load(this.tokenCacheKey)
102108
SsoAccessTokenProvider.logIfChanged(
103109
indent(

0 commit comments

Comments
 (0)