diff --git a/Changes.md b/Changes.md index eb4ac9acd..a493d74e4 100644 --- a/Changes.md +++ b/Changes.md @@ -5,6 +5,7 @@ HEAD --------- +- Reduce retry jitter, add jitter to `sidekiq_retry_in` values [#4957] - Minimize scheduler load on Redis at scale [#4882] - Improve logging of delay jobs [#4904, BuonOno] - Minor CSS improvements for buttons and tables, design PRs always welcome! diff --git a/lib/sidekiq/job_retry.rb b/lib/sidekiq/job_retry.rb index 552a75403..69ead3c28 100644 --- a/lib/sidekiq/job_retry.rb +++ b/lib/sidekiq/job_retry.rb @@ -214,16 +214,12 @@ def retry_attempts_from(msg_retry, default) end def delay_for(worker, count, exception) + jitter = rand(10) * (count + 1) if worker&.sidekiq_retry_in_block custom_retry_in = retry_in(worker, count, exception).to_i - return custom_retry_in if custom_retry_in > 0 + return custom_retry_in + jitter if custom_retry_in > 0 end - seconds_to_delay(count) - end - - # delayed_job uses the same basic formula - def seconds_to_delay(count) - (count**4) + 15 + (rand(30) * (count + 1)) + (count**4) + 15 + jitter end def retry_in(worker, count, exception) diff --git a/test/test_retry.rb b/test/test_retry.rb index bcaaf2e90..30a1fe24b 100644 --- a/test/test_retry.rb +++ b/test/test_retry.rb @@ -284,7 +284,7 @@ class CustomWorkerWithException sidekiq_retry_in do |count, exception| case exception when SpecialError - Sidekiq::JobRetry::USE_DEFAULT_RETRY_FORMULA + nil when ArgumentError count * 4 else @@ -306,11 +306,11 @@ class ErrorWorker end it "retries with a custom delay and exception 1" do - assert_equal 8, handler.__send__(:delay_for, CustomWorkerWithException, 2, ArgumentError.new) + assert_includes 4..35, handler.__send__(:delay_for, CustomWorkerWithException, 2, ArgumentError.new) end it "retries with a custom delay and exception 2" do - assert_equal 4, handler.__send__(:delay_for, CustomWorkerWithException, 2, StandardError.new) + assert_includes 4..35, handler.__send__(:delay_for, CustomWorkerWithException, 2, StandardError.new) end it "retries with a default delay and exception in case of configured with nil" do @@ -319,7 +319,7 @@ class ErrorWorker end it "retries with a custom delay without exception" do - assert_equal 4, handler.__send__(:delay_for, CustomWorkerWithoutException, 2, StandardError.new) + assert_includes 4..35, handler.__send__(:delay_for, CustomWorkerWithoutException, 2, StandardError.new) end it "falls back to the default retry on exception" do