我不明白如何克服 Stripe 的速率限制
I don't understand how to get over Stripe's rate limit
我正在尝试使用 Stripe 和 NodeJS(准确地说是 Express)开发电子商务网站的后端。
当服务器启动时,我正在尝试从 Stripe 获取我的产品。但是在第一个 stripe.products.list
调用之后,我收到一条错误消息,提示我超出了 api 速率限制。这不是真的,因为正如它在 Stripe doc 中所说,在测试模式下速率限制为 25/秒,而我在进行第二次调用之前等待 10 秒。
请在下面找到我用来拨打电话的功能。我只是在每次调用之前在带有 sleep() 函数的循环中使用它。
async function fetchFromLastObj(last_obj){
const data = stripe.products.list({
active: true,
limit: maxRetrieve,
starting_after: last_obj,
})
.then((resp) => {
console.log(`Retrieved ${resp.data.length} products.`);
return resp.data;
})
.catch((e) => { });
return data;
}
休眠功能:
const { promisify } = require('util')
const sleep = promisify(setTimeout)
有问题的循环:
var last_obj_seen = null;
var nb_iters = 0;
// fetching all products from stripe
while (true) {
console.log(`Iteration ${nb_iters+1}...`)
let fetchedList = [];
if (last_obj_seen == null) {
fetchedList = await fetchFirstBatch();
} else {
fetchedList = await fetchFromLastObj(last_obj_seen);
}
fetchedList = Array.from(fetchedList);
if (fetchedList.length == 0) { break; };
last_obj_seen = fetchedList.slice(-1)[0];
await sleep(10000);
fetchPrices((fetchedList))
.then((fetchedListWithPrices)=>{
saveList(fetchedListWithPrices);//not asynchronous
})
.catch((err) => { console.error("While fetching products from Stripe..."); console.error(err); });
nb_iters += 1;
if(nb_iters > 100){ throw Error("Infinite loop error"); }
if (nb_iters !== 0){
console.log("Waiting before request...");
await sleep(10000);
}
}
console.log("Done.");
您可以使用 the auto-pagination feature of the official Stripe libraries.
而不是自己处理分页逻辑
Our libraries support auto-pagination. This feature easily handles fetching large lists of resources without having to manually paginate results and perform subsequent requests.
在 Node 10+ 中你可以这样做,例如:
for await (const product of stripe.products.list()) {
// Do something with product
}
Stripe Node 库将在后台为您处理分页。
我正在尝试使用 Stripe 和 NodeJS(准确地说是 Express)开发电子商务网站的后端。
当服务器启动时,我正在尝试从 Stripe 获取我的产品。但是在第一个 stripe.products.list
调用之后,我收到一条错误消息,提示我超出了 api 速率限制。这不是真的,因为正如它在 Stripe doc 中所说,在测试模式下速率限制为 25/秒,而我在进行第二次调用之前等待 10 秒。
请在下面找到我用来拨打电话的功能。我只是在每次调用之前在带有 sleep() 函数的循环中使用它。
async function fetchFromLastObj(last_obj){
const data = stripe.products.list({
active: true,
limit: maxRetrieve,
starting_after: last_obj,
})
.then((resp) => {
console.log(`Retrieved ${resp.data.length} products.`);
return resp.data;
})
.catch((e) => { });
return data;
}
休眠功能:
const { promisify } = require('util')
const sleep = promisify(setTimeout)
有问题的循环:
var last_obj_seen = null;
var nb_iters = 0;
// fetching all products from stripe
while (true) {
console.log(`Iteration ${nb_iters+1}...`)
let fetchedList = [];
if (last_obj_seen == null) {
fetchedList = await fetchFirstBatch();
} else {
fetchedList = await fetchFromLastObj(last_obj_seen);
}
fetchedList = Array.from(fetchedList);
if (fetchedList.length == 0) { break; };
last_obj_seen = fetchedList.slice(-1)[0];
await sleep(10000);
fetchPrices((fetchedList))
.then((fetchedListWithPrices)=>{
saveList(fetchedListWithPrices);//not asynchronous
})
.catch((err) => { console.error("While fetching products from Stripe..."); console.error(err); });
nb_iters += 1;
if(nb_iters > 100){ throw Error("Infinite loop error"); }
if (nb_iters !== 0){
console.log("Waiting before request...");
await sleep(10000);
}
}
console.log("Done.");
您可以使用 the auto-pagination feature of the official Stripe libraries.
而不是自己处理分页逻辑Our libraries support auto-pagination. This feature easily handles fetching large lists of resources without having to manually paginate results and perform subsequent requests.
在 Node 10+ 中你可以这样做,例如:
for await (const product of stripe.products.list()) {
// Do something with product
}
Stripe Node 库将在后台为您处理分页。