ixti / sidekiq-throttled

Concurrency and rate-limit throttling for Sidekiq
MIT License
701 stars 75 forks source link

key-suffix ussage #63

Closed danielgomezrico closed 5 years ago

danielgomezrico commented 5 years ago

Hi

What do you mean by some trouble?

NB Don't forget to specify :key_suffix and make it return different values if you are using dynamic limit/period options. Otherwise you risk getting into some trouble.

I want to speficy a static key-suffix for multiple jobs, so I can throttle all of them togetter with the same setup using the same key.

Is that possible?

I tried putting a static key-suffix but that results in never throttling any job.

danielgomezrico commented 5 years ago

I undestood badly the readme, it turns out that the key should receive a function that receive the same number of parameters as the perform function of the worker:

ex:

class MyWorker
  include Sidekiq::Worker
  include Sidekiq::Throttled::Worker

  sidekiq_options queue: "my_queue"

  sidekiq_throttle({
                       :threshold => {
                           :limit => 100,
                           :period => 10.second,
                           :key_suffix => -> (_, _) { "my-key" }
                       }
                   })

  def perform(call, email)
  end

end

Otherwise the throttle does not work and each job run as fast as it can.

ixti commented 5 years ago

Mm, yes. You can pass Proc instead of Lambda so that it won't require to have same amount of args. These are different way of achieving that:

# Proc

proc { "my-key" }
proc { |*| "my-key" }

# Lambda

lambda { |*| "my-key" }
->(*) { "my-key" }
danielgomezrico commented 5 years ago

@ixti yeah it worked, thanks