ixti/sidekiq-throttled

Dynamic throttling when using shared pool

popcorn opened this issue · 2 comments

Hey folks, I know about using sidekiq_throttle_as to let multiple workers use the same throttling pool.
I also know about dynamic throttling by using the key_suffix.

Now I wonder if there's a way to combine the two?

Here's my situation:

I have multiple users in my app.
They give me their API keys for a 3rd-party service.
I have multiple Sidekiq workers who send requests to the 3rd-party API at the same time.
There are rate limits on this API so workers need to share the throttling pool, but also the workers should be throttled by a key_prefix.

Is there a way to achieve this without me monkey-patching or forking the gem?

@ixti - Hey Alexey, do you know the answer for this from the top of your head?

ixti commented

You can use multi-rule on shared pool:

Sidekiq::Throttled::Registry.add(
  :third_party_apis,
  threshold: [
    { limit: 10, period: 1.minute },
    { limit: 2, period: 10.seconds, key_suffix: ->(...) { ... } }
  ]
)

I'm open to accept improvement PRs though :D