我正在尝试将我的应用程序上的API调用限制为shopify,每个用户的调用限制为每秒2次。我有一个域,我可以用来识别每个用户,但我正在尝试使用一个公共节点库来做这件事-我已经在没有使用shopify官方npm包的情况下建立了后端,所以我试图用节点库或手动代码来完成它,我真的不确定我如何才能识别每个域值的限制,我还没有看到太多奇怪的资源。
下面是它的外观和我使用的库:
我正在尝试使用这个库:
https://www.npmjs.com/package/leaky-bucket
const LeakyBucket = require('leaky-bucket');
var bucket = new LeakyBucket({
capacity: 2, // items per interval, defaults to 60
interval: 1, // seconds, defaults to 60
maxWaitingTime: 60
// seconds, defaults to 300
});
var exports = module.exports = {
getAllOrders: (req, res) => {
const domain = req.params.domain;
console.log(domain)
bucket.throttle(function(domain) {
db.getStoreTocken(domain, (result) => {
const shopRequestUrl = 'https://' + domain + '/admin/orders.json';
const shopRequestHeaders = { 'X-Shopify-Access-Token': result, };
console.log(shopRequestUrl)
console.log(result)
request.get(shopRequestUrl, { headers: shopRequestHeaders }).then((shopResponse) => {
res.status(200).end(shopResponse);
console.log(shopResponse)
}).catch((error) => {
res.status(error.statusCode).send(error.error.error_description);
});
});
})
}正如你所看到的,我解析了这个库中的域,尽管它没有提到可以识别它的任何地方,我可以添加一些域标识符并将其与这个库混合在一起吗,或者我必须编写自己的代码并以某种方式做到这一点,或者只是用官方的shopify库重写这个该死的东西?
如果你能帮上忙,非常感谢。
发布于 2019-01-24 05:08:49
您可以创建一个对象,该对象包含每个域的存储桶列表:
//buckets.js
const LeakyBucket = require('leaky-bucket');
let domains = {};
module.exports = (domain) => {
if(domains[domain]) {
return domains[domain]
}
domains[domain] = new LeakyBucket({
capacity: 2, // items per interval, defaults to 60
interval: 1, // seconds, defaults to 60
maxWaitingTime: 60
// seconds, defaults to 300
});
return domains[domain];
}通过这种方式,您可以为每个域创建存储桶,但代价是拥有存储桶缓存。我不知道这是否适合您的需要,同时也要注意,如果您有2个或更多的Node.js进程在运行,每个进程都会在内存中有一个存储桶的副本。
https://stackoverflow.com/questions/54335046
复制相似问题