Fixed throttling of Stripe API requests

no-issue

This ensures any requests during exponential backoff are correctly rate
limited too
This commit is contained in:
Fabien O'Carroll 2020-08-18 11:28:12 +01:00
parent f8a705448b
commit 66b099222f

View File

@ -6,11 +6,14 @@ const testBucket = new LeakyBucket(EXPECTED_API_EFFICIENCY * 25, 1);
module.exports = function createStripeRequest(makeRequest) {
return async function stripeRequest(stripe, ...args) {
if (stripe.__TEST_MODE__) {
await testBucket.throttle();
} else {
await liveBucket.throttle();
}
const throttledMakeRequest = async (stripe, ...args) => {
if (stripe.__TEST_MODE__) {
await testBucket.throttle();
} else {
await liveBucket.throttle();
}
return await makeRequest(stripe, ...args);
};
const errorHandler = (err) => {
switch (err.type) {
case 'StripeCardError':
@ -20,7 +23,7 @@ module.exports = function createStripeRequest(makeRequest) {
case 'RateLimitError':
// Ronseal
debug('RateLimitError');
return exponentiallyBackoff(makeRequest, ...args).catch((err) => {
return exponentiallyBackoff(throttledMakeRequest, stripe, ...args).catch((err) => {
// We do not want to recurse further if we get RateLimitError
// after running the exponential backoff
if (err.type === 'RateLimitError') {
@ -48,7 +51,7 @@ module.exports = function createStripeRequest(makeRequest) {
throw err;
}
};
return makeRequest(stripe, ...args).catch(errorHandler);
return throttledMakeRequest(stripe, ...args).catch(errorHandler);
};
};