Skip to content
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

wrap multiple functions in same throat #6

Open
wheresrhys opened this issue Sep 27, 2015 · 2 comments
Open

wrap multiple functions in same throat #6

wheresrhys opened this issue Sep 27, 2015 · 2 comments

Comments

@wheresrhys
Copy link

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

@ForbesLindesay
Copy link
Owner

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.

@wheresrhys
Copy link
Author

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 notifications@github.com
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
#6 (comment)
.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants