From 66b099222fbd02d1a39bf1b47c7712a5b4f99732 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Tue, 18 Aug 2020 11:28:12 +0100 Subject: [PATCH] Fixed throttling of Stripe API requests no-issue This ensures any requests during exponential backoff are correctly rate limited too --- .../lib/stripe/api/createStripeRequest.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/ghost/members-api/lib/stripe/api/createStripeRequest.js b/ghost/members-api/lib/stripe/api/createStripeRequest.js index 005bd0a5e4..e0a2473086 100644 --- a/ghost/members-api/lib/stripe/api/createStripeRequest.js +++ b/ghost/members-api/lib/stripe/api/createStripeRequest.js @@ -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); }; };