Skip to content

Commit a32701f

Browse files
committed
move waitUntil inside acquireLock
1 parent 2a32743 commit a32701f

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

packages/core/src/codewhisperer/region/regionProfileManager.ts

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -140,17 +140,7 @@ export class RegionProfileManager {
140140
return []
141141
}
142142

143-
// WaitUntil lock is acquired, the API tps is low, server can't afford multiple IDE instances making the calls simultaneiously
144-
const cached = await waitUntil(
145-
async () => {
146-
const lock = await this.tryAcquireLock()
147-
RegionProfileManager.logger.info(`try obtaining cache lock %s`, lock)
148-
if (lock) {
149-
return lock
150-
}
151-
},
152-
{ timeout: 15000, interval: 1500, truthy: true }
153-
)
143+
const cached = await this.acquireLock()
154144

155145
RegionProfileManager.logger.info(`obtained cache lock %s`, cached)
156146

@@ -459,22 +449,37 @@ export class RegionProfileManager {
459449
})
460450
}
461451

462-
private async tryAcquireLock(): Promise<CachedApiResultWithLock | undefined> {
463-
const cachedValue = globals.globalState.tryGet<CachedApiResultWithLock>(
464-
'aws.amazonq.regionProfiles.cachedResult',
465-
Object,
466-
{ locked: false, result: undefined }
467-
)
452+
private async acquireLock(): Promise<CachedApiResultWithLock | undefined> {
453+
async function _acquireLock() {
454+
const cachedValue = globals.globalState.tryGet<CachedApiResultWithLock>(
455+
'aws.amazonq.regionProfiles.cachedResult',
456+
Object,
457+
{ locked: false, result: undefined }
458+
)
468459

469-
if (!cachedValue.locked) {
470-
await globals.globalState.update('aws.amazonq.regionProfiles.cachedResult', {
471-
...cachedValue,
472-
isAcquired: true,
473-
})
460+
if (!cachedValue.locked) {
461+
await globals.globalState.update('aws.amazonq.regionProfiles.cachedResult', {
462+
...cachedValue,
463+
isAcquired: true,
464+
})
474465

475-
return cachedValue
466+
return cachedValue
467+
}
468+
return undefined
476469
}
477-
return undefined
470+
471+
const lock = await waitUntil(
472+
async () => {
473+
const lock = await _acquireLock()
474+
RegionProfileManager.logger.info(`try obtaining cache lock %s`, lock)
475+
if (lock) {
476+
return lock
477+
}
478+
},
479+
{ timeout: 15000, interval: 1500, truthy: true }
480+
)
481+
482+
return lock
478483
}
479484

480485
private async releaesLock(cached: CachedApiResultWithLock) {

0 commit comments

Comments
 (0)