Skip to content

Instantly share code, notes, and snippets.

@khaledosman
Created April 23, 2020 12:12
Show Gist options
  • Save khaledosman/d70f07a5c423cbe3e8aa318214b2a4dd to your computer and use it in GitHub Desktop.
Save khaledosman/d70f07a5c423cbe3e8aa318214b2a4dd to your computer and use it in GitHub Desktop.
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