简体   繁体   English

如何限制每个用户的 API 调用? 漏桶算法

[英]How to limit API calls Per User? Leaky bucket algorithm

I am trying to limit API calls on my app to shopify, the calls are limited at 2 calls each second for each user.我正在尝试将应用程序上的 API 调用限制为 shopify,每个用户的调用限制为每秒 2 次。 I have a domain which i can use to identify each user but I'm trying to use a common node library to do this - I have already built the backend WITHOUT using the shopify official npm package so I am trying to accomplish it with a node library or with a manual bit of code, I really am unsure how I can identify the limits per domain value, I havent seen much resources for this strangely.我有一个域,我可以用它来识别每个用户,但我正在尝试使用通用节点库来执行此操作 - 我已经在不使用 shopify 官方 npm 包的情况下构建了后端,因此我正在尝试使用节点来完成它库或使用一些手动代码,我真的不确定如何确定每个域值的限制,奇怪的是我没有看到太多资源。

heres what it looks like and the library im using:这是它的外观和我正在使用的库:

I'm trying to use this library:我正在尝试使用这个库:

https://www.npmjs.com/package/leaky-bucket 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);
            });     
        });
    })
    }

As you can see i parse the domain in this library although it doesnt mention anywhere it can identify it, can i add some domain identifier and mix it with this library or do i have to write my own code and somehow do it, OR just rewrite the damn thing with the official shopify library?正如你所看到的,我解析了这个库中的域,尽管它没有提到它可以识别它的任何地方,我可以添加一些域标识符并将它与这个库混合,还是我必须编写自己的代码并以某种方式执行它,或者只是重写官方shopify库有什么问题?

thanks very much if you can help.如果你能帮忙,非常感谢。

You could create an object that holds a list of buckets per domain:您可以创建一个包含每个域的存储桶列表的对象:

//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];
}

That way you create buckets per domain, at the expense of having a cache of buckets.这样你就可以为每个域创建存储桶,但代价是拥有存储桶的缓存。 I don't know if this fits your need and also beware that if you have 2 or more Node.js process running, each process will have a copy in memory of the buckets.我不知道这是否符合您的需要,并且还要注意,如果您有 2 个或更多 Node.js 进程在运行,则每个进程都会在存储桶的内存中拥有一个副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何限制网页访问每个IP 1个用户? - how to limit webpage access to 1 user per ip? Google街景图片API的每用户费率限制是多少? - What is the per user rate limit for Google Street View Image API? 如何将实时流页面限制为每个用户一个连接? - How to limit live streaming page to one connection per user? 如何限制每个用户的aws javascript dynamodb条目? - How to limit entries to aws javascript dynamodb per user? 如何在Javascript中增加每小时对Github API使用的限制 - How to increase your limit of Github API uses per hour in Javascript 尽管页面重新加载,如何限制api调用的速率? - How does one limit rate of api calls despite page reload? 如何对Google Drive API调用的速率进行限制? - How do I rate limit my Google Drive API calls? 如何在特定时间范围内使用速率限制进行异步API调用? - How to make async API calls with rate limit for a particular timespan? 如何在 React 中将 API 请求限制为每天一次? - How to limit an API request to once per day in React? 限制对外部 api 节点的调用 - Limit calls to external api node
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM