ixti / sidekiq-throttled

Concurrency and rate-limit throttling for Sidekiq
MIT License
717 stars 78 forks source link

Its possible to use this gem like this ? #199

Open brunomiguelpinto opened 6 days ago

brunomiguelpinto commented 6 days ago

i wanted to have a shared rule per job but have a limit of something per key ( per organization )

Sidekiq::Throttled::Registry.add(
  :shared_throttling_rule,
  concurrency: [
    { limit: 10, key_suffix: ->(args) { args['org_id'] || 'default' } }
  ]
)

and then I wanted different jobs to share the same limit per organization

class TesteOneJob < ApplicationJob
  include Sidekiq::Throttled::Worker
  sidekiq_throttle_as :shared_throttling_rule

  def perform(job_number, org_id:)
    Rails.logger.debug "Performing TesteOneJob #{job_number} for organization #{org_id} at #{Time.zone.now}"
    sleep(1)
    Rails.logger.debug "Finished TesteOneJob #{job_number} at #{Time.zone.now}"
  end
end

class TesteJob < ApplicationJob
  include Sidekiq::Throttled::Worker
  sidekiq_throttle_as :shared_throttling_rule

  def perform(job_number, org_id:)
    Rails.logger.debug "Performing TesteJob #{job_number} for organization #{org_id} at #{Time.zone.now}"
    sleep(1)
    Rails.logger.debug "Finished TesteJob #{job_number} at #{Time.zone.now}"
  end
end
brunomiguelpinto commented 6 days ago

I did some tests and it ignores what I specified I wanted to have the possibility to count different jobs with the same key and per organization

ixti commented 5 days ago

The setup looks correct. Will need to figure out why does that not work.