Created
April 23, 2020 12:12
-
-
Save khaledosman/d70f07a5c423cbe3e8aa318214b2a4dd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function retryIfRequestRateTooLarge (promiseFn, currentRetry = 0, maxRetries = 100) { | |
return promiseFn() | |
.catch(async err => { | |
if ((err.message.includes('StatusCode: 429')) || err.message.includes('RetryAfterMs')) { | |
// if you're stuck here you might want to increase the collection's throughput (RU/s) temporarily from cosmosdb then set it back to 400 | |
const ms = err.message.split(',').find(str => str.includes('RetryAfterMs')).split('=')[1] || 1000 | |
console.log({ ms, currentRetry, maxRetries }) | |
if (currentRetry < maxRetries) { | |
return delay(Number(ms)).then(() => retryIfRequestRateTooLarge(promiseFn, currentRetry + 1, maxRetries)) | |
} else { | |
console.log('max retries attempted') | |
throw err | |
} | |
} else { | |
console.error(err) | |
throw err | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment