mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-25 11:55:03 +03:00
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:
parent
f8a705448b
commit
66b099222f
@ -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);
|
||||
};
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user