Skip to content

Commit

Permalink
Reduce retry jitter, add jitter to sidekiq_retry_in, closes #4957
Browse files Browse the repository at this point in the history
  • Loading branch information
mperham committed Aug 9, 2021
1 parent e8df351 commit 3575ccb
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
1 change: 1 addition & 0 deletions Changes.md
Expand Up @@ -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!
Expand Down
10 changes: 3 additions & 7 deletions lib/sidekiq/job_retry.rb
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions test/test_retry.rb
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 3575ccb

Please sign in to comment.