sidekiq-throttled icon indicating copy to clipboard operation
sidekiq-throttled copied to clipboard

Dynamic throttling when using shared pool

Open popcorn opened this issue 2 years ago • 1 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?

popcorn avatar Sep 03 '21 14:09 popcorn

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

popcorn avatar Sep 06 '21 08:09 popcorn

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

ixti avatar May 30 '23 02:05 ixti