Skip to content

Commit

Permalink
Limit request retries on errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank Schmid committed May 17, 2018
1 parent 6b2eb9f commit 24e7ceb
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/plugins/aws/provider/awsProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,22 +199,23 @@ class AwsProvider {
const requestOptions = _.isObject(options) ? options : {};
const shouldCache = _.get(requestOptions, 'useCache', false);
const paramsHash = objectHash.sha1(params);
const MAX_TRIES = 4;
const persistentRequest = (f) => new BbPromise((resolve, reject) => {
const doCall = () => {
const doCall = (numTry) => {
f()
// We're resembling if/else logic, therefore single `then` instead of `then`/`catch` pair
.then(resolve, e => {
if ((e.providerError && e.providerError.retryable) || e.statusCode === 429) {
if (numTry < MAX_TRIES && ((e.providerError && e.providerError.retryable) || e.statusCode === 429)) {
that.serverless.cli.log(
`Recoverable error occured (${e.message}), sleeping 5 seconds`
`Recoverable error occured (${e.message}), sleeping 5 seconds. Try ${numTry + 1} of ${MAX_TRIES}`
);
setTimeout(doCall, 5000);
setTimeout(doCall, 5000, numTry + 1);
} else {
reject(e);
}
});
};
return doCall();
return doCall(0);
});

// Emit a warning for misuses of the old signature including stage and region
Expand Down

0 comments on commit 24e7ceb

Please sign in to comment.