Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added awsRequest for aws-sdk call backoff and retry in customResources #8338

Merged
merged 16 commits into from Oct 7, 2020
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions lib/plugins/aws/customResources/resources/utils.js
Expand Up @@ -90,11 +90,40 @@ function wait(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

const MAX_AWS_REQUEST_TRY = (() => {
const DEFAULT_MAX_AWS_REQUEST_TRY = 4;
const userValue = Number(process.env.SLS_AWS_REQUEST_MAX_RETRIES);
return userValue >= 0 ? userValue : DEFAULT_MAX_AWS_REQUEST_TRY;
})();

function awsRequest(serviceInstance, method, ...args) {
const callAws = requestTry => {
return serviceInstance[method](...args)
.promise()
.then(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use async/await

result => result,
error => {
if (
requestTry < MAX_AWS_REQUEST_TRY &&
error &&
(error.statusCode === 429 ||
(error.retryable && error.statusCode !== 403 && error.code !== 'CredentialsError'))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can limit this to (error.statusCode === 429 || error.retryable), as in Lambda we should not expect credential errors, so it's not the case to consider

) {
return wait(4000 + Math.random() * 3000).then(() => callAws(++requestTry));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

7 seconds, is a lot in lambda, let's make it maybe 2000 + Math.random() * 1000, and maybe increase DEFAULT_MAX_AWS_REQUEST_TRY to 8

}
throw error;
}
);
};
return callAws(1);
}

module.exports = {
logger,
response,
getEnvironment,
getLambdaArn,
handlerWrapper,
wait,
awsRequest,
};