Skip to content

Commit 2abd861

Browse files
committed
fix: solve changing retry strategy
1 parent 10959cb commit 2abd861

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

packages/core/src/shared/awsClientBuilderV3.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import { extensionVersion } from './vscode/env'
3333
import { getLogger } from './logger/logger'
3434
import { partialClone } from './utilities/collectionUtils'
3535
import { selectFrom } from './utilities/tsUtils'
36-
import { memoizeAsync } from './utilities/functionUtils'
36+
import { memoizeWith } from './utilities/functionUtils'
3737

3838
export type AwsClientConstructor<C> = new (o: AwsClientOptions) => C
3939

@@ -78,7 +78,23 @@ export class AWSClientBuilderV3 {
7878
return shim
7979
}
8080

81-
public getAwsService = memoizeAsync(this.createAwsService.bind(this))
81+
private keyAwsService<C extends AwsClient>(
82+
type: AwsClientConstructor<C>,
83+
options?: Partial<AwsClientOptions>,
84+
region?: string,
85+
userAgent: boolean = true,
86+
settings?: DevSettings
87+
): string {
88+
return [
89+
String(type),
90+
JSON.stringify(options), // Serializing this allows us to detect when nested objects change (ex. new retry strategy)
91+
region,
92+
userAgent ? '1' : '0',
93+
settings ? String(settings.get('endpoints', {})) : '',
94+
].join(':')
95+
}
96+
97+
public getAwsService = memoizeWith(this.createAwsService.bind(this), this.keyAwsService.bind(this))
8298

8399
public async createAwsService<C extends AwsClient>(
84100
type: AwsClientConstructor<C>,

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,18 @@ export function memoize<T, U extends any[]>(fn: (...args: U) => T): (...args: U)
7777
return (...args) => (cache[args.map(String).join(':')] ??= fn(...args))
7878
}
7979

80-
export function memoizeAsync<T, U extends any[]>(fn: (...args: U) => Promise<T>): (...args: U) => Promise<T> {
80+
export function memoizeWith<T, U extends any[]>(
81+
fn: (...args: U) => Promise<T>,
82+
key: (...args: U) => Promise<string> | string = (...args: U) => args.map(String).join(':')
83+
): (...args: U) => Promise<T> {
8184
const cache: Map<string, T> = new Map()
8285

8386
return async (...args) => {
84-
const key = args.map(String).join(':')
85-
if (!cache.has(key)) {
86-
cache.set(key, await fn(...args))
87+
const cacheKey = await key(...args)
88+
if (!cache.has(cacheKey)) {
89+
cache.set(cacheKey, await fn(...args))
8790
}
88-
return cache.get(key)!
91+
return cache.get(cacheKey)!
8992
}
9093
}
9194

packages/core/src/test/shared/awsClientBuilderV3.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { CredentialsShim } from '../../auth/deprecated/loginManager'
3131
import { Credentials, MetadataBearer, MiddlewareStack } from '@aws-sdk/types'
3232
import { oneDay } from '../../shared/datetime'
3333
import { ConfiguredRetryStrategy } from '@smithy/util-retry'
34+
import { StandardRetryStrategy } from '@smithy/util-retry'
3435

3536
describe('AwsClientBuilderV3', function () {
3637
let builder: AWSClientBuilderV3
@@ -98,20 +99,19 @@ describe('AwsClientBuilderV3', function () {
9899
})
99100

100101
it('recreates client when config options change', async function () {
102+
const retryStrategy = new ConfiguredRetryStrategy(10)
101103
const firstClient = await builder.getAwsService(TestClient, {
102-
retryStrategy: new ConfiguredRetryStrategy(10),
104+
retryStrategy: retryStrategy,
103105
})
104106

105107
const secondClient = await builder.getAwsService(TestClient, {
106-
retryStrategy: new ConfiguredRetryStrategy(11),
108+
retryStrategy: new StandardRetryStrategy(1),
107109
})
108110

109111
const thirdClient = await builder.getAwsService(TestClient, {
110-
retryStrategy: new ConfiguredRetryStrategy(10),
112+
retryStrategy: retryStrategy,
111113
})
112-
113114
assert.notStrictEqual(firstClient.id, secondClient.id)
114-
console.log('%s %s %s', firstClient.id, secondClient.id, thirdClient.id)
115115
assert.strictEqual(firstClient.id, thirdClient.id)
116116
})
117117
})

0 commit comments

Comments
 (0)