@cloud-copilot/iam-collect
Version:
Collect IAM information from AWS Accounts
103 lines • 3.98 kB
JavaScript
import { LambdaClient } from '@aws-sdk/client-lambda';
import {} from '@aws-sdk/types';
import { NodeHttpHandler } from '@smithy/node-http-handler';
import { AdaptiveRetryStrategy, DefaultRateLimiter } from '@smithy/util-retry';
import {} from '../services.js';
import {} from './coreAuth.js';
const retrySettings = {
[LambdaClient.name]: {
beta: 0.4, // cut harder on throttle (default 0.7)
minFillRate: 0.2, // lower baseline QPS (default 0.5)
scaleConstant: 0.1, // slower cubic ramp (default 0.4)
smooth: 0.5 // dampen measured rate (default 0.8)
}
};
export class AwsClientPool {
constructor() {
this.clientCache = new Map();
this.defaultRetryStrategy = this.makeRetryStrategy({
beta: 0.5, // cut harder on throttle (default 0.7)
minFillRate: 0.25, // lower baseline QPS (default 0.5)
scaleConstant: 0.2, // slower cubic ramp (default 0.4)
smooth: 0.6 // dampen measured rate (default 0.8)
});
}
/**
* Returns a client of the specified type with the specified credentials and region.
* Will create a new client if one does not already exist in the cache.
*
* @param ClientType The client constructor to create an instance of.
* @param credentials The credentials to use for the client.
* @param region The region to use for the client.
* @returns A client of the specified type with the specified credentials and region.
*/
client(ClientType, credentials, region, endpoint) {
const cacheKey = this.getCacheKey(ClientType, credentials, region, endpoint);
if (!this.clientCache.has(cacheKey)) {
const client = new ClientType({
credentials: credentials.provider,
region,
maxAttempts: 10,
requestHandler: new NodeHttpHandler({
connectionTimeout: 5000,
socketTimeout: 15000
}),
retryStrategy: this.retryStrategyForClient(ClientType)
});
this.clientCache.set(cacheKey, client);
}
return this.clientCache.get(cacheKey);
}
getCacheKey(ClientType, credentials, region, endpoint) {
return `${ClientType.name}:${credentials.accountId}:${credentials.cacheKey}:${region}:${endpoint}`;
}
/**
* Destroys all clients in the pool and empties the cache.
*
* NOT THREAD SAFE, this should only be called when all other operations are complete.
*/
clear() {
this.clientCache.forEach((client) => {
if (typeof client.destroy === 'function') {
client.destroy();
}
});
this.clientCache.clear();
}
/**
* Do any async initialization required for the client pool. Should be called
* before the first use of the pool.
*/
async init() { }
/**
* Whether this client pool requires valid AWS credentials for each instance.
*/
requiresAwsCredentials() {
return true;
}
/**
* Determine if a given sync is supported by this client pool for the given service and region.
*
* @param service the AWS service
* @param syncName the name of the sync operation
* @param region the AWS region
* @returns true if the sync is supported, false otherwise
*/
isSyncSupported(service, syncName, region) {
return true;
}
retryStrategyForClient(client) {
const settings = retrySettings[client.name];
if (settings) {
return this.makeRetryStrategy(settings);
}
return this.defaultRetryStrategy;
}
makeRetryStrategy(rateLimiterOptions) {
return new AdaptiveRetryStrategy(async () => 20, {
rateLimiter: new DefaultRateLimiter(rateLimiterOptions)
});
}
}
AwsClientPool.defaultInstance = new AwsClientPool();
//# sourceMappingURL=ClientPool.js.map