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

Add dynamic cooldown policies #174

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

Add dynamic cooldown policies #174

wants to merge 1 commit into from

Conversation

ixti
Copy link
Owner

@ixti ixti commented Dec 18, 2023

Allow Config#cooldown_period and Config#cooldown_threshold to be specified as Proc:

Sidekiq::Throttled.configure do |c|
  c.cooldown_period = lambda do |queue_name|
    queue_name == "default" ? 2.0 : 10.0
  end

  c.cooldown_threshold = lambda do |queue_name|
    queue_name == "mostly-throttled" ? 0 : 10
  end
end

This will go very well in pair with planned push-back strategies PR.

@codecov-commenter
Copy link

codecov-commenter commented Dec 18, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.69%. Comparing base (27ec05e) to head (01b769d).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #174   +/-   ##
=======================================
  Coverage   98.69%   98.69%           
=======================================
  Files          19       19           
  Lines         460      461    +1     
  Branches       77       77           
=======================================
+ Hits          454      455    +1     
  Misses          6        6           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ixti ixti requested a review from freemanoid December 18, 2023 22:25
@freemanoid
Copy link
Collaborator

  c.cooldown_period = lambda do |unit_of_work|

I'm not sure about the unit_of_work in the interface. Maybe a queue name would be enough? I'm just thinking about how it can be done in a bit more explicit fashion.

@ixti
Copy link
Owner Author

ixti commented Dec 18, 2023

I'm not sure about the unit_of_work in the interface. Maybe a queue name would be enough? I'm just thinking about how it can be done in a bit more explicit fashion.

My rationale behind unit of work was that one may want to have different period/threshold based on job class and/or args. I guess the most straight-forward API in this case will be:

c.cooldown_period = lambda do |queue_name, message|
  job_record = Sidekiq::JobRecord.new(message)

  if job_record.display_class == "FooJob"
    1.0
  elsif "default" == queue_name
    2.0
  else
    3.0
  end
end

Naturally, in this case, it will also support:

c.cooldown_period = lambda do |queue_name|
  if "default" == queue_name
    2.0
  else
    3.0
  end
end

@ixti
Copy link
Owner Author

ixti commented Dec 19, 2023

After giving it another thought, I think we can keep it simple :D just queue name.

@freemanoid
Copy link
Collaborator

After giving it another thought, I think we can keep it simple :D just queue name.

I like the Sidekiq::JobRecord idea tho. 😄

@ixti
Copy link
Owner Author

ixti commented Jan 16, 2024

I like the Sidekiq::JobRecord idea tho. 😄

I do like it too. But it will involve object creation even when user does not need it (e.g. just caring about queue_name. Thus passing queue_name where the message arrived from and message give full flexibility without sacrificing performance.

@ixti ixti force-pushed the dynamic-expireable-set branch from 84a284b to 21136ba Compare January 16, 2024 15:29
@ixti
Copy link
Owner Author

ixti commented Jan 19, 2024

I'm inclined towards do |queue, message| approach.

@ixti
Copy link
Owner Author

ixti commented Jan 19, 2024

Will wait with final decision until real case scenario appears.

The final goal is to allow specify cooldown_period as Proc:

   Sidekiq::Throttled.configure do |c|
     c.cooldown_period = lambda do |unit_of_work|
       unit_of_work.queue == "default" ? 2.0 : 10.0
     end
   end
@ixti ixti force-pushed the dynamic-expireable-set branch from 21136ba to 01b769d Compare November 17, 2024 19:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants