throat
throat copied to clipboard
wrap multiple functions in same throat
Hi,
Would you be open to a PR implementing the ability to throttle multiple functions with the same throat instance. Something like
myLimiter = throat(2);
lib.method1 = myLimiter.wrap(lib.method1)
lib.method2 = myLimiter.wrap(lib.method2)
The use case is where e.g. you use one method to read from a db, and another to write to it, but you want to place a limit on the total connections
You can always just do something like:
myLimiter = throat(2)
function myLimiterWrap(fn) {
return function () {
var self = this, args = arguments
return myLimiter(function () {
return fn.apply(this, args)
})
})
}
lib.method1 = myLimiterWrap(lib.method1)
lib.method2 = myLimiterWrap(lib.method2)
I'm not sure I see a lot of benefit to solving this at a library level.
I suppose I would see it in these terms: Throat gives you the ability to create a queue and associate that queue with either a single function to be used arbitrarily many times, or arbitrarily many functions to be used once each. The wrap function would allow all the use cases that fall somewhere in the middle to be covered, with probably very little extra code, which I think would make your library (a very useful one already) more flexible and powerful for very little cost
On Mon, 28 Sep 2015 at 1:18 p.m., Forbes Lindesay [email protected] wrote:
You can always just do something like:
myLimiter = throat(2)function myLimiterWrap(fn) { return function () { var self = this, args = arguments return myLimiter(function () { return fn.apply(this, args) }) }) } lib.method1 = myLimiterWrap(lib.method1) lib.method2 = myLimiterWrap(lib.method2)
I'm not sure I see a lot of benefit to solving this at a library level.
— Reply to this email directly or view it on GitHub https://github.com/ForbesLindesay/throat/issues/6#issuecomment-143727338 .