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

[Question] Stacking throttle? #4

Open
psteininger opened this issue Apr 13, 2017 · 2 comments
Open

[Question] Stacking throttle? #4

psteininger opened this issue Apr 13, 2017 · 2 comments

Comments

@psteininger
Copy link

I haven't tried it myself, but I am wondering if there is a way to stack throttle definitions. Here is a use case:
We have a portion of a system that receives callbacks about incoming calls from provider A, and hangs up the call, and then schedules a job to start a call from provider B. Provider B has a rate limit of 2 requests per second. However, quite often provider A hits us twice with a callback for the same call, 3-15 seconds apart. So I would like to have 2 throttle guards:

  1. Standard 2 per second limit
  2. Parameter based throttle (i.e don't call the same number twice in a 20 second window)

How could I get this accomplished?

@nickelser
Copy link
Owner

nickelser commented Apr 14, 2017

It isn't documented (the other open issue addresses this 😄 #3), but you can have the throttle key based on a lambda, so you could programmatically generate a throttle key by the phone number.

So, how I would do it would be something like (untested, but should work):

class NumberCallbackJob < ApplicationJob
  throttle threshold: 2, period: 1.second
  concurrency 1, drop: true, key: -> (job) { job.arguments.first # if the first argument is a phone number e.g. }

  def perform
    # do the stuff
  end
end

@nickelser
Copy link
Owner

Oh my goodness, I didn't realize you opened both issues! Sorry if that is a repeat, then; let me know if there's an extension that would help with this (the feature request that might make sense would be multi-key handling).

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