-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: how to function-rate-limit in a module? #4
Comments
In your preferred case, you'd need to call it like: var limited = require('module').MyRequestLimited;
var provider = whatever;
limited('http://www.google.com/', provider, fnerr, fnsuccess)('http://www.google.com/', provider, fnerr, fnsuccess); But the arguments passed to I'd probably do something like: var rateLimit = require('function-rate-limit');
exports.MyRequestLimited = function (limitCount, limitInterval) {
return rateLimit(limitCount, limitInterval, exports.myRequest);
};
exports.myRequest = function(url, provider, error, success) {
// my logic here...
}); So then you could call it like: var limited = require('module').MyRequestLimited;
var provider = whatever;
var limitedReq = limited(1, 3*1000);
limitedReq('http://www.google.com/', provider, fnerr, fnsuccess); Not sure if that's what you're aiming for. |
Thanks for the quick answer! Not really what I was looking forward: in |
Hmm. You could set the limiting values based on the provider like so: exports.MyRequestLimited = function(provider) {
return rateLimit(provider.limitCount, provider.limitInterval, exports.myRequest);
}); In order for the rate limiting to work, you'd probably need to create rate limited functions for each provider... function ProviderRequest(provider) {
return rateLimit(provider.limitCount, provider.limitInterval, exports.myRequest);
}
exports.myRequestLimited = function (url, provider, error, success) {
if (!provider.limitedReq) {
provider.limitedReq = ProviderRequest(provider);
}
provider.limitedReq(url, provider, error, success);
}
exports.myRequest = function(url, provider, error, success) {
// my logic here...
}); Not sure though. I don't have a full understanding of what you've got going on. |
Yes...
myRequest is called correctly, but it is not rate limited... :-( P.S.: you were very kind, until now... Please don't feel obliged to answer me, I don't want to waste more of your time... :-( |
Yeah, you have to keep the instance of the rate limited function around and call it repeatedly. Otherwise you're creating a new set of rate limiting "accounting books" each time you initialize a new one. If you can't append the limited function to the provider object, then hopefully each provider has a name or something and you can have a local hash containing the rate limited functions... var providerLimitedReq = {};
function ProviderRequest(provider) {
return rateLimit(provider.limitCount, provider.limitInterval, exports.myRequest);
}
exports.myRequestLimited = function (url, provider, error, success) {
var limitedReq = providerLimitedReq[provider.name] = providerLimitedReq[provider.name] || ProviderRequest(provider);
limitedReq(url, provider, error, success);
} Hope this helps! |
Finally I think I understand your points... |
Just a bit weird question:
I'm building a module ("network-request"), which places http requests.
I'd like to integrate "function-rate-limit" functionality in it, but I can't understand how to do it...
I did try:
Which works, but I don't like it, since, for example, I can't pass it the limits (1 request every 3 seconds, in this example...).
I'd prefer something like this:
But this doesn't work (it freezes...).
Any clues?
The text was updated successfully, but these errors were encountered: