简体   繁体   中英

Node.js - How to throttle and limit logic but not limiting main endpoint?

The question might sound a little weird but I am not sure how to handle this specific issue.

I have a requests than does a few things and sends a response back to the client. In this specific requests i am also doing something after the response is sent back to do some backround stuff. The issue is lying inside this piece of logic / functionality since it requests a third party public API that has a rate limit. Let's say i send 20 requests/s to this endpoint.

10 requests will be processed by the third party public API and processed correctly, but the last 10 requests will not be processed and will return a rate limit error.

My question is - How can I change this logic / endpoint to support my demands. Any feedback and suggestions is much appreciated.

// endpoint /get-data/
await doAlotOfLogicHere();

setTimeout(() => {
      // Do something after 1 seconds
      // This specific action can only take 10 requests / second
      controller.doSomething(req,res)
    }, 1000);

// Send response to client
res.status(200).send(data);

controller.doSomething()

exports.doSomething = (req,res) => {
await sendDataToPublicAPI();
}

Your problem is a little big vague... Let me suggest something, though.

  1. Push the user request to a queue (this would be a database table with the resource, payload and timestamp)
  2. Fire it from a schedule or enter link description here
  3. Make little asynchronous request to check whether the response is avaiable
  4. Fetch the result data. It's best if you can cache it in your own database

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