简体   繁体   中英

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. 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.

heres what it looks like and the library im using:

I'm trying to use this library:

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM