mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-17 23:44:39 -05: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
1 changed files with 10 additions and 7 deletions
|
@ -6,11 +6,14 @@ const testBucket = new LeakyBucket(EXPECTED_API_EFFICIENCY * 25, 1);
|
||||||
|
|
||||||
module.exports = function createStripeRequest(makeRequest) {
|
module.exports = function createStripeRequest(makeRequest) {
|
||||||
return async function stripeRequest(stripe, ...args) {
|
return async function stripeRequest(stripe, ...args) {
|
||||||
if (stripe.__TEST_MODE__) {
|
const throttledMakeRequest = async (stripe, ...args) => {
|
||||||
await testBucket.throttle();
|
if (stripe.__TEST_MODE__) {
|
||||||
} else {
|
await testBucket.throttle();
|
||||||
await liveBucket.throttle();
|
} else {
|
||||||
}
|
await liveBucket.throttle();
|
||||||
|
}
|
||||||
|
return await makeRequest(stripe, ...args);
|
||||||
|
};
|
||||||
const errorHandler = (err) => {
|
const errorHandler = (err) => {
|
||||||
switch (err.type) {
|
switch (err.type) {
|
||||||
case 'StripeCardError':
|
case 'StripeCardError':
|
||||||
|
@ -20,7 +23,7 @@ module.exports = function createStripeRequest(makeRequest) {
|
||||||
case 'RateLimitError':
|
case 'RateLimitError':
|
||||||
// Ronseal
|
// Ronseal
|
||||||
debug('RateLimitError');
|
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
|
// We do not want to recurse further if we get RateLimitError
|
||||||
// after running the exponential backoff
|
// after running the exponential backoff
|
||||||
if (err.type === 'RateLimitError') {
|
if (err.type === 'RateLimitError') {
|
||||||
|
@ -48,7 +51,7 @@ module.exports = function createStripeRequest(makeRequest) {
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
return makeRequest(stripe, ...args).catch(errorHandler);
|
return throttledMakeRequest(stripe, ...args).catch(errorHandler);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue