Rate Limiting Api Calls


#1

I have a problem rate limiting http.get Api Calls. My goal is to put all apiCalls from every users requests into one queue and rate limit the calls (FIFO). This external API allows x requests every y seconds, and I would like to allow bursts.

I have been using this function to do my rate limiting earlier: http://www.matteoagosti.com/blog/2013/01/22/rate-limiting-function-calls-in-javascript/

I never had any problems with it. It works well even with Meteor, unless I’m using async function. Every time I’m trying to rate limit http.get, I’m getting that “Error: Meteor code must always run within a Fiber”, which means I’ve failed to modify my code for Meteor. Ive tried to wrapasync and what else! Just nothing works.

I’ve watched EventenMind videos, and I basically understand the concept. I just can’t modify this rate limiting function to work. I’m calling it from server side method, and eventually I want to insert the results into database, and view it on client side.

I haven’t found any other solution for rate limiting apicalls app wide, and allow bursts.

Can someone help me modify this existing function to work with http.get, or tell me another way of doing this? I REALLY need some help here :blush:

I know this is alot to ask, but I need to learn. Anything helps!

EDIT: I just figured out, that I can actually schedule a basic setTimeout function, and it works just fine. Weird…


#2

There is a good chance that you need to use Meteor.bindEnvironment to ensure your code is running inside a Fiber.


#3

Hmm… I just figured out, that I can actually schedule a basic setTimeout function, and it works just fine. I’ve thought that Meteor.http.get always runs within a fiber, and then its automatically environment bound? But maybe I understood wrong, and there is a problem with bindEnvironment.


#4

The solution was to use Meteor.setTimeout instead of setTimeout… I was trying to bindEnvironment on the callback, but all I had to do, is use Meteor setTimeout where that is already been done… God damnit :smiley: