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

Multiple throttle options #149

Open
apetrov88 opened this issue Jun 8, 2023 · 5 comments
Open

Multiple throttle options #149

apetrov88 opened this issue Jun 8, 2023 · 5 comments
Labels

Comments

@apetrov88
Copy link

Is it possible to set multiple throttle option like:

5 per 1 minute
100 per 1 hour
1000 per 1 day

All of them for the same worker.

@ixti
Copy link
Owner

ixti commented Jun 8, 2023

You should be able to do something like:

sidekiq_throttled(
  threshold: [
    { limit: 5, period: 5.minutes, key_suffix: "minutely" },
    { limit: 100, period: 1.hour, key_suffix: "hourly" },
    { limit: 1000, period: 1.day, key_suffix: "daily" },
  ]
)

@apetrov88 apetrov88 reopened this Jul 18, 2023
@apetrov88
Copy link
Author

Does not seem to work for me. Can you confirm the settings above are correctly?

@ixti ixti added the question label Sep 4, 2023
@danishsatkut
Copy link

@apetrov88 What's your sidekiq and sidekiq-throttled version?

@jakeonrails
Copy link
Contributor

jakeonrails commented Dec 20, 2024

I can confirm that this does not work with:

sidekiq (7.3.7)
    sidekiq-cron (2.0.1)
      sidekiq (>= 6.5.0)
    sidekiq-throttled (1.5.1)
      sidekiq (>= 6.5)
  sidekiq
  sidekiq-cron
  sidekiq-throttled

In the Sidekiq Admin, the jobs that I attempt to throttle with an array of throttle options, both using string values for key_suffix as well as procs that return string values, the throttled job does not even appear in the list, and in fact is not throttled at all.

When I replace the array of throttle options with a single hash of throttle options, the job reappears and is throttled as expected.

I have tried:

Will not appear on sidekiq UI, no throttling happens:

  sidekiq_throttle(
    threshold: [
      { limit: 5, period: 5.minutes, key_suffix: ->(job_id) { 'minutely' } },
      { limit: 100, period: 1.hour, key_suffix: ->(job_id) { 'hourly' } },
      { limit: 1000, period: 1.day, key_suffix: ->(job_id) { 'daily' } }
    ]
  )

Will not appear on sidekiq UI, no throttling happens:

  sidekiq_throttle(
    threshold: [
      { limit: 5, period: 5.minutes, key_suffix: 'minutely' },
      { limit: 100, period: 1.hour, key_suffix: 'hourly' },
      { limit: 1000, period: 1.day, key_suffix: 'daily' }
    ]
  )

This appears in the UI and correctly throttles

sidekiq_throttle(threshold: { limit: 1, period: 1.second })

@jakeonrails
Copy link
Contributor

Follow up:

I can confirm that this style works:

  sidekiq_throttle(
    threshold: [
      { limit: 1, period: 1.second, key_suffix: ->(job_id) { 'secondly' } },
      { limit: 10, period: 1.minute, key_suffix: ->(job_id) { 'minutely' } }
    ]
  )

By "works" I mean that it correctly throttles the jobs at the 1/second and 10/minute rate.

It was a red herring that the job is not listed in the /throttled page in Sidekiq Admin - only so-called "static" throttles appear there. If there is any ->() procs, the jobs are not listed on the dashboard :(

ixti pushed a commit that referenced this issue Jan 12, 2025
Add docs about using string keys to create multiple limits per unit of
time.

I was confused about implementing this type of behavior until I saw your
comment in #149, explaining the use of strings for the key_suffix.
Hopefully this doc will help other realize they don't need a proc to do
this but that the key_suffix is required for having multiple limits.

I was simply doing this:

```ruby
throttle: [
  { limit: 500, period: 1.minute },
  { limit: 18_000, period: 1.day }
]
```

This showed up in the UI on Sidekiq Admin as two separate throttles so
it looked like it worked but it does not.

Signed-off-by: Jake Moffatt <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants